Lecture Notes Chapter #10 Inheritance & Polymorphism
Lecture Notes Chapter #10 Inheritance & Polymorphism
Chapter #10
Inheritance & Polymorphism
GeometricObject1
GeometricObject ` Superclass
-color: String (object) Parent Class
-fllled: boolean (color) Base Class
-dateCreated: java.util.Date
+GeometricObject( )
+getColor( ): String
+setColor(color: String ): void
+isFilled( ): boolean
+setFilled(filled: boolean): void
void
+getDateCreated( ): java.util.Date
+toString( ): String
Subclass
Subclasses Child Class
derived from Derived Class
extended from Extended Class
Superclass
Circle4 Rectangle1
-radius: double -width: double
-height: double
+Circle( )
+Circle(radius: double) +Rectangle( )
+getRadius( ): double +Rectangle(width: double, height: double)
+setRadius(radius: double): void +getWidth( ): double
+getArea( ): double +setWidth(width: double): void
+getParimeter( ): double +getHeight( ): double
+getDiameter( ): double +setHeight(height: double): void
+printCircle( ): void +getArea( ): double
+getPerimeter( ): double
A child class inherits all accessible data fields and methods from its parent class!
A child class does not inherit the constructors of the parent class!
The child class may also add uniquely new data fields and methods!
1. Implementation
a. GeometricObject1.java
b. Circle4.java
public Circle4( ) { }
public Circle4(double radius ) { this.radius = radius; }
public Rectangle1( ) { }
public Rectangle1(double width, double height )
{
this width = width;
this height = height;
}
d. TestCircleRectangle.java
2. Constructor Chaining
A child class inherits all accessible data fields and methods from its parent
class, BUT the child class does not inherit the constructors of the parent
class!
public Faculty( )
{
System.out.println(“(4) Faculty no-arg constructor invoked”);
}
}
class Person
{
public Person( )
{
System.out.println(“(1) Person’s no-arg constructor invoked”);
}
}
Construction of the
Faculty Object
private data fields in a superclass are not accessible outside of that class,
hence they cannot be used directly by a subclass; they can be accessed &/or
mutated by public accessor &/or mutators defined in the superclass
} }
4. Object Class & Methods
Comparison Operators/Methods
o “==” operator is used to compare primitive data type values
o “==” operator is also used to compare whether two reference
variables refer to the same object (where arrays may be considered
to be objects)
o The modified “equals( )” method can be used to determine whether
two objects have the same contents
if the object o were to invoke a method, i.e., o.p( ); then the JVM searches
for the method p( ) in the classes in the order C1, C2, C3, C4, java.lang.Object
The call for the execution of the method m(new GraduateStudent( ));
results in a the invocation of the toString( ) method; the JVM starts a search of the
inheritance chain starting with the GraduateStudent class for an implementation of the
toString( ) method.
The Student class yields such an implementation which results in the output of the
string “Student”.
The call for the execution of the method m(new Student( )); results in the invocation
of its toString( ) method and the output of the second string “Student”.
The call for the execution of the method m(new Person( )); results in the invocation of
its toString( ) method and the output of the string “Person”.
The call for the execution of the method m(new Object( )); results in the invocation of
the java.lang.Object’s toString( ) method and the output of a string similar to
“java.lang.object@AD23F5”.
a. Implicit Casting
Object o = new Student( ); equivalent m( new Student( ));
m( o ); statements
b. Explicit Casting
Student b = o; compilation error !
An instance of Object is not necessarily an instance of Student
Student b = (Student) o;
c. Up Casting
Casting an instance of a subclass to a variable of a superclass is always
possible; implicit casting may be used.
d. Down Casting
Casting an instance of a superclass to a variable of a subclass: must
use explicit casting & object cast must be an instance of the subclass
e. instanceof Operator
Object o = new Circle( );
if( o instanceof Circle )
{
double d = ((Circle) o ).getDiameter( ));
}
The declared type determines which method to match at compile time;
“o.getDiameter( );” would cause a compile error since Object does not contain a
“getDiameter( )” method.
public int Search( Object o ) Returns the index of the first-matching element
{ in the stack by invoking the list.lastIndexOf( o )
return list.lastIndexOf(o); method since the top of the stack is the last
} element in the list; i.e., the end of the list is the
top of the stack.
Visibility Increases
Private Modifier
Hide members so that they cannot be accessed outside of the class
i.e., the members are not intended for use outside of the class
Used only for members of the class
No Modifier
Allow members of the class to be accessed directly from any class within
the same package but not from other packages
Can be used on the class as well as the members of the class
Protected Modifier
Enable members to be accessed by the subclasses in any package or
classes in the same package, i.e., members of the class are intended for
extenders of the class but not for users of the class
Used only for members of the class
Public Modifier
Enable members of the class to be accessed by any class, i.e., members of
the class are intended for users of the class
Can be used on the class as well as the members of the class
A subclass may override a method from a superclass and increase its visibility in
the subclass; but it may not restrict the methods visibility, e.g., if a method is
defined to be public in the superclass, it cannot be changed to protected, none
(default) nor private in the subclass!
Preventing Extending & Overriding