How does inheritance work in Java?

What is inheritance in Java/OOPs

Inheritance is the main feature of object oriented programming language. Inheritance allows the class to use the properties and methods of other classes. In other words we can say that the sub classes derive the properties and functionalities from the base class. Sub classes are also known as derived class and base class is known as super class.
Super class and sub class have a “IS-A” relationship between them.

Types of Inheritance

There are five types of inheritance
  1. Single Inheritance
    In this there is only single level of inheritance in which one child class inherits the property of the base class. It is the simplest form of the inheritance.
  2. Multiple Inheritance
    In this type of inheritance there is two or more super class and the child class inherits the property from it. Multiple inheritance is not possible in java programming language. In order to implement multiple inheritance we have the concept of Interfaces.
  3. Hierarchical Inheritance
    In hierarchical inheritance, multiple derived classes inherits the features from single base class.
  4. Multilevel Inheritance
    In multilevel inheritance the derived class inherits from a class which in turn inherits from some other class. Or we can say that there is a super class for one and subclass for others.
  5. Hybrid Inheritance
    Hybrid inheritance is the combination of hierarchical and multilevel inheritance.
The important points about inheritance
  • Inheritance allows the developers to reuse the codes so that efforts and time can be saved.
  • A subclass can be treated as if it is a super class.
  • Inheritance can be implemented as the objects of both super class and subclass can be created in the applications.
  • A class can be extended in which the additional and exclusive functionality can be placed without altering the super class functionalities and properties.
  • Through inheritance the relationships among objects(of sub class and super class) can easily be established.

An example of Inheritance in Java

class Student
 {
   String name,PRN;
   String specialization;
   Student(String name ,String PRN, String specialization)
    {
      this.name = name;
      this.PRN = PRN;
      this.specialization = specialization;
    }
  void display()
  {
    System.out.println("Name of Student: " +name);
    System.out.println("PRN of Student: " +PRN);
    System.out.println("Specialization of Student: " +specialization);
  }
 }
classPGStudent extends Student
 {
    long numberOfLectures;
    PGStudent(String name ,String PRN, String specialization,long numberOfLectures)
    {
      super(name,PRN,specialization);
      this.numberOfLectures = numberOfLectures;
     }
//overriding table
   void display()
   {
      super.display();
      System.out.println("No of lectures for PG student : "+numberOfLectures);
    }
  }
classUGStudent extends Student
  {
    long numberOfLectures;
    UGStudent(String name ,String PRN, String specialization,long numberOfLectures)
    {
      super(name,PRN,specialization);
      this.numberOfLectures = numberOfLectures;
    }
    void display()
    {
      super.display();
      System.out.println("No of lectures for PG student: "+numberOfLectures);
     }
}

In this example, there is Student base class and two sub classesPGStudent and UGStudent. There some common functionalities which child classes PGStudent and UGStudent can use from base class.

Read More:
Abstraction Vs Encapsulation
What are the advantages of using object oriented programming?
Manual Testing: Advantages and Disadvantages
Pros and Cons of White Box Testing

Copyright © ianswer4u.com

0 Reactions:

Post a Comment