Inheritance

If we talk about the real world. Everyone acquires the property of there parents.

So this thing we can also do in java. By inheritance.

Inheritance:

it is a concept in java by which we can inherit the class. Or

use methods and members of a class in another class. By creating an object of the current class.

For example:

inheritance java

Here we have two class which have different methods in it

We want to use the method of class Animal in class Dog

So, for this, we have to inherit class Animal to class Dog.

inheritence in java

Important terms:

Sub Class/Child Class/derived class/extended class:

This all term used for those classes which inherit other class. For example here Dog class inherits the animal class.

So, the dog is a subclass or base class of Animal class

Super Class/Parent Class/ Base class:

These terms use for those classes from where a class inherits the feature. For example:

Class Dog inherits the feature from Animal class. So, Animal class is a superclass or base class for Dog class

How to inherit the class:

To inherit the class  we use extends keyword

Syntax:

class Subclass-name extends Superclass-name  
{  
   //methods and fields  
}  

Let’s see an example

class Animal{  
void eat()
{
   System.out.println("eating...");
}  

void run()
{
   System.out.println("eating...");
}  

}  

class Dog extends Animal{  
void barking()
{
    System.out.println("barking...");
}  
}  
class Test{  
public static void main(String args[]){  
Dog d=new Dog();  
d.barking();  
d.eat();  
}
}  
barking...
eating...

Type of inheritance

There are three types of inheritance used in java

Types of inheritance in java

Note:

there are other two more type

Multiple and Hybrids.

but this is not supported in java but we can achieve this by the interface.  We learn to interface in another article

Single Inheritance :

Multiple and hybrids in java

When a single class inherits another class, is called single inheritance.

See example of single inheritances

class Animal{  
void eat()
{
   System.out.println("eating...");
}  

void run()
{
   System.out.println("eating...");
}  

}  

class Dog extends Animal{  
void barking()
{
    System.out.println("barking...");
}  
}  
class Test{  
public static void main(String args[]){  
Dog d=new Dog();  
d.barking();  
d.eat();  
}
}  
barking...
eating...

Multilevel inheritance

Multilevel inheritance in java

Understand with an example

Let’s suppose we have 3 classes’  class A, class B, class C

If class B inherited the class A then class B acquires the property of class B

After this, if class C  inherited the class B

Then class C has the property of both class,  class A and class B

This is called multi-label inheritance.

Example:

class A{  
void show1()
{
   System.out.println("show 1 method");
}  
}  
class B extends A{  

void bark()
{
  System.out.println("show 2 method");
}  
}  
class C extends Dog
{  
 void show3()
{
  System.out.println("show 3 method");
} 
 
}  


class Test{  
public static void main(String args[]){  
C temp=new C();  
temp.show1();  
temp.show2();  
temp.show3();  
}
}  
Show 1 method
Show 2 method
Show 3 method

Here class c acquire the property of both class A and B by we can access the member of class A and B by creating the object of class C

Hierarchical Inheritance:

When two different class inherits a single class is called hierarchical inheritance

Or when a superclass has two or more subclasses  is called hierarchical inheritance

Hierarchical Inheritance:

Example:

In fig. class A is a super class and class B and C is the subclass.

So class B can access  member of Class A

Class C also can access member of class A

class A
{  
      void show1()
{
    System.out.println("method of class A");}  
}  
class B extends A
{  
void show2()
{
System.out.println("method of class B");
}  
}  
class C extends A{  
void show3()
{
System.out.println("method of class C");
}  
}  

class Test{  
public static void main(String args[]){  
C temp=new C();  
c.show3();  //member of class c
c.show1();  // member of class A
//c.show2();//C.T.Error  
}
}  
Method of class c
Method of class A



SHARE THIS PAGE





Important Link