COP 3503 FALL 2012: Shayan Javed
COP 3503 FALL 2012: Shayan Javed
Inheritance
Derive new classes (subclass) from existing ones
(superclass).
Inheritance
Code reuse – methods and properties.
Inheritance
Can only inherit from one class.
Example
Let’s say you create the following classes:
Circle
Rectangle
Triangle
etc.
Example
public class GeometricObject {
private String Color;
private String name;
private float area;
// constructors...
public GeometricObject(String color, String name) {
…
}
Example
public class Circle extends GeometricObject {
private double radius; // specific property
Example
An error in the constructor!
public Circle(double radius, String color, String name) {
this.radius = radius;
this.color = color;
this.name = name;
}
Example
They are private properties.
Example
public class Rectangle extends GeometricObject {
private double width, height; // specific properties
super.method(parameters)...
Optional
20 / 71
Private
public class GeometricObject {
private String Color;
private String name;
private float area;
// constructors...
}
23 / 71
// constructors...
}
24 / 71
Polymorphism
Important aspect of OOP
26 / 71
Polymorphism
Important aspect of OOP
Polymorphism
Important aspect of OOP
Polymorphism
1. Overriding Methods
2. Overloaded methods
Overriding Methods
Sometimes need to overwrite methods written in
the superclass
Overriding Methods
The Object class and toString()
Overriding Methods
The Object class and toString()
System.out.println(circle1);
Output:
?
34 / 71
System.out.println(circle1);
Output:
Circle@19821f
35 / 71
System.out.println(circle1);
Output:
Compares references
39 / 71
circle1.equals(circle2); // ?
41 / 71
circle1.equals(circle3); // ?
43 / 71
?
48 / 71
?
49 / 71
returns true!
50 / 71
returns true!
51 / 71
String s = null;
Object casting
So we can determine
if (SuperClassObj instanceof subclass)
Object casting
So we can determine
if (SuperClassObj instanceof subclass)
Object casting
Once again...
Object casting
Once again...
Overloading Methods
Looked at it before.
Dynamic binding
Object type is determined at run-time
Dynamic binding
public class GeometricObject {
protected String Color;
protected String name;
protected float area;
}
62 / 71
Dynamic binding
// Circle
public class Circle {
public float getArea() {
return Math.PI * radius * radius;
}
}
// Rectangle
public class Rectangle {
Dynamic binding
GeometricObject[] objs = new GeometricObject[2];
64 / 71
Dynamic binding
GeometricObject[] objs = new GeometricObject[2];
objs[0] = new Circle(3);
objs[1] = new Rectangle(1, 2);
65 / 71
Dynamic binding
GeometricObject[] objs = new GeometricObject[2];
objs[0] = new Circle(3);
objs[1] = new Rectangle(1, 2);
Dynamic binding
GeometricObject[] objs = new GeometricObject[2];
objs[0] = new Circle(3);
objs[1] = new Rectangle(1, 2);
Dynamic binding
Output:
9.0
2.0
final Classes
Sometimes classes shouldn’t be allowed to be
extended.
final Methods
Methods which can’t be overridden in the
subclasses.
70 / 71
Summary...
Inheritance
super
protected
Polymorphism
Overloading
Overwriting
Dynamic binding
instanceof
Object casting
final classes and methods
71 / 71
Next lecture...
Abstract Classes
Interfaces