Inheritance in Java

Inheritance in Java

exploreJava logo

Inheritance is a property of the Object-oriented programming(OOP) language in which one class inherits properties of another class. In Object-oriented programming language, the concept of inheritance is very important as it allows the creation of a hierarchy of classes. With the concept of inheritance, a general class with features common to several classes can be created. This general class is then inherited by other more specific classes, each specific class adding its unique features. In Java, the class being inherited is called a superclass, and the class inheriting a superclass is called a subclass. The subclass is a specialized version of the superclass which inherits the features of the superclass and also added its features/elements.

Advantages of Inheritance

  • Code-reusability: The existing code can be reused by new class, no need to define codes.
  • Readable: Helps to create small classes and understand the features of classes easy.

In Java, extends keyword is used to inherit superclass by subclass. In the code below, two classes A and B are created. A is a superclass and B is a subclass. A class can inherit only one super class, no multiple inheritance is allowed in Java. A has two integer variables i and j and a method to set values for i and j. Subclass B inherits A and has access to members of A. B has one integer variable k and methods to set values for i, j and k; method setijk of B has three parameters i, jj, and k. In this method keyword super is used to access a member of the superclass as the name of a parameter and the member variables are the same (i in A and i in setijk of B). Then those numbers as added. Another method setk() in class B is defined to set value for its member k; method addall() is defined in B which adds all the values.

class A{
int i,j;
public A(){}
public void setij(int i, int j){
this.i = i;
this.j = j;
System.out.println("In A, Sum: "+(i+j));
}
}
class B extends A{
int k,sum=0;
public void setijk(int i,int jj,int k){
super.i = i;
j = jj;
this.k = k;
sum = i + j + k;
System.out.println("i: "+i+" j:"+j+" k:"+k);
System.out.println("In B setijk, Sum: "+(i+j+k));
}
public void setk(int k){
this.k = k;
}
public void addall(){
sum = i+j+k;
System.out.println("i: "+i+" j:"+j+" k:"+k);
System.out.println("In B addall, Sum: "+sum);
}
}
class inherit_demo{
public static void main(String[] para){
A a = new A();
a.setij(3,4);
B b = new B();
b.setij(44,55);
b.setijk(4,5,6);
b.setk(1);
b.addall();
}
}

Output


In A, Sum: 7 In A, Sum: 99 i: 4 j:5 k:6 In B setijk, Sum: 15 i: 4 j:5 k:1
In B addall, Sum: 10

super keyword

super keyword is used to access the members of the superclass from the subclass; super is only used to access the constructor of the immediate superclass; super with dot(.) operator is used to access members of the superclass. The super keyword is used with dot(.) operator to access the member of the superclass when the member of the superclass is hidden by the subclass, that is the members of the superclass and subclass are made the same.

In the program given below person is a superclass and student is a subclass; the person has two member variables, overloaded constructors and show method. The student has one constructor and a show method; in the constructor of the student four parameters and passed, two are passed to the constructor of the superclass using super keyword and two are set to the specific member variables of the student. Similarly, in the show method of student class super is used to call a show of superclass person. Here, with the use of super, duplication of code is reduced. The subclass does not have to set the members of its superclass separately and display those superclass members separately.

class person{
private String name;
private String mobilenumber;
public person(String n, String mn){
name = n;
mobilenumber = mn;
}
public person(){
}
public void show(){
System.out.println("\nName: "+name+"\nMobile Number: "+mobilenumber);
}
}
class student extends person{
private String schoolname;
private int grade;
public student(String n, String mob, String sn, int grade){
super(n,mob);
schoolname = sn;
this.grade = grade; //this is used as the names of member variable and parameters are same.
}
public void show(){
super.show();
System.out.println("\nSchool Name: "+schoolname+"\nGrade: "+grade);
}
}
class inherit_demo_II{
public static void main(String[] inn){
student s = new student("Dhurba","9841064960","AAMW",10);
s.show();
}
}
Output:
Name: Dhurba
Mobile Number: 9841064960

School Name: AAMW
Grade: 10


Member Access and Inheritance in Java

class in Java

Post a Comment

Previous Post Next Post