A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

What is Inheritance concept in Java?

What is Inheritance concept in Java

What is Inheritance concept in Java?

  • When one object acquires or achieves all the properties and behaviour of the parent class is called Inheritance.

  • When you derived from an existing(parent) class, you can reuse methods and fields of the parent class.

  • Also we can add new methods and fields in your current class also.

  • The class that inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

  • In inheritance extends is the keyword used to inherit the properties of a parent class.

  • Inheritance provides code reusability.

  • Inheritance used to achieve runtime polymorphism.

  • It is also called IS-A relationship which is also known as a parent-child relationship.

Example of extends keyword:-




public class Parent
{

 //methods and fields
 //code statement
 
}
class Child extends Parent
{

 //methods and fields
 //code statement

}


 

Types of Inheritance in Java?

Single Level Inheritance:-

When a class inherits another class. In the example, Parent class inherits the Child class.






public class Parent
{
 void Mom()
 {
  System.out.println("Hi Mom..");
 }  
}
class Child extends Parent
{
 void Dad()
 {
  System.out.println("Hi dad...");
 }  
}
class Test
{  
 public static void main(String args[])
 {  
  Child obj=new Child();  
  obj.Mom();
  obj.Dad(); 
 }
}

Output:-
Hi Mom..
Hi Dad...


 
Program 2:-




class Dady
{
 String myCar="mercedes benz";
}
class Son extends Dady
{
 String myByke="kawasaki z900";
}
public class TestInheritance
{
 public static void main(String[] args) 
 {
  System.out.println("******Lets See the car of dady***********");
  Dady dad=new Dady();
  Son myson=new Son();

  System.out.println("Dady car is "+dad.myCar);
  System.out.println("Son car is "+myson.myCar);

  //System.out.println("Dady byke is "+dad.myByke); there is no byke to dady
  System.out.println("Son byke is "+myson.myByke); //but son has car because he extends dady
 }
}
Output:-
******Lets See the car of dady***********
Dady car is mercedes benz
Son car is mercedes benz
Son byke is kawasaki z900


 

Multi Level Inheritance:-






public class Parent
{
 void Mom()
 {
  System.out.println("Hi Mom..");
 }  
}
class Child extends Parent
{
 void Dad()
 {
  System.out.println("Hi dad...");
 }  
}
class Baby extends Child
{
 void man()
 {
  System.out.println("Hi baby...");
 }  
}
class Test
{  
 public static void main(String args[])
 {  
  Baby obj=new Baby();  
  obj.Mom();
  obj.Dad(); 
  obj.man();
 }
}

Output:-

Hi Mom..
Hi Dad...
Hi baby...


 

Hierarchical Inheritance:-

When two or more classes inherits a single class.






public class Parent
{
 void Mom()
 {
  System.out.println("Hi Mom..");
 }  
}
class Child extends Parent
{
 void Dad()
 {
  System.out.println("Hi dad...");
 }  
}
class Baby extends Parent
{
 void man()
 {
  System.out.println("Hi baby...");
 }  
}
class Test
{  
 public static void main(String args[])
 {  
  Baby obj=new Baby();  
  obj.Mom();
  //obj.Dad();//compile time error
  obj.man();
 }
}

Output:-
Hi Mom..
Hi baby...


 











LEARN TUTORIALS

.

.