Java Inheritance

Inheritance

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.
The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class,
you can reuse methods and fields of parent class, and you can add new methods and fields also.

class Subclass-name extends Superclass-name
{
//methods and fields
}

////////////////////////////////
class Super {
…..
…..
}
class Sub extends Super {
…..
…..
}

The extends keyword indicates that you are making a new class that derives from an existing class.
In the terminology of Java, a class that is inherited is called a super class. The new class is called a subclass.
In simple and easy words , inheritance mean accessing the function or method , data members (variables etc). of another class. To use this feature we should have to follow the syntax.
suppose we have a CLASS A and we have another CLASS B and if we want to use the function of CLASS A in CLASS B so we can use inheritance. We just write an syntax in our program Class B extends A , which means that we can now access the data members and function of the Class A

Inheritance program
class Employee
{
float salary=40000.00;
}
class Programmer extends Employee
{
int bonus=10000;
public static void main(String args[])
{
Programmer p = new Programmer();
System.out.println(“Programmer salary is:”+p.salary);
System.out.println(“Bonus of Programmer is:”+p.bonus);
}
}

So let me explain this example so you can understand that how it works.
We have two classes CLASS Employee and CLASS Programmer, CLASS employee has data member (variable) salary .
So if we want to use this variable into CLASS programmer (if we ain’t want to define it in class programmer) we can inheritance this CLASS. SO we use the syntax or keyword EXTENDS (class programmer extends employee)
so CLASS programmer has access to the data of CLASS employee.
now Create an object to the CLASS programmer which is P now this can use the data of CLASS employee. (p.salary)
A subclass inherits all the members (fields, methods, and nested classes) from its superclass.

Super keyword
Super Keyword is used to differentiate the data members of superclass from the data members of subclass, if they have same names. It is used to invoke the superclass constructor from subclass.
super.variable
super.method();

Suppose we have one variable int num = 20; in class A and we have another same name variable in class B , So when we extends the class A in Class B so they have same name of variables so super keyword is used to identify or make the access of super class . Now if we want to access variable of Class A which is num = 20; then we use super.num to access the variable of subclass or class B we need to create an object of the class B so we can access it.

class A
{
int num = 20;
}
class B extends A
{
int num = 20;
pubic static void main(string args[])
B sub = new B();
System.out.println(“this is the value of class A : “+super.num);
System.out.println(“this is the value of class B : “+sub.num);
}

To use constructor of super class we can use super();

The next phase of inheritance is its types.
The types defines how we can access the features of Inheritance.

inheritance
credit to tutorialpoint.com

Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is not supported in java.

BACK

NEXT