1
COMP200
INHERITANCE
OOP using Java, from slides by Shayan Javed
1
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 =
1
1
2
Derive new classes (subclass) from existing
Only the Object class (java.lang) has no
Every class = subclass of Object.
3
Code reuse – methods and properties. Use classes to model objects of the same type Define general class
Then define specialised classes.
4
Can only inherit from one class. Can inherit from multiple classes in other
5
Let’s say you create the following classes:
Circle Rectangle Triangle etc.
Share certain properties, but also specialised
6
public class GeometricObject { private String Color; private String name; private float area; // constructors... public GeometricObject(String color, String name) { … } // get/set methods, etc. }
7
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; } }
8
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
9
They are private properties. NOT accessible by subclasses. Should use setColor() instead. public methods/properties inherited.
10
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; } }
11
Are constructors inherited?
calling a super class constructor calling a super class method
12
Constructors: super() calls the default constructor super(arguments) calls the constructors
13
Constructors:
public Circle(double radius, String color, String name) { super(color, name); this.radius = radius; }
14
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; }
15
16
public Circle(double radius, String color, String name) { this.radius = radius; }
17
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; }
18
19
Calling the super class methods super.method(parameters)... Optional
20
Using get/set methods to access properties of
21
Often want subclasses to directly access
Use protected instead of private/public Subclasses can now directly access
22
public class GeometricObject { private String Color; private String name; private float area; // constructors... // get/set methods, etc. }
23
public class GeometricObject { protected String Color; protected String name; protected float area; // constructors... // get/set methods, etc. }
24
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; } }
25
Important aspect of OOP
26
Important aspect of OOP “capability of an action or method to do
27
Important aspect of OOP “capability of an action or method to do different
“characteristic of being able to assign a different
28
1.
2.
3.
29
Sometimes need to overwrite methods written
Re-define the method in the subclass.
30
The Object class and toString() toString() = String output when you print the
31
The Object class and toString() toString() = String output when you print the
Object class has default toString() method
32
33
Circle circle1 = new Circle(“Circle1”, “Red”, 3.5); System.out.println(circle1);
34
Circle circle1 = new Circle(“Circle1”, “Red”, 3.5); System.out.println(circle1);
35
So override the method
36
Circle circle1 = new Circle(“Circle1”, “Red”, 3.5); System.out.println(circle1);
37
The Object class and equals() equals (Object obj) = returns whether the
38
Default implementation:
39
40
41
false!
42
false!
// ?
43
false!
// returns true!
44
Override it
public boolean equals(Object obj) { if (obj instanceof Circle) return this.radius == ((Circle)obj).getRadius(); else return false; }
45
Test if object is of a specified type
46
Test if object is of a specified type
Example:
47
Testing with the SuperClass:
48
Testing with the SuperClass:
49
Testing with the SuperClass:
50
Example:
51
52
returns true as long as the object is an instance
53
Example:
public boolean equals(Object obj) { if (obj instanceof Circle) return this.radius == ((Circle)obj).getRadius(); else return false; }
54
Be careful with null objects
55
So we can determine
Convert it to subclass by casting
56
So we can determine
Convert it to subclass by casting Can now access members/properties
57
public boolean equals(Object obj) { if (obj instanceof Circle) { return this.radius == ((Circle)obj).getRadius(); } else return false; }
58
public boolean equals(Object obj) { if (obj instanceof Circle) { Circle circle = (Circle)obj; // casting return this.radius == circle.getRadius(); } else return false; }
59
Looked at it before. Can have multiple methods with the same
But different parameters, return type
60
Object type is determined at run-time ability of a program to resolve references to
61
public class GeometricObject { protected String Color; protected String name; protected float area; public float getArea() { return -1; } }
62
// Circle public class Circle { public float getArea() { return Math.PI * radius * radius; } } // Rectangle public class Rectangle { public float getArea() { return width * height; } }
63
GeometricObject[] objs = new GeometricObject[2];
64
GeometricObject[] objs = new GeometricObject[2];
65
GeometricObject[] objs = new GeometricObject[2];
for (GeometricObject i : objs) { System.out.println(i.getArea()); }
66
GeometricObject[] objs = new GeometricObject[2];
for (GeometricObject i : objs) { System.out.println(i.getArea()); }
67
Output: 9.0 2.0
68
Sometimes classes shouldn’t be allowed to be
The String class for example. Use the keyword
69
Methods which can’t be overridden in the
70
Inheritance
super protected
Polymorphism
Overloading Overwriting Dynamic binding
instanceof Object casting final classes and methods
71
Abstract Classes Interfaces