Introduction
A class that is derived from another class is
called a subclass (also a derived class, extended class, or child class). The
class from which the subclass is derived is called a superclass (also a base
class or a parent class).
A subclass is a class that derives from another
class. A subclass inherits state and behavior from all of its ancestors. The
term superclass refers to a class's direct ancestor as well as all of its
ascendant classes
Java Inheritance (Subclass and Superclass)
In Java, it is possible to inherit attributes and
methods from one class to another. We group the "inheritance concept"
into two categories:
subclass (child) - the class that inherits from
another class
superclass (parent) - the class being inherited
from
To inherit from a class, use the extends keyword.
Super and this
"this" is a reference to the current object within an instance method or a contractor. "this" is used to refer to any member of the current object from within a constructor or an instance method
"super" can be used to call a constructor from the superclass and each class in Java programming is allowed to have one direct superclass
public class Personnel {
private String empNAme;
private String empno;
private int Age ;
public Personnel(){}
public Personnel(String no, String NAme, int Age){
this.empNAme = no;
this.empno = NAme;
this.Age = Age;
}
public String isempName() {
return empNAme;
}
public void setempName(String empName) {
this.empNAme = empName;
}
public String getEmId() {
return empno;
}
public void setEmId(String EmId) {
this.empno = EmId;
}
public int getAge() {
return Age;
}
}
public class Allowance extends Personnel{
private double basic;
public Allowance(String no, String na , int age) {
super(no, na, age);
this.basic=78000;
}
public Allowance(String no, String na , int age,
double basic){
super(no, na, age);
this.basic=basic;
}
public double getbasic() {
return basic;
}
public void setbasic(double basic) {
this.basic = basic;
}
}
public class Payroll {
public static void main(String[] args) {
Allowance emprec = new Allowance("e001",
"David Brown", 47);
System.out.println("Employee name : " +
emprec.isempName());
System.out.println("Employee No " +
emprec.getEmId());
System.out.println("Employee Age " +
emprec.getAge() + " legs.");
System.out.println("Basic Salary " +
emprec.getbasic());
}
}
OutPut
Employee name : e001
Employee No David Brown
Employee Age 47 legs.
Basic Salary 78000.0
Programming Explanation
Superclass and subclass
Personnel Class is Superclass or the parent class
to Allowance Class. The keywords extends after Allowance has made Allowance
class a child to subclass
Allowance now inherits state and behavior from the
Personnel
this
In the example above program in setempName() method, Personnel() method and setEmId() method of the Personnel Class uses this.empNAme , this.empno,and this.Age to access the instance variables within the current object
Super
In this example, the class Allowance extends the class Personnel. By calling super(no, na, age) in the Allowance constructor, we invoke the constructor of the Personnel class and pass the brand parameter. This ensures that the brand property of the Personnel class is properly initialized.
Conclusion
In conclusion, "super" and "this" are useful in Java inheritance for referring to the superclass and the current instance, respectively. It allows the initialization of superclass properties and access to superclass variables, and also avoidance of code duplication in constructors.
While “extends” used to indicate that the class which is being defined is derived from the base class using inheritance. It extends the functionality of the parent class to subclass.
Reference
Eck, D. J. (2019). Introduction to programming
using Java, version 8.1. Hobart and William Smith College. http://math.hws.edu/javanotes.