CH 04 OOP Inheritance
CH 04 OOP Inheritance
10636212
Chapter 04:
- Inheritance -
Inheritance (cont.)
• Class hierarchy
– Direct superclass
• Inherited explicitly (one level up hierarchy)
– Indirect superclass
• Inherited two or more levels up hierarchy
– Single inheritance
• Inherits from one superclass
– Multiple inheritance
• Inherits from multiple superclasses
– Java does not support multiple inheritance
CommunityMember
Faculty Staff
Administrator Teacher
Inheritance Hierarchy
Shape
TwoDimensionalShape ThreeDimensionalShape
• You can use the inherited members as is, replace them, hide them,
or supplement them with new members.
• The inherited fields can be used directly, just like any other fields.
• You can declare a field in the subclass with the same name as the
one in the superclass, thus hiding it (not recommended).
• You can declare new fields in the subclass that are not in the
superclass.
• You can write a new instance method in the subclass that has the
same signature as the one in the superclass, thus overriding it.
• You can write a new static method in the subclass that has the
same signature as the one in the superclass, thus hiding it.
• You can declare new methods in the subclass that are not in the
superclass.
// no-argument constructor
public Circle()
{
// implicit call to superclass constructor here
radius = 0;
System.out.println( "Circle constructor: " + this );
}
// Constructor
public Circle( double circleRadius, int xCoordinate, int yCoordinate
)
{
// call superclass constructor
super( xCoordinate, yCoordinate );
radius = circleRadius;
System.out.println( "Circle constructor: " + this);
}
}
} // end class Circle
wait()
Causes the current thread to wait until another thread invokes the notify() method
or the notifyAll() method for this object.
wait(long timeout)
Causes the current thread to wait until either another thread invokes
the notify() method or the notifyAll() method for this object, or a specified amount of
time has elapsed.
wait(long timeout, int nanos)
Causes the current thread to wait until another thread invokes the notify() method
or the notifyAll() method for this object, or some other thread interrupts the current
thread, or a certain amount of real time has elapsed.
// Constructor
public Person(String name, String address) {
this.name = name;
this.address = address;
}
// Getters
public String getName() {
return name;
}
public String getAddress() {
return address;
}
@Override
public String toString() {
return "Student: " + super.toString();
}
Annotation - @
• Annotation:
– a form of metadata (provide data about a program that is
not part of the program itself.)
– have no direct effect on the operation of the code they
annotate.
– became available in JDK 1.5
@override annotation
• @override: Checks that the method is an override.
– Informs the compiler that the element is meant to
override an element declared in a superclass
@Override
public String toSTring() { //create a compile error
class Engine {
..
}
class Vehicle {
..
}
}
© 2020 Dr. Ashraf Armoush , An-Najah National University 34
Inheritance vs. Composition (cont.)
• Which works best, composition or inheritance
in each of the following situations?
– Library/Book/chapter
– Department/Employee
– Circle/Point ???
© 2020 Dr. Ashraf Armoush , An-Najah National University 35