Inheritance

Posted on

 Java strongly supports the concept of reusability. It is always better if we could reuse something, which is already existing without trying to create the same thing overall again. The mechanism of inheriting the properties of one into another is referred as inheritance. The class whose member you want to inherit is referred as the super class. The class into which the members of super class will be inherited is referred as the sub class.

         Java supports three types of inheritance namely

  • Single inheritance.
  • Multi-level inheritance.
  • Hirearical inheritance.

Creating a sub class:

         May it be any kind of inheritance, the basic format of creating a subclass remains the same.

Syntax:

class sub_class extends super_class
{
…..........................
…..........................
Body of the sub class;
…..........................
…..........................
}

         When you extend a super class all the public members of the super class is inherited into the subclass and hence can be accessed by objects of the sub class.

Single inheritance:
         In a single inheritance we just have two class i.e. a super class and a sub class.

class Box
{
double width;
double height;
double depth;
void input()
{
width=10;
height=20;
depth=30;
}
void print()
{
System.out.println("width of the box is : "+width);
System.out.println("height of the box is : "+height);
System.out.println("depth of the box is : "+depth);
}
}
class Boxvol extends Box
{
void compute()
{
double vol=width*height*depth;
System.out.println("The volume of the box is : "+vol);
}
}
class App25
{
public static void main(String yck[])
{
Boxvol b=new Boxvol();
b.input();
b.print();
b.compute();
}
}


output:

\>javac App25.java
\>java App25
width of the box is : 10.0
height of the box is : 20.0
depth of the box is : 30.0
The volume of the box is : 6000.0

Multilevel inheritance:

         In multilevel inheritance we have a series of class created one from another. The top most class is referred as super class and all the subsequent class are referred as subclasses.

class Box
{
double width;
double height;
double depth;
void Input()
{
width=10;
height=20;
depth=30;
}
void Print()
{
System.out.println("width="+width);
System.out.println("height="+height);
System.out.println("depth="+depth);
}
}
class BoxLength extends Box
{
double length;
void Input_Length()
{
length=40;
}
void Print_Length()
{
System.out.println("length="+length);
}
}
class BoxVolume extends BoxLength
{
double volume;
void Compute()
{
volume=width*height*depth*length;
System.out.println("Volume of the box :"+volume);
}
}
class App26
{
public static void main(String yck[])
{
BoxVolume B=new BoxVolume();
B.Input();
B.Input_Length();
B.Print();
B.Print_Length();
B.Compute();
}
}


output:

\>javac App26.java
\>java App26
width=10.0
height=20.0
depth=30.0
length=40.0
Volume of the box :240000.0

Hierarchical inheritance:
         In a hierarchical inheritance, we have a super class at the root and all subsequent sub-classes extending from the super class in the form of branches.

class Number
{
int x,y;
void input(int p,int q)
{
x=p;
y=q;
}
void print()
{
System.out.println("The value of x is : "+x);
System.out.println("The value of y is : "+y);
}
}
class Addition extends Number
{
int sum;
void compute()
{
sum=x+y;
System.out.println("The sum of X & Y is : "+sum);
}
}
class Multiplication extends Number
{
int pro;
void compute()
{
pro=x*y;
System.out.println("The product of X & Y is : "+pro);
}
}
class App27
{
public static void main(String yck[])
{
Addition a=new Addition();
a.input(10,20);
a.print();
a.compute();
Multiplication m=new Multiplication();
m.input(50,60);
m.print();
m.compute();
}
}


output:

\>javac App27.java
\>java App27
The value of x is : 10
The value of y is : 20
The sum of X & Y is : 30
The value of x is : 50
The value of y is : 60
The product of X & Y is : 3000

Constructors in inheritance:
         Constructors are usually executed when you create an object for the corresponding class. But in inheritance constructors are not inherited into the sub class. If the super class contains a constructor then the question arises how does it execute, because in inheritance objects are mostly created for sub classes.

class Box
{
double width;
double height;
double depth;
Box()
{
width=5;
height=6;
depth=7;
}
void print()
{
System.out.println("Width of the box is : "+width);
System.out.println("height of the box is : "+height);
System.out.println("depth of the box is : "+depth);
}
}
class Boxvol extends Box
{
double vol;
Boxvol()
{
vol=width*height*depth;
}
void printvol()
{
System.out.println("The volume of the box is : "+vol);
}
}
class App28
{
public static void main(String yck[])
{
Boxvol b=new Boxvol();
b.print();
b.printvol();
}
}


output:

\>javac App28.java
\>java App28
Width of the box is : 5.0
height of the box is : 6.0
depth of the box is : 7.0
The volume of the box is : 210.0

Constructors with arguments: If a super class contains a constructor with arguments, then it becomes mandatory for the sub class to have a constructor. The sub class constructors takes up the responsibility to pass values to the super class constructor using a reserved key word “super”.

Syntax: super(list of values);

class Box
{
double height;
double depth;
double width;
Box(double h, double d, double w)
{
height=h;
depth=d;
width=w;
}
void print()
{
System.out.println("Height is = "+height);
System.out.println("Depth is = "+depth);
System.out.println("Width is = "+width);
}
}
class Boxvol extends Box
{
double vol;
Boxvol(double he, double de, double wi)
{
super(he, de, wi);
vol=height*depth*width;
}
void printvol()
{
System.out.println("The volume of Box is = "+vol);
}
}
class App29
{
public static void main(String yck[])
{
Boxvol b=new Boxvol(10, 20, 30);
b.print();
b.printvol();
}
}


output:

\>javac App29.java
\>java App29
Height is = 10.0
Depth is = 20.0
Width is = 30.0
The volume of Box is = 6000.0