Introduction to Inheritance
Overview
Inheritance ensures Reusability
Example of Inheritance
What is actually Inherited?
Overloading Vs. Overriding
1
Inheritance ensures reusability
One of the good features of Java is reusability. That is once a class
is defined, it can be used in any application where its method are
required.
Java further extends the notion of re-usability through inheritance.
If we need to write a class and if there is an existing class that
almost satisfies our requirements, then we do not need to start from
scratch. We fully take advantage of the existing class by writing a
class that provides only the additional or modified requirements.
The class we write is called an extension, child or sub-class of the
existing class.
The existing class is called parent, ancestor, super or base class.
2
Example of Inheritance
Consider the following Student class:
class Student {
private long id;
private String name;
private double gpa;
public Student(long id, String name, double gpa) {
this.id = id;
this.name = name;
this.gpa = gpa;
}
public Student() {
this(999999, "No name", 0.0);
}
public void changeGPA(double newGPA) {
gpa = newGPA;
}
public double getGPA() {
return gpa;
}
public void print() {
System.out.print(id+" "+name+ " "+gpa);
}
}
3
Example of Inheritance (Cont’d)
Class TestStudent {
public static void main (String[] args) {
Student s1 = new Student();
s1.print();
Student s2 = new Student(991234,"Ahmed", 3.45);
s2.changeGPA(3.75);
s2.print();
}
}
Now suppose we wish to write another class to represent Research Assistant.
Research Assistant is also a student but with the additional field for work
load.
Instead of writing another class from the scratch, we extend the Student class
as follows.
4
Example of Inheritance (Cont’d)
class ResearchAssistant extends Student {
private int workLoad; // in hours
ResearchAssistant(long id, String name, double gpa,int workLoad){
super(id, name, gpa);
this.workLoad = workLoad;
}
ResearchAssistant() {
super();
workLoad = 0;
}
public void print() {
super.print();
System.out.print(" " + workLoad);
}
}
class TestReserchAssistant {
public static void main (String[] args) {
ResearchAssistant s1 = new ResearchAssistant();
s1.print();
5
Example of Inheritance (Cont’d)
ResearchAssistant s2 =
new ResearchAssistant(991234, "Ahmed",3.45,15);
s2.changeGPA(3.75);
s2.print();
}
}
The super reference can be used to refer to the parent class. Like this, it can be used
in dual form – with a dot to access a member (method or field) or without dot to call a
constructor.
When super is used to call the parent’s constructor, it must be the first statement in
the subclass’s constructor.
If a subclass constructor does not explicitly call the parent’s constructor, the compiler
automatically inserts a call to the parent’s default constructor.
If the parent class does not have a default constructor and none of the other
constructor is called, the compiler reports an error.
6
What is actually inherited?
Non-private fields and methods of the parent class are
automatically inherited by the sub-class except those that are
overridden by the subclass.
If a method is re-defined in the subclass with the same signature
as that in the parent class, it is said to override the method in the
parent class.
Constructors are not inherited, but they can be invoked in the
subclass’s constructor as seen in the example.
Giving a field in a class the same name as a field in an ancestor
hides the ancestor's field. The field exists, but cannot be accessed
by its simple name. If has to be called using the super keyword.
7
Overloading vs. Overriding
Overloading deals with multiple methods in the same class
with the same name but different signatures
Overriding deals with two methods, one in a parent class and
one in a child class, that have the same signature
Overloading lets you define a similar operation in different
ways for different data
Overriding lets you define a similar operation in different
ways for different object types
8
Another Example
The following example further illustrates the idea of inheritance:
class Employee {
private String name;
private double salary;
public Employee(String n, double s) {
name = n;
salary = s;
}
public String toString() {
return "Name: " + name + ", Salary=" + salary;
}
}
class Manager extends Employee {
private String department;
public Manager(String n, double s, String d) {
super(n, s);
department = d;
}
public String toString() {
return super.toString() + ", Department=" + department;
}
}
9
Another Example (cont’d)
public class Problem4 {
public static void main(String[] args) {
Employee e = new Employee("Ibrahim", 65000);
Manager m = new Manager(“Ali", 85000, "Engineering");
System.out.println(e);
System.out.println(m);
}
}
10