Inheritance in Java with Example - Javastudypoint

Saturday, December 29, 2018

Inheritance in Java with Example


Inheritance is one of the key features of object-oriented programming (OOPs). Inheritance is a process of inheriting the properties and behavior of the existing class into a new class. When we inherit the class from an existing class, we can reuse the methods and fields of the parent class. Inheritance can be defined as 
Is-A relationship, which is also known as the parent-child relationship.

Why we use Inheritance?

  • The main purpose of inheritance is code reusability. We can reuse the code when we inherit the properties and behavior of the existing class into a new class.
  • The runtime polymorphism (method overriding)  can be achieved by inheritance.


Important terminologies in Inheritance:

Class: Class is used to describe the properties and behavior of an object. It is a blueprint of an object.

Super Class: Super Class is also known as a parent class or a base class. The super class is the class whose features are inherited from a subclass.

Sub Class: Sub Class is also known as a child class or a derived class or an extended class. Sub Class is the class which inherits the other class.

The syntax of Inheritance in Java:


class SuperClass{
   //methods and fields
}
class SubClass extends SuperClass{
      //methods and fields
}
The extends is a keyword which is used to inherit the properties and behavior of a class.

Inheritance Example in Java:


class Person{
 private String name;
 private int age;
 public void setName(String name){
  this.name = name;
 }
 public String getName(){
  return name;
 }
 public void setAge(int age){
  this.age = age;
 }
 public int getAge(){
  return age;
 }
}
class Student extends Person{
 private int rollno;
 public void setRollno(int rollno){
  this.rollno = rollno;
 }
 public int getRollno(){
  return rollno;
 }
}
class InheritanceExample{
 public static void main(String args[]){
  Student obj = new Student();
  obj.setRollno(101);
  obj.setName("Prashant");
  obj.setAge(21);
  System.out.println("Student rollno is: "+obj.getRollno());
  System.out.println("Student name is: "+obj.getName());
  System.out.println("Student age is: "+obj.getAge());
  
 }
}

Output: 


Types of Inheritance in Java:

  • Java supports only three types of inheritance Single Inheritance, Multilevel Inheritance, and Hierarchical Inheritance.
  • Multiple and Hybrid Inheritance in java can be supported through interface only.


1. Single Inheritance: 

In single inheritance, one class can extend the functionality of another one class. In single inheritance only one parent and one child class is present.
Inheritance in Java
Single Inheritance
Syntax:

class SuperClass{
      //methods and fields
}
class SubClass extends SuperClass{
         //methods and fields
}

Example of Single Inheritance:

class Birds{
 public void birdsEat(){
  System.out.println("Birds are eating");
 }
}
class Crow extends Birds{
 public void crowEat(){
  System.out.println("Crow is eating");
 }
}
class Person{
 public static void main(String args[]){
  Crow obj = new Crow();
  obj.birdsEat();
  obj.crowEat();
 }
}

Output:

2. Multiple Inheritance:

In multiple inheritance, there is more than one parent class and one child class is present. The child class inherits from more than one parent class. Java doesn't support multiple inheritance. It can be supported through interface only.

Inheritance in Java
Multiple Inheritance



3. Multilevel Inheritance: 

In multilevel inheritance, there is more than one level. If one class can inherit from a derived class and derived class becomes the base class of the new class then it is called multilevel inheritance.



Inheritance in Java
Multilevel Inheritance

Syntax:

class A
{
  // methods and fields;
}
class B extends A
{
 // methods and fields
}
class C extends B
{
   // methods and fields
}

Example of Multilevel Inheritance:

class Birds{
 public void birdsEat(){
  System.out.println("Birds are eating");
 }
}
class Crow extends Birds{
 public void crowEat(){
  System.out.println("Crow is eating");
 }
}
class BabyCrow extends Crow{
 public void babyCrowEat(){
  System.out.println("Baby Crow is eating");
 }
}
class MultipleInheritance{
 public static void main(String args[]){
  BabyCrow obj = new BabyCrow();
  obj.birdsEat();
  obj.crowEat();
  obj.babyCrowEat();
 }
}

Output:


4. Hierarchical Inheritance:

In Hierarchical inheritance, from a single parent class, we are inheriting multiple child class. In the below diagram, class A inherits the class B, class C, and class D.
Inheritance in Java
Hierarchical Inheritance

Syntax:


class A
{
  // methods and fields;
}
class B extends A
{
  // methods and fields
}
class C extends A
{
   // methods and fields
}
class D extends A
{
   // methods and fields
}

Example of Hierarchical Inheritance:

class Birds{
 public void birdsEat(){
  System.out.println("Birds are eating");
 }
}
class Crow extends Birds{
 public void crowEat(){
  System.out.println("Crow is eating");
 }
}
class Sparrow extends Birds{
 public void sparrowEat(){
  System.out.println("Sparrow is eating");
 }
}
class Person{
 public static void main(String args[]){
  Sparrow obj = new Sparrow();
  obj.birdsEat();
  obj.sparrowEat();
  obj.crowEat();  //compile-time error
 }
}

Output:


5. Hybrid Inheritance: 

If we are using multiple types of inheritance simultaneously such types of inheritance are called Hybrid Inheritance. But java does not support Hybrid Inheritance. Hybrid Inheritance can be supported through interface only.


Inheritance in Java
Hybrid Inheritance

Why Java doesn't support multiple inheritance?

Java doesn't support multiple inheritance due to the ambiguity problem.

Consider a scenario where X, Y, and Z are the three classes. The Z class inherits both the class i.e. class X and class Y. If class X and class Y have the same method and we call it from child class object ( class Z object), there will be ambiguity to call the method of class X or a class Y. Therefore compiler show compiles time error if we inherit 2 classes.
Inheritance in Java


class X{
 public void show(){
  System.out.println("We are in X class");
 }
}
class Y extends X{
 public void show(){
  System.out.println("We are in Y class");
 }
}
class Person extends X,Y{
 public static void main(String args[]){
  Person obj = new Person();
  obj.show();  //Now which show() method would be invoked?  
 }
}


You may also like:

Encapsulation in Java OOPs with Example.
Java Authentication and Authorization Service (JAAS)
MVC Architecture in JSP with Example
JSP Standard Tag Library - JSTL Tutorial 
Session Tracking in Servlet and its techniques
JSP Life Cycle. What are Java Beans?


No comments:

Post a Comment