COMP200 INHERITANCE OOP using Java, from slides by Shayan Javed 2 - - PowerPoint PPT Presentation

comp200
SMART_READER_LITE
LIVE PREVIEW

COMP200 INHERITANCE OOP using Java, from slides by Shayan Javed 2 - - PowerPoint PPT Presentation

1 1 COMP200 INHERITANCE OOP using Java, from slides by Shayan Javed 2 Inheritance Derive new classes ( subclass ) from existing ones ( superclass ). Only the Object class (java.lang) has no superclass Every class =


slide-1
SLIDE 1

1

COMP200


INHERITANCE

OOP using Java, from slides by Shayan Javed

1

slide-2
SLIDE 2

2

Inheritance

Derive new classes (subclass) from existing

  • nes (superclass).

Only the Object class (java.lang) has no

superclass

Every class = subclass of Object.

slide-3
SLIDE 3

3

Inheritance

Code reuse – methods and properties. Use classes to model objects of the same type Define general class

Then define specialised classes.

slide-4
SLIDE 4

4

Inheritance

Can only inherit from one class. Can inherit from multiple classes in other

languages (C++)

slide-5
SLIDE 5

5

Example

Let’s say you create the following classes:

Circle Rectangle Triangle etc.

Share certain properties, but also specialised

properties

slide-6
SLIDE 6

6

Example

public class GeometricObject { private String Color; private String name; private float area; // constructors... public GeometricObject(String color, String name) { … } // get/set methods, etc. }

slide-7
SLIDE 7

7

Example

public class Circle extends GeometricObject { private double radius; // specific property public Circle(double radius, String color, String name) { this.radius = radius; this.color = color; this.name = name; } // get/set methods, etc. public double getArea() { return radius * radius * Math.PI; } }

slide-8
SLIDE 8

8

Example

An error in the constructor!

public Circle(double radius, String color, String name) { this.radius = radius; this.color = color; this.name = name; }

Why can’t we use “this.color” and

“this.name”?

slide-9
SLIDE 9

9

Example

They are private properties. NOT accessible by subclasses. Should use setColor() instead. public methods/properties inherited.

slide-10
SLIDE 10

10

Example

public class Rectangle extends GeometricObject { private double width, height; // specific properties public Rectangle(double w, double h, String color, String name) { this.height = h; this.width = w; setColor(color); setName(name); } // get/set methods, etc. public double getArea() { return width * height; } }

slide-11
SLIDE 11

11

The super keyword

Are constructors inherited?

  • Yes. super used for:

calling a super class constructor calling a super class method

slide-12
SLIDE 12

12

The super keyword

Constructors: super() calls the default constructor super(arguments) calls the constructors

according to the arguments

slide-13
SLIDE 13

13

The super keyword

Constructors:

public Circle(double radius, String color, String name) { super(color, name); this.radius = radius; }

slide-14
SLIDE 14

14

The super keyword

Constructors:

public Circle(double radius, String color, String name) { super(color, name); this.radius = radius; } public GeometricObject(String color, String name) { this.color = color; this.name = name; }

slide-15
SLIDE 15

15

The super keyword

What if we don’t add the super(..) constructor call?

slide-16
SLIDE 16

16

The super keyword

public Circle(double radius, String color, String name) { this.radius = radius; }

slide-17
SLIDE 17

17

The super keyword

public Circle(double radius, String color, String name) { this.radius = radius; } Is the same as: public Circle(double radius, String color, String name) { super(); this.radius = radius; }

slide-18
SLIDE 18

18

The super keyword

Called Constructor Chaining

slide-19
SLIDE 19

19

The super keyword

Calling the super class methods super.method(parameters)... Optional

slide-20
SLIDE 20

20

Protected fields

Using get/set methods to access properties of

the superclass is cumbersome…

slide-21
SLIDE 21

21

The protected keyword

Often want subclasses to directly access

properties/methods

Use protected instead of private/public Subclasses can now directly access

slide-22
SLIDE 22

22

Private

public class GeometricObject { private String Color; private String name; private float area; // constructors... // get/set methods, etc. }

slide-23
SLIDE 23

23

The protected keyword

public class GeometricObject { protected String Color; protected String name; protected float area; // constructors... // get/set methods, etc. }

slide-24
SLIDE 24

24

The protected keyword

public class Circle extends GeometricObject { private double radius; // specific property public Circle(double radius, String color, String name) { this.radius = radius; this.color = color; this.name = name; } }

Now works!

slide-25
SLIDE 25

25

Polymorphism

Important aspect of OOP

slide-26
SLIDE 26

26

Polymorphism

Important aspect of OOP “capability of an action or method to do

different things based on the object that it is acting upon. ”

slide-27
SLIDE 27

27

Polymorphism

Important aspect of OOP “capability of an action or method to do different

things based on the object that it is acting upon. ”

“characteristic of being able to assign a different

meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.”

slide-28
SLIDE 28

28

Polymorphism

1.

Overriding Methods

2.

Overloaded methods

3.

Dynamic (late) method binding

slide-29
SLIDE 29

29

Overriding Methods

Sometimes need to overwrite methods written

in the superclass

Re-define the method in the subclass.

slide-30
SLIDE 30

30

Overriding Methods

The Object class and toString() toString() = String output when you print the

  • bject
slide-31
SLIDE 31

31

Overriding Methods

The Object class and toString() toString() = String output when you print the

  • bject

Object class has default toString() method

slide-32
SLIDE 32

32

The toString() method

public String toString() { String str; ... return str; }

slide-33
SLIDE 33

33

The toString() method

Circle circle1 = new Circle(“Circle1”, “Red”, 3.5); System.out.println(circle1);

Output:

?

slide-34
SLIDE 34

34

The toString() method

Circle circle1 = new Circle(“Circle1”, “Red”, 3.5); System.out.println(circle1);

Output:

Circle@19821f

slide-35
SLIDE 35

35

The toString() method

So override the method

public String toString() { String str = “”; str += “Circle: ” + getName() + “,”; str += “Color: ” + getColor() + “,”; str += “Radius: ” + getRadius(); return str; }

slide-36
SLIDE 36

36

The toString() method

Circle circle1 = new Circle(“Circle1”, “Red”, 3.5); System.out.println(circle1);

Output:

Circle: Circle1,Color: Red,Radius: 3.5

slide-37
SLIDE 37

37

The equals() method

The Object class and equals() equals (Object obj) = returns whether the

  • bject passed in and this one are the same
slide-38
SLIDE 38

38

The equals() method

Default implementation:

public boolean equals(Object obj) { return (this == obj); } Compares references

slide-39
SLIDE 39

39

The equals() method

Circle circle1 = new Circle(3.5); Circle circle2 = new Circle(3.5);

slide-40
SLIDE 40

40

The equals() method

Circle circle1 = new Circle(3.5); Circle circle2 = new Circle(3.5);

circle1.equals(circle2); // ?

slide-41
SLIDE 41

41

The equals() method

Circle circle1 = new Circle(3.5); Circle circle2 = new Circle(3.5);

circle1.equals(circle2); // returns

false!

slide-42
SLIDE 42

42

The equals() method

Circle circle1 = new Circle(3.5); Circle circle2 = new Circle(3.5);

circle1.equals(circle2); // returns

false!

Circle circle3 = circle1; circle1.equals(circle3);

// ?

slide-43
SLIDE 43

43

The equals() method

Circle circle1 = new Circle(3.5); Circle circle2 = new Circle(3.5);

circle1.equals(circle2); // returns

false!

Circle circle3 = circle1; circle1.equals(circle3);

// returns true!

slide-44
SLIDE 44

44

The equals() method

Override it

public boolean equals(Object obj) { if (obj instanceof Circle) return this.radius == ((Circle)obj).getRadius(); else return false; }

slide-45
SLIDE 45

45

The instanceof operator

Test if object is of a specified type

if (object instanceof Class)

slide-46
SLIDE 46

46

The instanceof operator

Test if object is of a specified type

if (object instanceof Class)

Example:

Circle circle1 = new Circle(3.4); if (circle1 instanceof Circle) { … }

slide-47
SLIDE 47

47

The instanceof operator

Testing with the SuperClass:

if (subclassObj instanceof SuperClass) { … }

?

slide-48
SLIDE 48

48

The instanceof operator

Testing with the SuperClass:

if (Circle instanceof GeometricObject) { … }

?

slide-49
SLIDE 49

49

The instanceof operator

Testing with the SuperClass:

if (subclassObj instanceof SuperClass) { … }

returns true!

slide-50
SLIDE 50

50

The instanceof operator

Example:

Circle circle1 = new Circle(3.4);

if (circle1 instanceof GeometricObject) { … }

returns true!

slide-51
SLIDE 51

51

The instanceof operator

What if we write:

if (SuperClassObj instanceof subclass) { … }

slide-52
SLIDE 52

52

The instanceof operator

if (SuperClassObj instanceof subclass) { … }

returns true as long as the object is an instance

  • f the subclass
slide-53
SLIDE 53

53

The instanceof operator

Example:

Object is a superclass of Circle

public boolean equals(Object obj) { if (obj instanceof Circle) return this.radius == ((Circle)obj).getRadius(); else return false; }

slide-54
SLIDE 54

54

The instanceof operator

Be careful with null objects

String s = null; s instanceof String = false

slide-55
SLIDE 55

55

Object casting

So we can determine

if (SuperClassObj instanceof subclass)

Convert it to subclass by casting

slide-56
SLIDE 56

56

Object casting

So we can determine

if (SuperClassObj instanceof subclass)

Convert it to subclass by casting Can now access members/properties

slide-57
SLIDE 57

57

Object casting

Once again...

public boolean equals(Object obj) { if (obj instanceof Circle) { return this.radius == ((Circle)obj).getRadius(); } else return false; }

slide-58
SLIDE 58

58

Object casting

Once again...

public boolean equals(Object obj) { if (obj instanceof Circle) { Circle circle = (Circle)obj; // casting return this.radius == circle.getRadius(); } else return false; }

slide-59
SLIDE 59

59

Overloading Methods

Looked at it before. Can have multiple methods with the same

name.

But different parameters, return type

slide-60
SLIDE 60

60

Dynamic binding

Object type is determined at run-time ability of a program to resolve references to

subclass methods at runtime.

slide-61
SLIDE 61

61

Dynamic binding

public class GeometricObject { protected String Color; protected String name; protected float area; public float getArea() { return -1; } }

slide-62
SLIDE 62

62

Dynamic binding

// Circle public class Circle { public float getArea() { return Math.PI * radius * radius; } } // Rectangle public class Rectangle { public float getArea() { return width * height; } }

slide-63
SLIDE 63

63

Dynamic binding

GeometricObject[] objs = new GeometricObject[2];

slide-64
SLIDE 64

64

Dynamic binding

GeometricObject[] objs = new GeometricObject[2];

  • bjs[0] = new Circle(3);
  • bjs[1] = new Rectangle(1, 2);
slide-65
SLIDE 65

65

Dynamic binding

GeometricObject[] objs = new GeometricObject[2];

  • bjs[0] = new Circle(3);
  • bjs[1] = new Rectangle(1, 2);

for (GeometricObject i : objs) { System.out.println(i.getArea()); }

slide-66
SLIDE 66

66

Dynamic binding

GeometricObject[] objs = new GeometricObject[2];

  • bjs[0] = new Circle(3);
  • bjs[1] = new Rectangle(1, 2);

for (GeometricObject i : objs) { System.out.println(i.getArea()); }

What’s the output?

slide-67
SLIDE 67

67

Dynamic binding

Output: 9.0 2.0

The subclass getArea() methods are called. At run-time, Java dynamically determines the class.

slide-68
SLIDE 68

68

final Classes

Sometimes classes shouldn’t be allowed to be

extended.

The String class for example. Use the keyword

“final” final class String {

... }

slide-69
SLIDE 69

69

final Methods

Methods which can’t be overridden in the

subclasses.

slide-70
SLIDE 70

70

Summary...

Inheritance

super protected

Polymorphism

Overloading Overwriting Dynamic binding

instanceof Object casting final classes and methods

slide-71
SLIDE 71

71

Next lecture...

Abstract Classes Interfaces