11 InheritanceandPolymorphism PDF
11 InheritanceandPolymorphism PDF
11 InheritanceandPolymorphism PDF
Inheritance and
Polymorphism
Motivations
Suppose you will define classes to
model circles, rectangles, and triangles.
These classes have many common
features.
What is the best way to design these
classes so to avoid redundancy?
The answer is to use inheritance.
2
1
10/30/2015
Circle Rectangle
-radius: double -width: double
+Circle() -height: double
+Circle(radius: double) +Rectangle()
+Circle(radius: double, color: String, +Rectangle(width: double, height: double)
filled: boolean) +Rectangle(width: double, height: double
+getRadius(): double color: String, filled: boolean)
+setRadius(radius: double): void +getWidth(): double
+getArea(): double +setWidth(width: double): void
+getPerimeter(): double +getHeight(): double
+getDiameter(): double +setHeight(height: double): void
+printCircle(): void +getArea(): double
+getPerimeter(): double 3
Superclass
Subclass
2
10/30/2015
3
10/30/2015
Caution
You must use the keyword super to
call the superclass constructor.
Invoking a superclass constructor’s name
in a subclass causes a syntax error.
Java requires that the statement that
uses the keyword super appear first in
the constructor.
8
4
10/30/2015
Constructor Chaining
Constructing an instance of a class invokes all the superclasses’ constructors
along the inheritance chain. This is called constructor chaining.
public class Faculty extends Employee {
public static void main(String[] args) {
Faculty f = new Faculty();
}
public Faculty() {
Super(); System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
class Person {
public Person() {
Super(); System.out.println("(1) Person's no-arg constructor is invoked");
}
} 9
5
10/30/2015
Defining a Subclass
A subclass inherits from a superclass.
You can also:
Add new properties.
Add new methods.
Override the methods of the
superclass.
11
12
6
10/30/2015
Circle Rectangle
-radius: double -width: double
+Circle() -height: double
+Circle(radius: double) +Rectangle()
+Circle(radius: double, color: String, +Rectangle(width: double, height: double)
filled: boolean) +Rectangle(width: double, height: double
+getRadius(): double color: String, filled: boolean)
+setRadius(radius: double): void +getWidth(): double
+getArea(): double +setWidth(width: double): void
+getPerimeter(): double +getHeight(): double
+getDiameter(): double +setHeight(height: double): void
+printCircle(): void +getArea(): double
+getPerimeter(): double 13
7
10/30/2015
Note
An instance method can be
overridden only if it is accessible.
Thus a private method cannot be
overridden, because it is not accessible
outside its own class.
If a method defined in a subclass is
private in its superclass, the two methods
are completely unrelated.
15
Note cont.
Like an instance method, a static method
can be inherited.
However, a static method cannot be
overridden.
If a static method defined in the
superclass is redefined in a subclass, the
method defined in the superclass is
hidden.
16
8
10/30/2015
class B {
public void p(double i) {
System.out.println(i * 2);
}
}
class A extends B {
// This method overrides the method in B
public void p(double i) {
System.out.println(i);
}
} 17
class B {
public void p(double i) {
System.out.println(i * 2);
}
}
class A extends B {
// This method overloads the method in B
public void p(int i) {
System.out.println(i);
}
} 18
9
10/30/2015
19
10
10/30/2015
11
10/30/2015
Polymorphism
public class Demo {
public static void main(String[] a) { Method m takes a
m(new Object());
m(new Person());
parameter of the
m(new Student());
m(new GraduateStudent());
Object type.
}
public static void m(Object x){
You can invoke it with
System.out.println(x.toString()); any object.
}
}
Dynamic Binding
public class Demo {
public static void main(String[] a) {
m(new GraduateStudent());
m(new Student());
m(new Person()); This capability is known as
m(new Object());
}
public static void m(Object x) {
dynamic binding.
toString()
System.out.println(x.toString ());
}
}
12
10/30/2015
Dynamic Binding
Dynamic binding works as follows:
Suppose an object o is an instance of
classes C1, C2, ..., Cn-1, and Cn, where C1 is a
subclass of C2, C2 is a subclass of C3, ..., and
Cn-1 is a subclass of Cn.
That is, Cn is the most general class, and
C1 is the most specific class.
Cn C n-1 ..... C2 C1
13
10/30/2015
Generic Programming
public class Demo {
public static void main(String[] a) { Polymorphism allows methods
m(new GraduateStudent());
m(new Student()); to be used generically for a wide
m(new Person()); range of object arguments.
m(new Object());
} This is known as:
public static void m(Object x){
}
System.out.println(x.toString()); generic programming
}
Casting Objects
Casting can also be used to convert an object of one
class type to another within an inheritance hierarchy.
m( new Student() );
assigns the object new Student() to a parameter of the
Object type. This statement is equivalent to:
Object o = new Student(); // Implicit casting
m( o );
14
10/30/2015
15
10/30/2015
31
16
10/30/2015
Note
The == comparison operator is used for
comparing two primitive data type values
or for determining whether two objects
have the same references.
The equals method is intended to test
whether two objects have the same
contents, provided that the method is
modified in the defining class of the objects.
34
17
10/30/2015
35
18
10/30/2015
38
19
10/30/2015
Shuffling an ArrayList
Integer[] array = {3, 5, 95, 4, 15, 34, 3, 6, 5};
ArrayList<Integer> list = new
ArrayList<>(Arrays.asList(array));
java.util.Collections.shuffle(list);
System.out.println(list); 40
20
10/30/2015
41
Accessibility Summary
42
21
10/30/2015
Visibility Modifiers
43
22
10/30/2015
Note
The modifiers are used on classes
and class members (data and
methods), except that the final modifier
can also be used on local variables in a
method.
A final local variable is a constant
inside a method.
46
23