google.com, pub-8786015629279405, DIRECT, f08c47fec0942fa0 Inheritance in Java

Inheritance in Java

0


What is Inheritance ?

Inheritance in Java, is a technique in which a class (is called child class) holds / acquires all the properties and method of  other class (is called parent/base class). 
This is one of the property of OOPS ( Object Oriented Programming Structure).
The main aim to use the Inheritance in Java, is reusability 
For Example:
Using inheritance, we can create a class A and this class use the properties of class B and we can also add some method in class A. So both class methods can be accessed through the object of class A. Here class B is reusing by class A.  

Syntax of Inheritance

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

Four main terms used in the Syntax

  1. Class
  2. Sub-class
  3. Super-class
  4. extends


  1. Class - A class is a collection of objects which have common properties. It is a template or blueprint from which objects are created.
  2. Sub-class - Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
  3. Super-class – Super-class is the class from where a subclass inherits the features. It is also called a base class or a parent class.
  4. Extends - The extends keyword indicates that you are making a new class that derives from an existing class.


Types of Inheritance in Java

There are three types of Inheritance in Java using Class.

  1. Single Level Inheritance
  2. Multilevel Inheritance 
  3. Hierarchical Inheritance

Other Two types of Inheritance, 

  1. Multiple Inheritance
  2. Hybrid Inheritance 

Only create by Interface in Java. But not created by class.

Single Level Inheritance in Java

a class A inherits another class B, it is known as a single inheritance.

Single Level Inheritance


Example Single Level Inheritance

class A{  
void displayA(){System.out.println(“Class A...");}  
}  
class B extends A{  
void displayB(){System.out.println(“Class B...");}  
}  
class singleinheritance{  
public static void main(String args[]){  
B b=new B();  
b.displayB();  
b.displayA();  
}} 

Output:
 Class B…
 Class A…
 
Multi-Level Inheritance in Java

When a class A inherit another class B and class B another class C and so on or like a chain of inheritance, it is known as multilevel inheritance.


Multilevel Inheritance in Java



class A{  
void displayA(){System.out.println(“Class A...");}  
}  
class B extends A{  
void displayB(){System.out.println(“Class B...");}  
}  
class C extends B{  
void displayC(){System.out.println(“Class C...");}  
}  
class multilevelinheritance{  
public static void main(String args[]){  
C b=new C();  
b.displayB();  
b.displayA(); 
b.displayC(); 
}} 

Output:

 Class B…
 Class A…
 Class C…
 
Hierarchical Inheritance in Java

When two or more classes inherits a single class, it is known as hierarchical inheritance.

Hierarchical Inheritance in Java



class A{  
void displayA(){System.out.println(“Class A...");}  
}  
class B extends A{  
void displayB(){System.out.println(“Class B...");}  
}  
class C extends A{  
void displayC(){System.out.println(“Class C...");}  
}  
class multilevelinheritance{  
public static void main(String args[]){  
C b=new C();  
b.displayA(); 
b.displayC(); 
}} 

Output:

Class A…
 Class C…
 
Here, Class C only use the method of class A, and class B also use the method of Class A. B don’t use the method of Class C and C don’t use the method of Class B.


Multiple Inheritance in Java

When a class A inherit two or more classes ( B, C and so on ) is called Multiple Inheritance. 

Multiple Inheritance in Java


Hybrid Inheritance in Java

When more than one type of inheritance technique used under a inheritance technique is called Hybrid Inheritance. 

Hybrid Inheritance in Java


Multiple and Hybrid Inheritance is done by only using the Interface in Java.


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Thank you for your interest 😊

We will back shortly after reviewing...

Thank you for your interest 😊

We will back shortly after reviewing...

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top