INHERITANCE IN JAVA!

In Java, a class that act as a base class is also called super class. The class that inherits the properties and methods of a base class is called a sub class. A sub class is a specialized version of a super class. It inherits all of the instance variables and methods of the super class and add its own, unique elements.
The ‘extends’ keyword:
Inheritance can be implemented in Java using the ‘extends’ keyword. Use the keyword extends next to the class name of the sub class, followed by the name of the base class. The general form of the class that inherits a super class is shown here:
class subclass_name extends superclass_name
{
// body of the class
}
During inheritance a sub class can extend the functionality of only one super class, i.e., we can specify only one super class for any sub class that extends. Although a sub class inherits all the members of its super class, it can’t access the private members of the super class. Following is an example program for inheritance in Java:
n this example, we have two classes: one is Box and another one is ColorBox. Box is a super class and the ColorBox is a sub class. Box provides three instance variables (width, height and depth) and two methods (a constructor and volume). The ColorBox is a type of Box that adds another instance variable color.
Through inheritance, the new class ColorBox contains four instance variables (width, height, depth and color) and three methods (two constructors and the method volume). As all the members of the super class are public by default, they can be inherited and accessed freely by the sub class ColorBox.
If the instance variables (width, height and depth) are declared as private members, they can’t be directly accessed though they are inherited by the sub class. They can be accessed only through the methods of the base class. The following is an example for using the private members of a super class from a sub class:
In this example, the instance variables of the base class (width, height and depth) are declared under private scope. So, these variables are inherited by the sub class ColoBox without having permission to access them directly. The only way to access the private members of the super class from a sub class is through the methods of the super class.
Java doesn’t support the inheritance of multiple super classes for creating a sub class. Instead, it allows multilevel inheritance, in which a sub class becomes a super class of another sub class. Therefore, a reference variable of a super class can be assigned a reference to any of the object of its sub class. But, a reference variable of a sub class can’t hold a reference to the object of its super class.