Inheritance: is a mechanism for enhancing existing classes. If you need to implement a new class and a class representing a more general concept is already available, then the new class can inherit from the existing class. For example , suppose you need to define a class SavingsAccount to model an account that pays a fixed interest rate on deposit. You already have a class BankAccount, and a savings account is a special case of a bank account. In this case, it makes sense to use the language construct of inheritance. Here’s the syntax for the class definition.
Class SavingsAccount extends BankAccount
{
new methods;
new instance fields;
}
In the SavingsAccount class you specify only new methods and instance fields. the SavingsAccount class automatically inherits all methods and instance fields of the BankAccount Class.
when we want to invoke a superclass constructor from a subclass, we use the keyword super.
public Class SavingsAccount extends BankAccount
{
public SavingsAccount(initialBalance)
{
super(initialBalance);
}
}
When you form a subclass of a given Class, you can specify additional instance fields and methods.
1- You can override methods from the superclass. If you specify a method with the same signature, it override the same method of the superclass
2- You can inherit methods from the superclass. If you do not explicitly override a superclass method, you automatically inherit the method.
what follow is a sample code, in which you can see inheritance. There are two classes Employee (superclass) and the Manager (subclass). You may ask, why the Employee is the superclass?, well because there could be many kinds of Employee: Managers, Executives, HourlyWorkers, etc…., so in that case the classes Manager, Executives are subclasses of Employee.
Employee Class:
public class Employee { //variables private String employeeName; private double employeeSalary; //construc Employee that takes name and salary public Employee(String aname, double asalary) { employeeName = aname; employeeSalary = asalary; } //getters public String getEmployee() { return employeeName; } public double getSalary() { return employeeSalary; } @Override public String toString() { return "Employee[name="+this.getEmployee()+",salary="+this.getSalary()+"]"; } }
Manager Class:
public class Manager extends Employee { //variables private String mgrDepartment; //construc manager that takes name and department public Manager(String aname, double asalary, String adepartment) { super(aname, asalary); this.mgrDepartment = adepartment; } //getters public String getManager() { return super.getEmployee(); } public double getSalary() { return super.getSalary(); } public String getDeparment() { return mgrDepartment; } @Override public String toString() { return "Manager[super="+super.toString()+",department="+this.getDeparment(); } }
Notice how the super keywork is used in the Manager subclass to call the Employee construct, and the getSalary() method of the superclass, and finally @Override is used to override a method of the superclass that has the same method signature.