Inheritance Concepts In Java

BoxColor(33.33,43.33,53.33,”BLUE”);

double vol = MyBox1.volume();

System.out.println(“volume of MyBox1 is  : “+vol);

System.out.println(“Color of MyBox1 is  : “+MyBox1.color);

vol = MyBox2.volume();

System.out.println(“volume of MyBox2 is  : “+vol);

System.out.println(“Color of MyBox2 is  : “+MyBox2.color);

}

}

Explanation : Compare this subclass ColorBox and BoxWeight. Each subclass adds its own unique attributes. This is the strength of inheritance.

A Superclass Variable Can Reference a Subclass Object

A reference variable of a superclass can be assigned a reference to any subclass derived from that superclass.
This aspect of inheritance is quite useful in a variety of situations.

/*consider the below example in which a super class variable references a subclass Object.*/

class Box{

double WIDTH,LENGTH,HEIGHT;

Box()

{

WIDTH=LENGTH=HEIGHT=-1;

}

Box(double side)

{

WIDTH=LENGTH=HEIGHT=side;

}

Box(double WIDTH,double LENGTH,double HEIGHT)

{

this.LENGTH = LENGTH;

this.WIDTH = WIDTH;

this.HEIGHT = HEIGHT;

}

Box(Box obj)

{

WIDTH  =  obj.WIDTH;

LENGTH = obj.LENGTH;

HEIGHT = obj.WIDTH;

}

double volume(){

return (LENGTH * WIDTH * HEIGHT);

}

}

class BoxWeight extends Box

{

double WEIGHT;

BoxWeight()

{

super();

WEIGHT=-1;

}

BoxWeight(double side,double WEIGHT)

{

super(side);

this.WEIGHT = WEIGHT;

}

BoxWeight(double WIDTH,double LENGTH,double HEIGHT,double WEIGHT){

super(WIDTH,LENGTH,HEIGHT);

this.WEIGHT = WEIGHT;

}

BoxWeight(BoxWeight obj){

super(obj);

this.WEIGHT = obj.WEIGHT;

}

}

class RefDemo

{

public static void main(String args[])

{

BoxWeight weightbox = new BoxWeight(3,5,7,8.37);

Box       plainbox  = new Box();

System.out.println(“The volume of the box “+weightbox.volume());

System.out.println(“The weight of the box “+weightbox.WEIGHT);

//Now assign BoxWeight reference to Box reference

plainbox = weightbox;

//The following statement is valid because Box defines volume() method

System.out.println(“The volume of the box “+plainbox.volume());

//The following statement is invalid because Box doesnot define WEIGHT member

// System.out.println(“The weight of the box “+plainbox.WEIGHT);

}

}

Explanation: It is the type of reference variable, that determines what members can be accessed. when a reference to a subclass object is assigned to a superclass reference variable, we will have access to those parts of the object defined by the superclass. This makes sense, because superclass has no knowledge of what a subclass adds to it.This is why the last code in the above program is illegal and hence is commented.

Member Access and Inheritance

Although a subclass includes all of the members of its super class, it cannot access those members of the superclass that have been declared as private.
Java provides a solution, for this problem, by using a key word super.

Using Super

When u declare member variables of a class using access specifier, private and if you use  that class as super class, then  there would be no way for a subclass to directly access or initialize these variables, on its own(because the primary attribute of OOP’s, encapsulation is implemented).
Java provides a solution, for this problem,by using a key word super.
Whenever a subclass needs to refer to its super class, it can do so using super.
super has two general forms of uses:
The first use is to, call the super class’s constructor.The second use is to, access a member of the superclass that has been hidden by a member of a subclass. The member could be either a member variable or member method of a class

Lets examine the uses of super, here in detail
First use of super :

Using super to call super class constructors.

A subclass can call, a super class constructor method, by use of the following form of super

super( parameter-list )

here parameter-list specifies, parameters needed, if any, by the constructor in the super class.

Remember super() must always be the first statement, to be executed in side the subclasses

constructor.

/*

lets reconsider the example BoxWeigthDemo.java and save it as  BoxWeigthDemo1.java.

This program illustrates the power of ecapsulation and usage  of super key word in  the

Pages: 1 2 3 4 5 6 7