0% found this document useful (0 votes)
2 views

Rest_of_module2(Multilevel_inheritance to using final in inheritance)

The document discusses the creation of multilevel hierarchies in object-oriented programming, illustrating how subclasses inherit traits from their superclasses. It covers method overriding, abstract classes, and the use of the final keyword to restrict method overriding and class inheritance. Examples are provided to demonstrate these concepts, including classes for Box, BoxWeight, Shipment, Figure, Rectangle, and Triangle.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Rest_of_module2(Multilevel_inheritance to using final in inheritance)

The document discusses the creation of multilevel hierarchies in object-oriented programming, illustrating how subclasses inherit traits from their superclasses. It covers method overriding, abstract classes, and the use of the final keyword to restrict method overriding and class inheritance. Examples are provided to demonstrate these concepts, including classes for Box, BoxWeight, Shipment, Figure, Rectangle, and Triangle.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Creating a Multilevel Hierarchy

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;

Box(Box ob) { // pass object to constructor


width = ob.width;
height = ob.height;
depth = ob.depth;
}
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
// Add weight.
class BoxWeight extends Box {
double weight; // weight of box
// construct clone of an object
BoxWeight(BoxWeight ob) { // pass object to constructor
super(ob);
weight = ob.weight;
}
// constructor when all parameters are specified
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;
}
// default constructor
BoxWeight() {
super();
weight = -1;
}
BoxWeight(double len, double m) {
super(len);
weight = m;
}
}
// Add shipping costs.
class Shipment extends BoxWeight {
double cost;
// construct clone of an object
Shipment(Shipment ob) { // pass object to constructor
super(ob);
cost = ob.cost;
}
// constructor when all parameters are specified
Shipment(double w, double h, double d,
double m, double c) {
super(w, h, d, m); // call superclass constructor
cost = c;
}
// default constructor
Shipment() {
super();
cost = -1;
}
// constructor used when cube is created
Shipment(double len, double m, double c) {
super(len, m);
cost = c;
}
}
class DemoShipment {
public static void main(String args[]) {
Shipment shipment1 =
new Shipment(10, 20, 15, 10, 3.41);
Shipment shipment2 =
new Shipment(2, 3, 4, 0.76, 1.28);
double vol;
vol = shipment1.volume();
System.out.println("Volume of shipment1 is " + vol);
System.out.println("Weight of shipment1 is "
+ shipment1.weight);
System.out.println("Shipping cost: $" + shipment1.cost);
System.out.println();

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

A more Practical example of Method Overriding.

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

Inside Area for Rectangle.


Area is 45
Inside Area for Triangle.
Area is 40
Area for Figure is undefined.
Area is 0

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.

The previous program is rewritten using the abstract keyword.


abstract class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
// area is now an abstract method
abstract double area();
}
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 AbstractAreas {
public static void main(String args[]) {
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref; // this is OK, no object is created
figref = r;
System.out.println("Area is " + figref.area());

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.

Using Final in Inheritance

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.

You might also like