Inheritance

 Inheritance

1.The process of getting property of one class into another class is called Inheritance.
2.In other word we can say that the process of deriving a new class from an old class is called inheritance in which the new class is called derived or child or sub class and old class is called Base or Parent or Super class.
3.When a class inherits the property of a class it means it can access all the data member and member function of that class except private element.
3.In this type of programming mainly two types of classes are used.

  • Parent/Super/Base class
  • Child/Sub/Derived class

Parent/Super/Base class
The class which is inherited by another class is called Parent or Super or Base class.
Child/Sub/Derived class
The class which inherits the property of another class is called Child or Sub or Derived class.

How to inherit one class into another

Derived class extends Base class
Example
class Subtraction extends Addition
Here Subtraction is a Derived class and Addition is a Base class and extends is a keyword which is used to inherit one class into another.

Types of Inheritance

There are five types of inheritance in Java.

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Single Inheritance

In this types of inheritance only two classes are used in which one is inherited by another.



Multiple Inheritance

1.When two or more than two classes are inherited by a single class simultaneously called multiple inheritance.
2.In other word we can say that in this type of inheritance Base class may be two or more than two but derived class should be one.
3.In this type of inheritance at least three class are compulsory.
4.Java does not support multiple inheritance therefor interface are used to implement multiple inheritance.


Multilevel Inheritance

1.When first class is inherited by second class, second class is inherited by third class and so on called multi level inheritance.
2.In this type of inheritance each derived class is the base class for the next class.
3.In this type of inheritance at least three class are compulsory.


Hierarchical Inheritance

1.When a single class is inherited by two or more than two classes simultaneously called hierarchical inheritance.
2.In other word we can say that in this type of inheritance derived class may be two or more than two but Base class should be one.
3.In this type of inheritance at least three class are compulsory.


Hybrid Inheritance

1.The combination of two or more than two inheritance is called Hybrid inheritance.
2.It can be combination of any two or more than two inheritance(single,multiple,multilevel,hierarchical).

3.In this type of inheritance atleast three class are compulsary.




Advantage of Inheritance

1.Code Reusability: It means function inside base class is shared by all the derived class.
2.Time Saving:Because there is no need to define existing property(same code) of a class in another class.
3.Less Cost:Because existing code is reused, it leads to less development and maintenance costs.
4.It helps to reduce code redundancy.




Post a Comment

0 Comments