Inheritance 2
Inheritance 2
The process by which one class acquires the properties(data members) and functionalities(methods) of
another class is called inheritance. The aim of inheritance is to provide the reusability of code so that a
class has to write only the unique features and rest of the common properties and functionalities can be
extended from the another class.
Child Class:
The class that extends the features of another class is known as child class, sub class or derived class.
Parent Class:
The class whose properties and functionalities are used(inherited) by another class is known as parent
class, super class or Base class.
Inheritance is a process of defining a new class based on an existing class by extending its common data
members and methods.
Inheritance allows us to reuse of code, it improves reusability in your java application.
Note: The biggest advantage of Inheritance is that the code that is already present in base class need not
be rewritten in the child class.
This means that the data members(instance variables) and methods of the parent class can be used in
the child class as.
If you are finding it difficult to understand what is class and object then refer the guide that I have
shared on object oriented programming: OOPs Concepts
To inherit a class we use extends keyword. Here class XYZ is child class and class ABC is parent class. The
class XYZ is inheriting the properties and methods of ABC class.
Inheritance Example
class Teacher {
void does(){
System.out.println("Teaching");
System.out.println(obj.collegeName);
System.out.println(obj.designation);
System.out.println(obj.mainSubject);
obj.does();
Output:
Beginnersbook
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 {
return designation;
this.designation = designation;
return collegeName;
this.collegeName = collegeName;
void does(){
System.out.println("Teaching");
}
public class JavaExample extends Teacher{
*/
System.out.println(obj.getCollegeName());
System.out.println(obj.getDesignation());
System.out.println(obj.mainSubject);
obj.does();
Beginnersbook
Teacher
Physics
Teaching
The important point to note in the above example is that the child class is able to access the private
members of parent class through protected methods of parent class. When we make a instance
variable(data member) or method protected, this means that they are accessible only in the class itself
and in child class. These public, protected, private etc. are all access specifiers and we will discuss them
in the coming tutorials.
Types of inheritance
To learn types of inheritance in detail, refer: Types of Inheritance in Java.
Single Inheritance: refers to a child and parent class relationship where a class extends the another class.
Multilevel inheritance: refers to a child and parent class relationship where a class extends the child
class. For example class C extends class B and class B extends class A.
Hierarchical inheritance: refers to a child and parent class relationship where more than one classes
extends the same class. For example, classes B, C & D extends the same class A.
Multiple Inheritance: refers to the concept of one class extending more than one classes, which means a
child class has two parent classes. For example class C extends both classes A and B. Java doesn’t
support multiple inheritance, read more about it here.
Hybrid inheritance: Combination of more than one types of inheritance in a single program. For example
class A & B extends class C and another class D extends class A then this is a hybrid inheritance example
because it is a combination of single and hierarchical inheritance.
constructor of sub class is invoked when we create the object of subclass, it by default invokes the
default constructor of super class. Hence, in inheritance the objects are constructed top-down. The
superclass constructor can be called explicitly using the super keyword, but it should be first statement
in a constructor. The super keyword refers to the superclass, immediately above of the calling class in
the hierarchy. The use of multiple super keywords to access an ancestor class other than the direct
parent is not permitted.
class ParentClass{
ParentClass(){
System.out.println("Constructor of Parent");
JavaExample(){
*/
System.out.println("Constructor of Child");
new JavaExample();
Output:
Constructor of Parent
Constructor of Child
When we declare the same method in child class which is already present in the parent class the this is
called method overriding. In this case when we call the method from child class object, the child class
version of the method is called. However we can call the parent class method using super keyword as I
have shown in the example below:
class ParentClass{
ParentClass(){
System.out.println("Constructor of Parent");
void disp(){
System.out.println("Parent Method");
JavaExample(){
System.out.println("Constructor of Child");
void disp(){
System.out.println("Child Method");
//Calling the disp() method of parent class
super.disp();
obj.disp();
The output is :
Constructor of Parent
Constructor of Child
Child Method
Parent Method
-
3685
Share
When a class is declared, with its specification, and the other sub-class members
will want to use its member methods(functions); that’s when they will go for
inheritance concepts. Inheritance concepts are everywhere in Java programming.
Inheritance in Java is a concept that acquires the properties from one class to other
classes; for example, the relationship between father and son.
In Java, a class can inherit attributes and methods from another class. The class
that inherits the properties is known as the sub-class or the child class. The class
from which the properties are inherited from is know as the superclass or the parent
class.
In Inheritance, the properties of the base class is acquired by the derived classes.
Inheritance Syntax in Java:
1 class derived_class extends base_class
2 {
3 //methods
4 //fields
}
5
5 }
7 {
The extends keyword extends a class and is an indicator that a class is being
inherited by another class. When you say class B extends a class A, it means that
class B is inheriting the properties(methods, attributes) from class A. Here, class A
is the superclass or parent class and class B is the sub-class or child class.
Program Example:
1 class Base
2 {
10 {
12 }
}
13
class Test
14
{
15
16
public static void main(String[] args)
17
{
18
Derived d = new Derived(); // creating object
19
d.M1(); // print Base Class Method
20
d.M2(); // print Derived Class Method
21
}
22 }
23
Output:
Derived Class/Sub-class: Derived class is a class that inherits from a base class. It
is also known as subclass or child class.
Base Class/Superclass: The base class is the main class where derived classes
inherit the features. It is also known as the superclass or parent class.
Reusability: The name itself says reuse the repeated code in the programs. It is a
mechanism to reuse existing code when you are creating new classes.
Single Inheritance
Multiple Inheritance
Multi-Level Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Single Inheritance
In Inheritance, we can access superclass methods and variables. We can also access
subclass methods and variables through subclass objects only. We have to take care
of superclass and subclass methods, and variable’s names shouldn’t conflict.
Program Example:
1 class A
2 {
int a, b;
3
void display()
4
{
5
System.out.println(“Inside class A values =”+a+” ”+b);
6
}
7
}
8 class B extends A
9 {
10 int c;
11 void show()
12 {
20 obj.b=20;
21 obj.c=30;
22 obj.display();
obj.show();
23
}
24
25
26 }
27
Output:
Java developers want to use multiple inheritances in some cases. Fortunately, Java
developers have interface concepts expecting the developers to achieve multiple
inheritances by using multiple interfaces.
We can take an example of three classes, class Vehicle, class Car and class SUV.
Here, the class Vehicle is the grandfather class. The class Car extends class
Vehicle and class SUV extends class Car.
Hierarchical Inheritance in Java:
In Hierarchical Inheritance in Java, more than one derived class extend a single
base class. In simple words, more than one child class extend a single parent class
or a single parent class has more than one child classes.
For example, consider a parent class Car. Now, consider child classes Audi, BMW
and Mercedes. In Hierarchical Inheritance in Java, class Audi, class BMW and
class Mercedes, all these three extend class Car.
Hybrid Inheritance in Java:
Why? Because we clearly observe that there is two kinds of inheritance here-
Hierarchical and Single Inheritance.
3 {
11 {
13 }
14 }
class Inh_In_Java
15
{
16
public static void main(String[] args)
17
{
18
Parent p=new Parent();
19
p.M1();
20 p.M2(); // error-wrong way to call derived class method
21 }
22 }
23
Output:
2 {
10 {
12 }
}
13
class Inh_In_Java
14
{
15
public static void main(String[] args)
16
17 {
19 p.M1();
20 p.M2();
21 }
}
22
23
Output:
2 {
9 {
11 {
19 p.M1();
20 p.M2();
21 }
}
22
23
Output:
4. From the above three examples, we understand that inheritance will be useful
when derived class objects call base class(parent class or superclass) methods and
variables. It will not throw an error.
Inheritance Example in Java-4:
1
2 class Parent
3 {
11 {
13 }
}
14
class Inh_In_Java
15
{
16
public static void main(String[] args)
17
{
18
Child p=new Child();
19
p.M1();
20 p.M2();
21 }
22 }
23
Output:
If the child class has the same method in its implementation as that of its parents
class, then the concept of method overriding comes into play.
In method overriding, the child class has the same method as that of the parent
class. The main use of this is to achieve runtime polymorphism.
Methods must share the same name in child and parent class.
It must have the same parameter as in the superclass.
There must be an IS-A type of Inheritance.
2 {
int i =20;
3
void display()
4
{
5
System.out.println(“Superclass display method”);
6
}
7
}
8 class Subclass extends Superclass
9 {
10 int i = 100;
11 void display()
12 {
super.display();
13
System.out.println(“Subclass display method”);
14
System.out.println(“ i value =”+i);
15
System.out.println(“superclass i value =”+super.i);
16
}
17
}
18 class SuperUse
19 {
21 {
24 obj.display();
25 }
26 }
27
Output: