Inheritance in java is one of the most important concept that is used most extensively in real world applications.
In real world, a web application is made of different layers like front end that is seen by user and on which user interacts ,when user sends requests to server front layer , the one that handles the request and send requests to next layer, business layer i.e layer which handles all the business logic and dao layer i.e database layer come into picture.
Every layer has a base class like for example in case of using Spring framework for mvc mechanism , we define a BaseController class , this class has all the basic methods like getting details of logged in user and storing the info in session and many more methods that can be inherited and used by all the other controller classes. A need for inheritance arises when we need some base class having all the generalized methods and fields which a specialized child class can inherit from base class and then add some more specialized methods and fields.
Inheritance in java means by extending a class , the child class , the one that extends , inherits all the fields and methods of the parent class. Inheritance thus promotes code reusability.
Apart from classes , interfaces can also inherit from their parent interfaces by extending the same.
Lets dive deeper into inheritance
Inheritance with classes
1)In Java, when an “Is-A” relationship exists between two classes we use Inheritance.
2)The parent class is termed super class or base class and the inherited class is the sub class.
3)The keyword “extend” is used by the sub class to inherit the features of super class.
Java only supports single inheritance between classes. This means a class can inherit only from a single class , it cannot extend multiple classes.
A derived class inherits all the public and protected members from the base class, which are not static.In addition, the members with default access are inherited if the child class is in the same package as parent class.
Inheritance and Type Casting
A parent class reference can always refer to and have instance of sub class.
For example, suppose student class extends Parent class.This is referred to as upcasting (from a subclass type to a superclass type).
Student student=new Student();
The above kind of instances one can find throughout java code as this is also one of the best coding practices . As long as it is possible one should declare variable type as that of Parent class, method arguments should also be declared as parent class variable to make it resuable for different child class.
Example, public void test(List list) , now this list can have instance of either an ArrayList, LinkedList any child class.
It may also be possible to cast an object from a superclass type to a subclass type, but only if the object really is an instance of that subclass (or an instance of a subclass of that subclass). This is referred to as downcasting (from a superclass type to a subclass type).
Student student=new Student();