Programs on Inheritance

Inheritance

Inheritance is creating the object for one class and acquiring the properties of the parent object . Main theme of inheritance is code reusability .

Types of inheritance in java :
  1. Single Inheritance.
  2. Multilevel Inheritance.
  3. Hierarchical Inheritance.
  4. Multiple Inheritance.
  5. Hybrid Inheritance.
Note:

Java does not support Multiple and Hierarchical Inheritance using classes and can be supported using interfaces .


Extends is the keyword to perform any type of inheritance.

1.Single Inheritance is a type of inheritance where one child class inherit the properties of one parent class.





Programs on Single Inheritance


class SingleInheritance 
{
String nameOfInstitute;

String location;

public void insertData(String nameOfInstitute,String location)
{
this.nameOfInstitute=nameOfInstitute;
this.location=location;
}
}
class SingleInheritanceTest extends SingleInheritance
{
public void printDetails()
{
System.out.println("Name Of Institue :"+nameOfInstitute+"\n"+"Location of institute:"+location);
}
public static void main(String args[])
{
SingleInheritanceTest sit=new SingleInheritanceTest();

sit.insertData("void Main Technologies","Dilsuknagar"); 
                
                //method of super class called by subclass object

sit.printDetails();
}
}

Save:

SingleInheritanceTest .java

Compile:

javac SingleInheritanceTest .java

Run:

java SingleInheritanceTest 

Output:

Name Of Institue: void Main Technologies

Location of institute: Dilsuknagar.

Note:

Super class methods or variables can be called by using the subclass Object , but not  vice-versa.


2.Multi Level Inheritance: Sub classes are inherited form super class and another sub class can be inherited from the 1st sub class and goes on such type of inheritances is called as Multi level inheritance.

                                       




Programs on Multi Level Inheritance.


class MultiLevelInheritance 
{
String nameOfInstitute;

String location;

public void insertData(String nameOfInstitute,String location)
{
this.nameOfInstitute=nameOfInstitute;
this.location=location;
}
}
class MultiLevelInheritance2 extends MultiLevelInheritance
{
String caption;
public void updateData(String caption)
{
this.caption=caption;
}
}
class MultiLevelInheritanceTest extends MultiLevelInheritance2
{
public void printDetails()
{
System.out.println(nameOfInstitute+"\n"+caption+"\n"+location);
}
public static void main(String args[])
{
MultiLevelInheritanceTest mit=new MultiLevelInheritanceTest();

mit.insertData("void Main Technologies","Dilsuknagar"); 

mit.updateData("Institute for all technical courses");

//method of super class called by subclass object

mit.printDetails();
}

}

Save:

MultiLevelInheritanceTest.java

Compile:

javac MultiLevelInheritanceTest.java

Run:

java MultiLevelInheritanceTest

Output:



Institute for all technical courses
Dilsuknagar.

Note:

By creating a object for last sub class we can invoke all the classes , variables ,  and methods. But by creating the object for intermediate classes we cant call the sub classes of that intermediate class but we can invoke the super classes of that intermediate class. 

3.Hybrid Inheritance

Combination of two or more inheritance is called as Hybrid Inheritance.

Programs on  Hybrid Inheritance.


class HybridInheritance
{
String instituteName;

String caption;
}
class HybridInheritance2 extends HybridInheritance1
{
instituteName="Void Main Technologies";
void print1()
{
System.out.println(instituteName);
}
}
class HybridInheritance3 extends HybridInheritance1
{
caption="Institute for all Technical Courses";
void print2()
{
System.out.println(caption);
}
}
class HybridInheritanceTest extends HybridInheritance2,HybridInheritance3
{
public static void main(String[] args) 
{
HybridInheritanceTest hit=new HybridInheritanceTest();

hit.print1();

hit.print2();
}
}

Save:

 HybridInheritanceTest.java

Compile:

javac  HybridInheritanceTest.java

Run:

 java HybridInheritanceTest

Output:

Error

Note:

Java do not support Multiple and Hierarchical Inheritance using classes and can be supported using interfaces . Hence, Java will not support Hybrid Inheritance Using classes.


Lets see all the remaining inheritance in the Topic of Interfaces .



Comments

Popular posts from this blog

Arrays In Java

Java Inner Classes