inheritance. In protest arranged programming, inheritance empowers new questions go up against the properties of existing items. A class that is utilized as the reason for legacy is known as a superclass or base class. A class that acquires from a superclass is known as a subclass or inferred class.

Important terminology:


Super Class: The class whose highlights are acquired is known as super class(or a base class or a parent class).

Sub Class: The class that acquires alternate class is known as sub class(or an inferred class, broadened class, or tyke class). The subclass can include its own particular fields and strategies notwithstanding the superclass fields and techniques.

Reusability: Inheritance underpins the idea of "reusability", i.e. when we need to make another class and there is as of now a class that incorporates a portion of the code that we need, we can get our new class from the current class. By doing this, we are reusing the fields and techniques for the current class.

What is inheritance? to know about this question let’s see an example.

package studywebsite;

public class human
{
    public int age;
    public String name;
 public human() {

        this.age=0;
        this.name="";
    }
    public void age()
    {
        System.out.println("AGE is "+this.age);
    }
}


package studywebsite;

public class student extends human
{
    public static void main(String[] args)
    {
        student obj=new student();
        obj.age();
    }
}
There are two classes and human and student you can see that with student class there is a keywork used “extends” it is a keyword which is used to extend a class with other class.

By extending the class you can use all the attributes of parent classes in child class.
In this case human is parent class and student is child class.

Example Lectures

Example 7.1
Example 7.2
Example 7.3
Example 7.4
Example 7.5


Next Lecture                                                                                                                   Previous Lecture