Rest_of_module2(Multilevel_inheritance to using final in inheritance)
Rest_of_module2(Multilevel_inheritance to using final in inheritance)
Hierarchies can be built to contain as many layers as a user wants. Given three classes called
A, B, and C, C can be a subclass of B, which is a subclass of A. When this type of situation
occurs, each subclass inherits all of the traits found in all of its superclasses. In this case, C
inherits all aspects of B and A.
In the following example, a new subclass Shipment is created along with BoxWeight.
Shipment inherits all of the traits of BoxWeight and Box, and adds a field called cost, which
holds the cost of shipping.
class Box {
private double width;
private double height;
private double depth;
vol = shipment2.volume();
System.out.println("Volume of shipment2 is " + vol);
System.out.println("Weight of shipment2 is "
+ shipment2.weight);
System.out.println("Shipping cost: $" + shipment2.cost);
}
}
Because of inheritance, Shipment can make use of the previously defined classes of Box and
BoxWeight, adding only the extra information it needs for its own, specific application. This
is part of the value of inheritance; it allows the reuse of code. This example illustrates one other
important point: super( ) always refers to the constructor in the closest superclass. The super(
) in Shipment calls the constructor in BoxWeight. The super( ) in BoxWeight calls the
constructor in Box. In a class hierarchy, if a superclass constructor requires parameters, then
all subclasses must pass those parameters “up the line.”
Method Overriding
In a class hierarchy, when a method in a subclass has the same name and type signature as a
method in its superclass, then the method in the subclass is said to override the method in the
superclass. When an overridden method is called from within a subclass, it will always refer to
the version of that method defined by the subclass. The version of the method defined by the
superclass will be hidden.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
System.out.println("k: " + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}
Output
k=3
When show( ) is invoked on an object of type B, the version of show( ) defined within B
is used. That is, the version of show( ) inside B overrides the version declared in A. If the
user wishes to access the superclass version of an overridden method, it can be done by
using super.
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
super.show(); // this calls A's show()
System.out.println("k: " + k);
}
}
In this case the output would be
i and j: 1 2
k: 3
The following program creates a superclass called Figure that stores the dimensions of a two-
dimensional object. It also defines a method called area( ) that computes the area of an object.
The program derives two subclasses from Figure. The first is Rectangle and the second is
Triangle. Each of these subclasses overrides area( ) so that it returns the area of a rectangle
and a triangle, respectively.
class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
double area() {
System.out.println("Area for Figure is undefined.");
return 0;
}
}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class FindAreas {
public static void main(String args[]) {
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref;
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
figref = f;
System.out.println("Area is " + figref.area());
}
}
Output
Here in the above program, a superclass reference variable is used to reference a subclass
object.
Abstract classes
There are situations in which one would want to define a superclass that declares the structure
of a given abstraction without providing a complete implementation of every method.
Sometimes there will be a need to create a superclass that only defines a generalized form that
will be shared by all of its subclasses, leaving it to each subclass to fill in the details. In such a
condition a method is declared abstract in the base class. This method is overridden in the
subclass with the suitable implementation.
Any class that contains one or more abstract methods must also be declared abstract. To declare
a class abstract, the abstract keyword is used in front of the class keyword at the beginning of
the class declaration. There can be no objects of an abstract class. That is, an abstract class
cannot be directly instantiated with the new operator. Abstract constructors, or abstract static
methods cannot be defined. Any subclass of an abstract class must implement all of the abstract
methods in the superclass.
figref = t;
System.out.println("Area is " + figref.area());
}
}
It is now not possible to create an object of class Figure. But it is still possible to create a
reference variable.
To disallow a method from being overridden,specify final as a modifier at the start of its
declaration. Methods declared as final cannot be overridden. The following fragment illustrates
final:
class A {
final void meth() {
System.out.println("This is a final method.");
}
}
class B extends A {
void meth() { // ERROR! Can't override.
System.out.println("Illegal!");
}
}
Because meth( ) is declared as final, it cannot be overridden in B.
In order to prevent a class from being inherited, final keyword can be used. To do this, precede
the class declaration with final. Declaring a class as final implicitly declares all of its methods
as final, too.
final class A {
// ...
}
// The following class is illegal.
class B extends A { // ERROR! Can't subclass A
// ...
}
It is illegal for B to inherit A since A is declared as final.