. . .

Explain Inheritance with a suitable example?

Inheritance

Inheritance is the process by which objects. It means One class acquire the properties of objects of another class in the hierarchy. For example, the scooter is a type of class two-wheelers, which is again a type of (or kind of) the class motor vehicles. As shown in the Figure the principle behind it is that the derived class shares common characteristics with the class from which it is derived. New classes can be built from the existing classes.

It means that we can add additional features to an existing class without modifying it. The new class is referred to as a derived class or subclass and the original class is known as a base class or superclass. Therefore, the concept of inheritance provides the idea of reusability. This inheritance mechanism allows the programmer to reuse a class that is made almost but not exactly, similar to the required one by adding a few more features to it.

As shown in Figure, three classes have been derived from one base class. Feature A and Feature B of the base class are inherited in all the three derived classes. Also, each derived class has added its own features according to the requirement. Therefore, new classes use the concept of reusability and extend their functionality.

Inheritance
Figure

Now we will try to understand with the help of java programming

* inheritance samples  */

public class Two{
  static String str="Hello";
  static void print()
  {
    System.out.println(str);
  }
}

class TwoMain extends Two{
    public static void main(String args[])
    {
    print();
    }

}

Please share the article

Leave a Reply

Your email address will not be published. Required fields are marked *

fourteen − five =