alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    C#
    Deep Learning
    Statistics for AIML
    Natural Language Processing
    Machine Learning
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    C#
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    C#
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Identity And Access Management
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

Java - Object-Oriented Programming (OOPS) Concept - Inheritance Tutorial

Inheritance is a process of copying all property and behavior from parent class to child class.

  • It provides code reusability.( Reusability means child class can use methods and fields of parents class )
  • It can also be used to achieve runtime polymorphism using method overriding.

Child class (Subclass) can use the methods and field of parents class(super Class) after inheritance.

For inheritance between parents class and child class, use the extends keyword.

extends keyword is used to gain the functionality into child class using parents class.

 

Syntax for inheritance

class ParentClass {

   .....

}

class ChildClass extends ParentClass {

   .....

}

 

 

Example 1-

class Bird{ //Parent Class
String name2="Bird";
void fly(){
System.out.println(" can Fly");
}
}
public class Parrot extends Bird{ //Child Class
String name1="Parrot";
void color(){
System.out.print(" is a green color Bird. ");
}
public static void main(String args[]){
Parrot p=new Parrot();
System.out.print(p.name1);
p.color();
System.out.print(p.name2);
p.fly();
}
}

Run Program

Example 1 Explanation-

As in the above example, Parrot is SubClass i.e ChildClass and  Bird is SuperClass i.e ParentClass. The relationship between child class and parent class using extends keyword to show that Parrot is a type of Bird Class.

Here Parrot Class can use the field and method of own class as well as Bird Class.

 

Types Of Inheritance in Java

  1. Single
  2. Multilevel
  3. Hierarchical

Some of the inheritance can be used through interface only that are

  1. Multiple
  2. Hybrid

This two inheritance cannot be used normally like other inheritance

 

  1. Single Inheritance

When single Child Class inherits the property of single Parent class. This type of inheritance is known as single inheritance.

class ParentClass {

   .....

}

class ChildClass extends ParentClass {

   .....

}

 

Eg-

class Bird{ //Parent Class
String name2="Bird";
}
public class Parrot extends Bird{ //Child Class
String name1="Parrot";
public static void main(String args[]){
Parrot p=new Parrot();
System.out.println(p.name1+" is "+p.name2);
}
}

Run Program

  1. Multilevel Inheritance

In multilevel inheritance, there can be any number of class that forms a chain-like structure.

In the above example, there is childclass1 which inherits the property of childclass2. Childclass2 which inherits the property of Childclass3. Childclass3 which inherits the property of ParentClass.

And therefore in this example, childclass1 can access the method and field of ParentClass.

 

class ParentClass {

   .....

}

class ChildClass3 extends ParentClass {

   .....

}

class ChildClass2 extends ChildClass3 {

   .....

}

class ChildClass1 extends ChildClass2 {

   .....

}

 

Example-

class Species{  //Parent Class

 String name3="Species";

}

class Bird extends Species{  //Parent Class

 String name2="Bird";

} 

public class Parrot extends Bird{  //Child Class

 String name1="Parrot"; 

 public static void main(String args[]){ 

   Parrot p=new Parrot(); 

   System.out.println(p.name1+" is "+p.name2+". "+p.name2+" is "+p.name3); 

} 

} 

Run Program

 

In the above example, the Species variable name3 can be accessed using the Parrot object.

 

  1. Hierarchical Inheritance

When two or more Child classes can inherit a single parent class. It is known as Hierarchical inheritance.

In the above example there is childclass1 and childclass2 which inherit the property of ParentClass. Here one Childclass cannot use the property i.e is method and field of other Child classes. But both the child class can use the property of the Parent class

 

class ParentClass {

   .....

}

class ChildClass2 extends ParentClass {

   .....

}

class ChildClass1 extends ParentClass {

   .....

}

Example1

class Bird{ //Parent Class
String name3="Bird";
}

class Crow extends Bird{ //Child Class 2
String name2="Crow";
}

class Parrot extends Bird{ //Child Class1
String name1="Parrot"; }
public class HierarchicalInheritance{
public static void main(String args[]){
Parrot p=new Parrot();
System.out.println(p.name1+" is "+p.name3);
//System.out.println(p.name2+" is "+p.name3); this line will give error: cannot find symbol name2
}
}

Run Program

  1. Multiple Inheritance

In this inheritance, one child class can inherit multiple parents class.

This inheritance is only supported using an interface to prevent ambiguity.

class Bird1{  //Parent Class
 String name3="Bird1";
}

class Bird2{  //Child Class 2
 String name2="Bird2";
} 

class Parrot extends Bird1,Bird2{  //Child Class1
 String name1="Parrot"; }

 public class MultipleInheritance{
 public static void main(String args[]){ 
   Parrot p=new Parrot(); 
   System.out.println(p.name1+" is "+p.name3); 
} 
}  

 

The above program will give an error. Therefore to prevent such error multiple inheritances are only used with the interface.

  1. Hybrid Inheritance

Hybrid inheritance is a combination of hierarchical and multiple.

As it also creates ambiguity. Hence this inheritance is not used in java.

Java

Java

  • Introduction
  • Installation and running a first java program
    • JDK Installation
    • Environment Variable Path Setup
    • Running a first java program
  • 1st Java Program Explanation
    • Program Syntax
  • JDK,JRE,JVM
    • JRE
    • JDK
    • JVM
  • Thing to know before proceed
    • Variable
    • Datatype
    • Operator
    • Keyword
    • Access Modifier
  • Decision Control and looping statement
    • If-else
    • Switch
    • For
    • While
    • do-while
    • break
    • continue
  • Object-Oriented Programming (OOPS) Concept
    • About OOPs
    • Object & Class
    • Inheritance
    • Polymorphism (Runtime & Compile Time)
    • Abstraction (Using abstract and interface)
    • Encapsulation

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.