Introduction to Computer Science for Engineers L-3: Object-Oriented - - PowerPoint PPT Presentation

introduction to computer science for engineers
SMART_READER_LITE
LIVE PREVIEW

Introduction to Computer Science for Engineers L-3: Object-Oriented - - PowerPoint PPT Presentation

I C S E Introduction to Computer Science for Engineers L-3: Object-Oriented Design ICSE Exercise Update Working during the weekend is not very comfortable :/ Hence, we change the rhythm of publishing/submitting exercises tasks


slide-1
SLIDE 1

I C S E

Introduction to Computer Science for Engineers

L-3: Object-Oriented Design

slide-2
SLIDE 2

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

2

Exercise Update

  • Working during the weekend is not very comfortable :/
  • Hence, we change the rhythm of publishing/submitting

exercises

  • tasks will be published approx. 10 days before the exercise


—> on monday evening or tuesday morning

  • submission deadline will be fridays (before the

corresponding exercise) at 2 pm!

slide-3
SLIDE 3

Some Old Slides

slide-4
SLIDE 4

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

4

Source Code: Welcome

/* A simple Java Program,which says Welcome to ICSE */ public class Welcome { public static int var; public static void say(String s) { System.out.print(“Welcome to”+s); } public static void main(String[] argv) { say(”ICSE!"); } } the class name (Save code as Welcome.java) a variable a method The main method (The entry point while executing the program) a comment

slide-5
SLIDE 5

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

5

Source Code: Welcome

/* A simple Java Program,which says Welcome to ICSE */ public class Welcome { public static int var; public static void say(String s) { System.out.print(“Welcome to”+s); } public static void main(String[] argv) { say(”ICSE!"); } } the class name (Save code as Welcome.java) a variable a method The main method (The entry point while executing the program) a comment public static void main(String[] argv) {

slide-6
SLIDE 6

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

6

Methods

Signature:

  • name combined with the number and types of its parameters

Example:

  • Call of the method for max for int and double parameter

Notes:

  • d2: implicit type conversion

int à double

  • i2: explicit type conversion

double à int

slide-7
SLIDE 7

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

7

Methods

Signature:

  • name combined with the number and types of its parameters

Example:

  • Call of the method for max for int and double parameter

Notes:

  • d2: implicit type conversion

int à double

  • i2: explicit type conversion

double à int

double i1 = max (1,2); … int i2 = max ( (int) d2, i1);

slide-8
SLIDE 8

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

8

Content

Object-Oriented Design

  • Goals, Principles and Pattern
  • Inheritance
  • Overloading vs. Overriding
  • Types of Inheritance
  • Interfaces and Abstract Classes
slide-9
SLIDE 9

Object-Oriented Design:
 Goals, Principles and Design Patterns

slide-10
SLIDE 10

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

10

Object-Oriented Design: Goals

slide-11
SLIDE 11

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

11

Object-Oriented Design: Goals

Robustness:

  • to be capable of handling unexpected inputs that are not

explicitly defined for its application

slide-12
SLIDE 12

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

12

Object-Oriented Design: Goals

Robustness:

  • to be capable of handling unexpected inputs that are not

explicitly defined for its application

Adaptability:

  • to be able to evolve over time in response to changing

conditions in its environment

slide-13
SLIDE 13

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

13

Object-Oriented Design: Goals

Robustness:

  • to be capable of handling unexpected inputs that are not

explicitly defined for its application

Adaptability:

  • to be able to evolve over time in response to changing

conditions in its environment

  • Portability (related concept):
  • the ability of software to run with minimal change on 


different hardware and operation system platforms

slide-14
SLIDE 14

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

14

Object-Oriented Design: Goals

Robustness:

  • to be capable of handling unexpected inputs that are not explicitly

defined for its application

Adaptability:

  • to be able to evolve over time in response to changing conditions in

its environment

  • Portability (related concept):
  • the ability of software to run with minimal change on 


different hardware and operation system platforms

Reusability:

  • to be able to use the same code in a component of different systems
slide-15
SLIDE 15

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

15

Object-Oriented Design: Principles

slide-16
SLIDE 16

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

16

Object-Oriented Design: Principles

Abstraction:

  • to distill a system down to its most fundamental parts
  • to describe the parts of a system involves naming them and

explaining their functionality

  • to hide the implementation details from the user
slide-17
SLIDE 17

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

17

Object-Oriented Design: Principles

Abstraction:

  • to distill a system down to its most fundamental parts
  • to describe the parts of a system involves naming them and explaining

their functionality

  • to hide the implementation details from the user
  • Example: Abstract Data Types (ADT)
  • a mathematical model of a data structure that specifies the type of data

stored, the operations supported on them, and the types of parameters of the operations

  • expressed by an interface:
  • specifies what each operation does, but not how it does it
  • realized by a class:
  • specifies how the operations are performed a
slide-18
SLIDE 18

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

18

Object-Oriented Design: Principles

Encapsulation:

  • to hide the implementation details of different components of

a software system

  • each module maintains a consistent interface but reveals

no internal details for outsiders

  • gives the programmers the freedom in implementing the

details

slide-19
SLIDE 19

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

19

Object-Oriented Design: Principles

Encapsulation:

  • to hide the implementation details of different components of a

software system

  • each module maintains a consistent interface but reveals no

internal details for outsiders

  • gives the programmers the freedom in implementing the details

Modularity:

  • refers to an organizing principle in which different components
  • f a software system are divided into separate independent

functional units to reduce complexity

slide-20
SLIDE 20

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

20

Object-Oriented Design: Design Patterns

Design patterns:

  • provides a general template for a solution that can be applied in many

different situations.

Elements of a design pattern:

  • Name:
  • to identify the pattern;
  • Context:
  • describes the scenarios for which this pattern can be applied
  • Template:
  • describes how to implement the pattern
  • Result:
  • describes and analyzes what the pattern produces
slide-21
SLIDE 21

Classes and Objects: Basic Concepts

slide-22
SLIDE 22

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

22

Classes and Objects

Class: a template/blue print, defining the data which the object stores and the

methods for accessing and modifying that data Classes can contain any of the following variable types:

  • local variables: data inside methods, constructors or blocks.
  • will be declared and initialized within the method
  • will be destroyed when the method has completed
  • instance variables: data within a class but outside any method
  • will be initialized when the class is instantiated
  • can be accessed from inside any method, constructor or blocks

Methods represent operations that can act on the data associated

  • consists of constructors, procedures, and functions
slide-23
SLIDE 23

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

23

Classes and Objects

Class: a template/blue print, defining the data which the object stores and

the methods for accessing and modifying that data

  • variables: represent the data associated with an object
  • methods: operations that can act on the data associated

public class Counter { public int count; // instance variable public Counter( ) { } // method (default constructor) public Counter(int a){ // method (constructor with one parameter) int count = a; // local variable } public void increment( ){count++;} // change the instance variable public void increment(int a){count+=a;} // change the instance variable public int getCount( ) {return count;} // an accessor method } }

slide-24
SLIDE 24

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

24

Classes and Objects

Keyword this

  • to differentiate between an instance variable and a local variable

with the same name

  • allow one constructor body to invoke another constructor body

super(values);

public class Counter { public int count; // instance variable public Counter( ) { // method (default constructor) this(0); // invoke one-parameter constructor with value zero } public Counter(int a){ // method (constructor with one-parameter) this.count = a; // set the instance variable equal to parameter } }

slide-25
SLIDE 25

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

25

Classes and Objects

Object: is an instance of a class which have a state and behavior

  • a new object is created by using the new operator:
  • a new object is dynamically allocated in memory, and all instance

variables are initialized to standard default values

  • the constructor is called with the parameters specified
  • a reference to the newly created object is returned

public class Example{ public static void main(String[ ] args) { Counter c; // declares a variable; Counter d = new Counter(3); // creates a new object; assigns reference c = new Counter(); d = c; // assigns d to reference the same object as c } }

slide-26
SLIDE 26

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

26

Classes and Objects

slide-27
SLIDE 27

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

27

Classes and Objects

The Dot Operator:

  • to access methods and instance variables associated with an object

public class Example{ public static void main(String[ ] args) { Counter countVariable = new Counter(); // creates a new object countVariable.increment(); // call a method countVaraible.count = 3; // call a variable countVaariable.increment(2); // call a method } } ObjectReference = new Constructor(); //create an object ObjectReference.variableName; //call a variable ObjectReference.methodName(); //call a method

slide-28
SLIDE 28

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

28

Classes and Objects

Overloading:

  • in the same class, same method with different signatures
  • signature of a method is a name combined with the number and types
  • f its parameters

Counter c = new Counter( ); // creates a new object; assigns reference c.increment(); // increases its value by 1 c.increment(3) // increases its value by 3 public class Counter { public int count; // instance variable public Counter( ) { } // method (default constructor) public void increment( ){count++;} // change the instance variable public void increment(int a){count+=a;} // change the instance variable }

slide-29
SLIDE 29

Classes and Objects: Access Modifier

slide-30
SLIDE 30

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

30

Classes and Objects

Access Modifier:

  • control the level of access for classes, variables, methods and constructors
  • public: anyone can access the defined aspect
  • private: anyone of the same class ( not methods of a subclass) can access the

defined aspect

  • protected: anyone of the same package or of its subclasses can access the

defined aspect

public data type variable [ = value][, variable [= value] …]; public returnType methodeName (parameter list) {} private data type variable [ = value][, variable [= value] …]; private returnType methodeName (Parameter List) {} protected data type variable [ = value][, variable [= value] …]; protected returnType methodeName (Parameter List) {}

slide-31
SLIDE 31

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

31

Classes and Objects

Access Modifier – public

  • can be accessed from any other class

public class Example { public static void main( String[] args) { Counter c = new Counter(); c.count = 42; // count is visible; dot notation can be used int temp = c.getCount(); // method is visible; temp will be 42 } public class Counter { // classes are allowed to construct new instances public int count; // anyone can access this instance variable public Counter(){} public int getCount() { return count; } // other can call to that method }

slide-32
SLIDE 32

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

32

Classes and Objects

Access Modifier – private

  • can only be accessed within the declared class itself

public class Example { public static void main( String[ ] args) { Counter c = new Counter(); c.count = 42; // count is not visible —> ERROR int temp = c.getCount(); // method is not visible —> ERROR } public class Counter { private int count; // only methods of the same class can access this public Counter(){} private int getCount() { return count; } //count is visible }

slide-33
SLIDE 33

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

33

Classes and Objects

Access Modifier – protected

  • can only be accessed within the declared class itself and classes

that are designated as subclasses of the given glass

public class Example { public static void main( String[ ] args) { Counter c = new Counter(); c.count = 42; // count is not visible —> ERROR int temp = c.getCount(); // method is not visible —> ERROR } public class Counter { protected int count; // only methods of the same class can access this public Counter(){} protected int getCount() { return count; } //count is visible }

slide-34
SLIDE 34

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

34

Classes and Objects

Non Access Modifier:

  • static: to create variables and methods that will exist independently of any

instances created for the class.

  • static variables are used to store “global” information about a class
  • final: to finalize the implementations of classes, methods, and variables
  • final variable can never be assigned a new value
  • final reference variable will always refer to the same object
  • final methods cannot be overridden by a subclass
  • final class can not be subclassed

access modifier static data type variable [ = value]; access modifier static returnType methodeName (parameter list) {} public static final int CONSTANT = 254;

slide-35
SLIDE 35

Back to the Algorithm

slide-36
SLIDE 36

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

36

Algorithm: Average

public class Average{ public static double getAverage(int[] data){ int counter = data.length; return (double)linearSum(data, counter)/ (double)counter; } public static int linearSum(int[] data, int n) { if (n == 0) return 0; else return linearSum(data, n-1)+data[n-1]; } public static void main(String[] args) { int[] data = {7,8,5,3,7,7,6,9}; System.out.println(getAverage(data)); } }

slide-37
SLIDE 37

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

37

Algorithm: Class Average

public class Average{ }

slide-38
SLIDE 38

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

38

Algorithm: Class Average

public class Average{ public int[] data; }

slide-39
SLIDE 39

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

39

Algorithm: Class Average

public class Average{ public int[] data; public Average () {} public Average (int[] data) { this.data = data; } }

slide-40
SLIDE 40

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

40

Algorithm: Class Average

public class Average{ public int[] data; public Average () {} public Average (int[] data) { this.data = data; } public double getAverage() { int n = this.data.length; return (double)linearSum(this.data, n)/ (double)n; } }

slide-41
SLIDE 41

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

41

Algorithm: Class Average

public class Average{ public int[] data; public Average () {} public Average (int[] data) { this.data = data; } public double getAverage() { int n = this.data.length; return (double)linearSum(this.data, n)/ (double)n; } public static int linearSum(int[] data, int n) { if (n == 0) return 0; else return linearSum(data, n-1)+data[n-1]; } }

slide-42
SLIDE 42

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

42

Algorithm: Class Test

public class Test{ }

slide-43
SLIDE 43

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

43

Algorithm: Class Test

public class Test{ public static void main( String[ ] args) { } }

slide-44
SLIDE 44

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

44

Algorithm: Class Test

public class Test{ public static void main( String[ ] args) { int[] dataSet1 ={7,8,5,3,7,7,6,9}; Average object1 = new Average(dataSet1); double avg = object1.getAverage(); System.out.println("Average: " + avg); } }

slide-45
SLIDE 45

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

45

Algorithm: Class Test

public class Test{ public static void main( String[ ] args) { int[] dataSet1 ={7,8,5,3,7,7,6,9}; Average object1 = new Average(dataSet1); double avg = object1.getAverage(); System.out.println("Average: " + avg); } }

slide-46
SLIDE 46

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

46

Algorithm: Class Test

public class Test{ public static void main( String[ ] args) { int[] dataSet1 ={7,8,5,3,7,7,6,9}; Average object1 = new Average(dataSet1); double avg = object1.getAverage(); System.out.println("Average: " + avg); } }

slide-47
SLIDE 47

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

47

Algorithm: Class Test

public class Test{ public static void main( String[ ] args) { int[] dataSet1 ={7,8,5,3,7,7,6,9}; Average object1 = new Average(dataSet1); double avg = object1.getAverage(); System.out.println("Average: " + avg); int[] dataSet2 ={5,2,5,6,7,2,4,1}; Average object2 = new Average();

  • bject2.data = dataSet2;

System.out.println("Average: " + object2.getAverage()); } }

slide-48
SLIDE 48

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

48

Algorithm: Class Test

public class Test{ public static void main( String[ ] args) { int[] dataSet1 ={7,8,5,3,7,7,6,9}; Average object1 = new Average(dataSet1); double avg = object1.getAverage(); System.out.println("Average: " + avg); int[] dataSet2 ={5,2,5,6,7,2,4,1}; Average object2 = new Average();

  • bject2.data = dataSet2;

System.out.println("Average: " + object2.getAverage()); } }

slide-49
SLIDE 49

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

49

Algorithm: Class Test

public class Test{ public static void main( String[ ] args) { int[] dataSet1 ={7,8,5,3,7,7,6,9}; Average object1 = new Average(dataSet1); double avg = object1.getAverage(); System.out.println("Average: " + avg); int[] dataSet2 ={5,2,5,6,7,2,4,1}; Average object2 = new Average();

  • bject2.data = dataSet2;

System.out.println("Average: " + object2.getAverage()); int sum = Average.linearSum(dataSet1, 4); } }

slide-50
SLIDE 50

Object-Oriented Design:
 Inheritance

slide-51
SLIDE 51

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

51

Object-Oriented Design: Inheritance

Definition:

  • can be defined as the process where one class acquires the

properties (methods and fields) of another.

  • the information is made manageable in a hierarchical order
slide-52
SLIDE 52

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

52

Object-Oriented Design: Inheritance

Superclass aka base class, parent class

  • the class whose properties are inherited

Subclass aka derived class, child class

  • the class which inherits the properties (fields, methods, and nested

classes) of other.

  • constructors are not inherited by subclasses, but the constructor of the

superclass can be invoked from the subclass

  • the subclass extends the superclass in two ways:
  • to augment existing behavior by adding new fields and new methods
  • to specialize existing behaviors by overriding existing method
slide-53
SLIDE 53

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

53

Object-Oriented Design: Inheritance

Example: Cal (Superclass) Example: My_Cal (Subclass)

public class Cal{ int z; public int add(int x, int y){ return x+y; } } public class My_Cal extends Cal{ //extend class Cal public int mult(int x, int y){ z=x*y; return z; } }

slide-54
SLIDE 54

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

54

Object-Oriented Design: Inheritance

Example: Cal (Superclass) Example: My_Cal (Subclass)

public class Cal{ int z; public int add(int x, int y){ return x+y; } } public class My_Cal extends Cal{ public int mult(int x, int y){ z=x*y; return z; } } mult() int z add()

slide-55
SLIDE 55

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

55

Object-Oriented Design: Inheritance

Polymorphism:

  • the ability of an object to take on many forms.
  • most common use of polymorphism in OOD occurs when a parent class

reference is used to refer to a child class object

  • Liskov Substitution Principle:
  • a variable (or parameter) with a declared type can be assigned an instance from

any direct or indirect subclass of that type

  • Dynamic dispatch:
  • deciding at runtime to call the version of the method that is most specific to the

actual type of the referenced object (not the declared type). superclass object = new subclass();

slide-56
SLIDE 56

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

56

Object-Oriented Design: Inheritance

Example: My_Cal (Subclass)

public class My_Cal extends Cal{ //extend class Cal public int mult(int x, int y){ z=x*y; return z; } } public class Test_My_Cal{ public static void main(String args[]){ int a=20, b=10; My_Cal object = new My_Cal(); //create an object of My_Cal int c = object.add(a,b); int d = object.mult(a,b); } }

slide-57
SLIDE 57

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

57

Object-Oriented Design: Inheritance

Example: My_Cal (Subclass)

public class My_Cal extends Cal{ //extend class Cal public int mult(int x, int y){ z=x*y; return z; } } public class Test_Cal{ public static void main(String args[]){ int a=20, b=10; Cal object = new My_Cal(); //create an object of Cal int c = object.add(a,b); //call method add() int d = object.mult(a,b); //ERROR can not call method } }

slide-58
SLIDE 58

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

58

Object-Oriented Design: Inheritance

Keyword super

  • to differentiate the members of superclass from the members of

subclass, if they have same names

  • to invoke the superclass constructor from subclass

super(values); super.variable super.method();

slide-59
SLIDE 59

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

59

Object-Oriented Design: Inheritance

Example: Event (Superclass) Example: Lecture (Subclass) :

public class Event{ public int num; public Event(int num){ this.num=num; } public void getParticipantNum(){ System.out.println(num) } public class Lecture extends Event{ //extend class Event public Lecture(int num){ super(num); //call constructor of the Superclass super.num--; //call variable of the Superclass super.getParticipantNum(); //call method of the Superclass } }

slide-60
SLIDE 60

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

60

Object-Oriented Design: Inheritance

Access Modifier:

  • control the level of access for classes, variables, methods and constructors
  • public: anyone can access the defined aspect
  • private: anyone of the same class ( not methods of a subclass) can access the defined

aspect

  • protected: anyone of the same package or of its subclasses can access the defined aspect

public data type variable [ = value][, variable [= value] …]; public returnType methodeName (parameter list) {} private data type variable [ = value][, variable [= value] …]; private returnType methodeName (Parameter List) {} protected data type variable [ = value][, variable [= value] …]; protected returnType methodeName (Parameter List) {}

slide-61
SLIDE 61

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

61

Object-Oriented Design: Inheritance

Example: Event (Superclass) Example: Lecture (Subclass) :

public class Event{ private int num; private Event(int num){ this.num=num; //num is visible } private void getParticipantNum(){ System.out.println(num) //num is visible } public class Lecture extends Event{ //extend class Event public Lecture(int num){ super(num); //Error not visible super.num--; //Error not visible super.getParticipantNum(); //Error not visible } }

slide-62
SLIDE 62

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

62

Object-Oriented Design: Inheritance

Example: Event (Superclass) Example: Lecture (Subclass) :

public class Event{ protected int num; protected Event(int num){ this.num=num; //num is visible } protected void getParticipantNum(){ System.out.println(num) //num is visible } public class Lecture extends Event{ //extend class Event public Lecture(int num){ super(num); //call constructor of the Superclass super.num--; //call variable of the Superclass super.getParticipantNum(); //call method of the Superclass } }

slide-63
SLIDE 63

Object-Oriented Design:
 Types of Inheritance

slide-64
SLIDE 64

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

64

Object-Oriented Design: Inheritance

Single Inheritance: Multi Level Inheritance:

public class A{ … } public class B extends A{ … } public class A{ … } public class B extends A{ … } public class C extends B{ … }

Class A Class A Class A Class B Class C

slide-65
SLIDE 65

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

65

Object-Oriented Design: Inheritance

Hierarchical Inheritance: Multiple Inheritance:

public class A{ … } public class B extends A{ … } public class C extends A{ … }

Class A Class B Class C Class C Class A Class B

public class A{ … } public class B{ … } public class C extends A, B { … }

slide-66
SLIDE 66

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

66

Object-Oriented Design: Inheritance

Hierarchical Inheritance: Multiple Inheritance:

public class A{ … } public class B extends A{ … } public class C extends A{ … }

Class A Class B Class C Class C Class A Class B

public class A{ … } public class B{ … } public class C extends A, B { … }

NOT ALLOWED

slide-67
SLIDE 67

Object-Oriented Design:
 Overriding

slide-68
SLIDE 68

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

68

Object-Oriented Design: Overriding

Overriding:

  • the ability to define a behavior that's specific to the subclass type
  • to override the functionality of an existing method
  • Basic rules for method overriding:
  • the parameter list should be exactly the same as that of the overridden method
  • the returnType should be the same or a subtype of the return type declared in

the original overridden method in the superclass

  • the access level of the modifiers cannot be more restrictive than the overridden

method's access level

  • instance methods can be overridden only if they are inherited by the subclass

modifiers returnType methodName (parameter list) {}

slide-69
SLIDE 69

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

69

Object-Oriented Design: Overriding

Method Overriding:

public class Figure { public float area () { return -1.0f; } } class Rectangle extends Figure { //extend class Figure private float w, h; Rectangle (float w, float h) { this.w = w; this.h = h; } public float area () { return w*h; } //overriding method area public float area (float w) { return w*h; } //overloading } class Circle extends Figure { //extend class Figure private float r; Circle ( float r) { this.r = r; } public float area () {return r * r * 3.1415;} //overriding }

slide-70
SLIDE 70

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

70

Object-Oriented Design: Overriding

Method Overriding:

class Rectangle extends Figure { public float area () { return w*h; } . . . class Circle extends Figure { public float area () { return r*r *3.14159265; } . . . Rectangle r = new Rectangle (2 ,3); Circle c = new Circle (1); float a = r.area(); // a = 6 float b = r.area(3); // b = 9 a = c.area(); // a = 3.14159265 Figure f = null; f = r; a = f.area(); // a = 6 f = c; a = f.area(); // a = 3.14159265

slide-71
SLIDE 71

Object-Oriented Design:
 Interfaces

slide-72
SLIDE 72

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

72

Object-Oriented Design: Interfaces

Interface:

  • is a collection of method declarations with no data and no bodies
  • enforces the encapsulation principle
  • nly provide method signatures
  • do not have constructors
  • cannot be directly instantiated
  • keyword: interface, extends
  • a class implements an interface, thereby inheriting the abstract methods of the

interface

  • must implement all of the methods declared in the interface
  • keyword: implements
slide-73
SLIDE 73

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

73

Keyword: interface Keyword: implements

public interface Lecture{ public string getName(); public int getID(); } public class ICSE implements Lecture{ private string name; private int id; private string descript; public ICSE(string n, int i, string desc){ name = n; id = i; descript = desc; } public string getName(){ return name; } public int getID () {return id;} public string getDescription() {return descript} }

Object-Oriented Design: Interfaces

slide-74
SLIDE 74

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

74

Keyword: implements

  • a class can implement several interfaces

Multiple Inheritance for Interfaces:

Interface C Interface A Interface B

public interface A{ … } public interface B{ … } public interface C extends A, B{ … }

Object-Oriented Design: Interfaces

class ICSE implements Lecture, Exercise, Exam { … }

slide-75
SLIDE 75

Object-Oriented Design:
 Abstract Classes

slide-76
SLIDE 76

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

76

Abstract Class

  • serves a role somewhat between that of a traditional class and that of

an interface

  • may define signatures for one or more methods without providing an

implementation of those method - abstract method

  • keyword: abstract
  • no object can be created directly from an abstract class
  • to use an abstract class you have to inherit it from another class,

provide implementations to the abstract methods in it

  • a subclass of an abstract class must provide an implementation to the

abstract methods

Object-Oriented Design: Abstract Classes

slide-77
SLIDE 77

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

77

Keyword: abstract Keyword: extends

public abstract class Lecture { private string name; public Lecture(string name){this.name = name;} public int getName() {return this.name;} public abstract int getID(); } public class ICSE extends Lecture{ private int id; public ICSE (string name, int id) { super(name); this.id=id; } public int getID () {return id;} }

Object-Oriented Design: Abstract Classes

slide-78
SLIDE 78

Object-Oriented Design:
 Generics

slide-79
SLIDE 79

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

79

Object A: ObjectPairA a = new ObjectPairA(“ABC”, 2); Object B: ObjectPairB b = new ObjectPairB(“ABC”, 2.0);

public class ObjectPairA { String first; int second; public ObjectPair(String a, int b) { // constructor first = a; second = b; } }

Object-Oriented Design: Generics

public class ObjectPairB { String first; float second; public ObjectPair(String a, float b) { // constructor first = a; second = b; } }

slide-80
SLIDE 80

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

80

Generics

  • support for writing generic classes and methods that can operate on

a variety of data types while often avoiding the need for explicit casts.

Generics Framework

  • define data types in terms of a set of formal type parameters
  • single-letter uppercase names are conventionally used
  • using actual type parameters to indicate the concrete types
  • a generic type is not defined at compile time but becomes fully

specified at run time

Object-Oriented Design: Generics

slide-81
SLIDE 81

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

81

Generics Methods

  • a single generic method declaration that can be called with arguments of

different types

  • Rules to define Generic Methods:
  • all generic method declarations have a set of formal type parameter delimited by

angle brackets: < >

  • formal type parameters can be used to declare the return type and act as

placeholders for the types of the arguments passed to the generic method, which are known as actual type parameters

  • formal type parameters are separated by commas and can represent only

reference types, not primitive types (like int, double and char) à wrapper classes: Integer, Double and Character

Object-Oriented Design: Generics

modifiers < A > methodName (parameter list) {}

slide-82
SLIDE 82

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

82

Object-Oriented Design: Generics

Method: minFunction(int varA, int varB) Generics Method: minFunction(A varA, A varB)

public static < A > A minFunction(A varA, A varB) { if (varA > varB) return varB; else return varA; } public static int minFunction(int varA, int varB) { } public static void main (String args[]) { Integer a = 3; Integer b = 5; Integer c = minFunction(a, b) }

slide-83
SLIDE 83

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

83

Method: biggerThan(int varA, int varB, string varC) Generics Method: biggerThan(A varA, B varB)

Object-Oriented Design: Generics

public < K,L > void biggerThan(K varA, K varB, L varC) { if (varA > varB) System.out.println(varC); } public static void biggerThan(int varA, int varB, string varC){} public static void main (String args[]) { biggerThan(1, 2, “false”); biggerThan(2.5, 2.0, “true”); }

slide-84
SLIDE 84

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

84

Method: minFunction(int[] varA) Generics Method: minFunction(A[] varA)

Object-Oriented Design: Generics

public static < A > A minFunction(A[] varA) { if (varA[0] >= varA[1]) return varA[0]; else return varA[1]; } public static int minFunction(int[] varA) { } public static void main (String args[]) { Integer[] a = {3,5}; int c = minFunction(a) }

slide-85
SLIDE 85

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

85

Generic Class

  • A generic class declaration looks like a non-generic class

declaration, except that the class name is followed by a type parameter section

Object-Oriented Design: Generics

modifiers class className < A > {} public class ObjectPair < D,A >{ private D first; private A second; public ObjectPair(D a, A b) { // constructor first = a; second = b; } public D Getfirst() { return first; } }

slide-86
SLIDE 86

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

86

Summary

Object-Oriented Design

  • Goals, Principles and Pattern
  • Inheritance
  • Overloading vs. Overriding
  • Types of Inheritance
  • Interfaces and Abstract Classes
  • Generics
slide-87
SLIDE 87

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

87

Recommended Reading

Chapter 2: Object-Oriented Design

  • Goals, Principles and Patterns
  • Inheritance
  • Interfaces and Abstract Classes
  • Exceptions
  • Casting and Generics
  • Nested Classes

Data Structures & Algorithms in Java

by Michael T. Goodrich, 
 Roberto Tamassia and 
 Michael H. Goldwasser

slide-88
SLIDE 88

ICSE: Introduction to Computer Sciences for Engineers – L-3 (WS16/17)

ICSE

88

Coming up…

Fundamental Algorithms

  • Search: Finding a Target Value
  • Linear Search
  • Binary Search
  • Sort: Sorting an Array of Integers
  • Selection Sort
  • Insertion Sort
  • Quicksort
  • Mergesort