Inheritance

It is possible to acquire all the members of a class and use them in another class by relating the objects of the two classes. {OR} It create new classes from existing classes, so that the new classes will acquire all the features of existing classes is called Inheritance. When a class is written by a programmer and another programmer wants the same features members(i.e is Variables, methods) is his class also, then the other programmer will go for inheritance. This is done by deriving the new class from the existing class.
There is a two types of classes in Inheritance.

1) Super Class 2) Sub Class
1) Super Class : The class
from which members are acquring is know as Super Class.

2) Sub Class : The class To which members are acquired is know as Sub Class.
Java provides a keyword "
extends" for inheritance. In a java " subclass extends superclass" (class B extends A)
1) Inheritance is basic concept of OOP which will allow you to use Parent class Features. 2) Using inheritance you can use parent class variable and method using child class object.

Inheritance is used:-
1) Code reusability
2) No code duplication
3) For method overidding

Scenarios :
1)
Child class reference and child class object - This will allow to access all the methods of base class and Child class.
2)
Base class reference and Base class object -This will allow to access all the methods of base class only.
3) 
Base class reference and child class object - This will allow to access all the methods of base class only and not child class method.
4)
Child class reference and Base class object- This will be invalid scenario. Child can not hold parent.

There are set of rules to be followed for creating an inheritance. These are the rules of an inheritance:

1) Multiple inheritance will not supported in Java.
2) One Sub Class should inherit one Super class only.
3) Private/Varible members can't be inherited to subclass.
4) Constructors con't be inherited to subclass.
5) Static members con't be inherited to subclass.

Advantages of inheritance in java:-
In inheritance, a programmer reuses the superclass code without rewriting it, in creation of sublcass. So Developping the classes becomes very easy.

Comments