Notes Inheritance in Java
Notes Inheritance in Java
Important terminology:
Super Class: The class whose features are inherited is known as superclass(or a
base class or a parent class).
Sub Class: The class that inherits the other class is known as a subclass(or a
derived class, extended class, or child class). The subclass can add its own fields
and methods in addition to the superclass fields and methods.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to
create a new class and there is already a class that includes some of the code that we
want, we can derive our new class from the existing class. By doing this, we are
reusing the fields and methods of the existing class.
How to use inheritance in Java
The keyword used for inheritance is extends.
Syntax :
class derived-class extends base-class
{
//methods and fields
}
class Teacher {
String designation = "Teacher";
String collegeName = "Beginners College";
void does(){
System.out.println("Teaching");
}
}
Beginners college
Teacher
Physics
Teaching
Based on the above example we can say that PhysicsTeacher IS-A Teacher. This means
that a child class has IS-A relationship with the parent class. This is inheritance is known
as IS-A relationship between child and parent class
Note:
The derived class inherits all the members and methods that are declared as public or
protected. If the members or methods of super class are declared as private then the
derived class cannot use them directly. The private members can be accessed only in its
own class. Such private members can only be accessed using public or protected getter
and setter methods of super class as shown in the example below.
class Teacher {
private String designation = "Teacher";
private String collegeName = "Beginnersbook";
public String getDesignation() {
return designation;
}
protected void setDesignation(String designation) {
this.designation = designation;
}
protected String getCollegeName() {
return collegeName;
}
protected void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
void does(){
System.out.println("Teaching");
}
}
In practice, inheritance and polymorphism are used together in java to achieve fast performance and
readability of code.
class Calculation {
int z;
Output
The sum of the given numbers:30
The difference between the given numbers:10
The product of the given numbers:200
In the given program, when an object to My_Calculation class is created, a copy of the contents
of the superclass is made within it. That is why, using the object of the subclass you can access
the members of a superclass.
is-a relationship
In Java, inheritance is an is-a relationship. That is, we use inheritance only if there
exists an is-a relationship between two classes. For example,
Car is a Vehicle
Orange is a Fruit
Surgeon is a Doctor
Dog is an Animal
Here, Car can inherit from Vehicle, Orange can inherit from Fruit, and so on.
Types of inheritance
There are five types of inheritance.
1. Single Inheritance
In single inheritance, a single subclass extends from a single superclass. For example,
In multilevel inheritance, a subclass extends from a superclass and then the same
subclass acts as a superclass for another class. For example,
3. Hierarchical Inheritance
4. Multiple Inheritance
Note: Java doesn't support multiple inheritance. However, we can achieve multiple
inheritance using interfaces.
5. Hybrid Inheritance
Output
One
Two
One
2. Multilevel Inheritance: In Multilevel Inheritance, a derived class will be inheriting a base class
and as well as the derived class also act as the base class to other class. In the below image, class A
serves as a base class for the derived class B, which in turn serves as a base class for the derived
class C. In Java, a class cannot directly access the grandparent’s members.
// Java program to illustrate the
// concept of Multilevel inheritance
class one {
public void print()
{
System.out.println("ISC");
}
}
class two extends one {
public void print_for() { System.out.println("Computer"); }
}
class three extends two {
public void print2()
{
System.out.println("Science");
}
}
// Driver class
class Main {
public static void main()
{
three ob = new three();
ob.print();
ob.print_for();
ob.print2();
}
}
Output
ISC
Computer
Science
Output
Class A
Class B
Class A
Class C
Class A
Class D
Hierarchical Inheritance
4. Multiple Inheritance (Through Interfaces): In Multiple inheritances, one class can have more
than one superclass and inherit features from all parent classes. Please note that Java
does not support multiple inheritances with classes. In java, we can achieve multiple inheritances
only through Interfaces. In the image below, Class C is derived from interface A and B.
// Java program to illustrate the
// concept of Multiple inheritance
import java.io.*;
import java.lang.*;
import java.util.*;
interface one {
public void print_1();
}
interface two {
public void print_2();
}
Output
Computer Science
With Java
class ParentClass{
//Parent class constructor
ParentClass(){
System.out.println("Constructor of Parent");
}
void disp(){
System.out.println("Parent Method");
}
}
class JavaExample extends ParentClass{
JavaExample(){
System.out.println("Constructor of Child");
}
void disp(){
System.out.println("Child Method");
//Calling the disp() method of parent class
super.disp();
}
public static void main(){
//Creating the object of child class
JavaExample obj = new JavaExample();
obj.disp();
}
}
The output is :
Constructor of Parent
Constructor of Child
Child Method
Parent Method
Following figure shows the four classes in this example and how they are related.
The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be
applied to them.
Visibility
Modifier Alpha Beta Alphasub Gamma
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
Java language has four access modifier to control access level for classes and
its members.
Protected: Protected has scope within the package and all sub classes