Explain the Terms used in Inheritance in java?
What is Super Keyword ?
Explain inheritance with it's types in java?
Explain types of inheritance in java ?
Inheritance in Java:-
- Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
- Sub Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods. Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
- Super Class: The class whose features are inherited is known as superclass(or a base class or a parent class). Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
- Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class. As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
Student name is: Saurabh
Roll_no is: 1
Invoking Superclass Constructor
If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass. But if you want to call a parameterized constructor of the superclass, you need to use the super keyword as shown below.
super(values);
The super keyword
The super keyword is similar to this keyword. Following are the scenarios where the super keyword is used.
It is used to differentiate the members of superclass from the members of subclass, if they have same names.
It is used to invoke the superclass constructor from subclass.
- Single Inheritance:
- Multilevel Inheritance
- Hierarchical Inheritance:
- Hybrid Inheritance(Through Interfaces):
- Multiple Inheritance (Through Interfaces):
- Single Inheritance:-
Single inheritance
in java
- Multilevel Inheritance :-