Inheritance PDF
Inheritance PDF
Super Class: The class whose features are inherited is known as super class(or a base
class or a parent class).
Sub Class: The class that inherits the other class is known as sub class(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.
Example: In below example of inheritance, class Bicycle is a base class, class MountainBike
is a derived class which extends Bicycle class and class Test is a driver class to run program.
filter_none
edit
play_arrow
brightness_4
// base class
class Bicycle
{
// the Bicycle class has two fields
public int gear;
public int speed;
// derived class
class MountainBike extends Bicycle
{
}
// driver class
public class Test
{
public static void main(String args[])
{
}
}
Output:
No of gears are 3
speed of bicycle is 100
seat height is 25
In above program, when an object of MountainBike class is created, a copy of the all methods
and fields of the superclass acquire memory in this object. That is why, by using the object of
the subclass we can also access the members of a superclass.
Please note that during inheritance only object of subclass is created, not the superclass. For
more, refer Java Object Creation of Inherited Class.
Illustrative image of the program:
In practice, inheritance and polymorphism are used together in java to achieve fast
performance and readability of code.
class one
{
public void print_geek()
{
System.out.println("Geeks");
}
}
Geeks
for
Geeks
class one
{
public void print_geek()
{
System.out.println("Geeks");
}
}
class two extends one
{
public void print_for()
{
System.out.println("for");
}
}
// Drived class
public class Main
{
public static void main(String[] args)
{
three g = new three();
g.print_geek();
g.print_for();
g.print_geek();
}
}
Output:
Geeks
for
Geeks
class one
{
public void print_geek()
{
System.out.println("Geeks");
}
}
// Drived class
public class Main
{
public static void main(String[] args)
{
three g = new three();
g.print_geek();
two t = new two();
t.print_for();
g.print_geek();
}
}
Output:
Geeks
for
Geeks
interface one
{
public void print_geek();
}
interface two
{
public void print_for();
}
// Drived class
public class Main
{
public static void main(String[] args)
{
child c = new child();
c.print_geek();
c.print_for();
c.print_geek();
}
}
Output:
Geeks
for
Geeks
Default superclass: Except Object class, which has no superclass, every class has one
and only one direct superclass (single inheritance). In the absence of any other explicit
superclass, every class is implicitly a subclass of Object class.
Superclass can only be one: A superclass can have any number of subclasses. But a
subclass can have only one superclass. This is because Java does not support multiple
inheritance with classes. Although with interfaces, multiple inheritance is supported
by java.
Inheriting Constructors: A subclass inherits all the members (fields, methods, and
nested classes) from its superclass. Constructors are not members, so they are not
inherited by subclasses, but the constructor of the superclass can be invoked from the
subclass.
Private member inheritance: A subclass does not inherit the private members of its
parent class. However, if the superclass has public or protected methods(like getters
and setters) for accessing its private fields, these can also be used by the subclass.
In sub-classes we can inherit members as is, replace them, hide them, or supplement them
with new members:
The inherited fields can be used directly, just like any other fields.
We can declare new fields in the subclass that are not in the superclass.
The inherited methods can be used directly as they are.
We can write a new instance method in the subclass that has the same signature as the
one in the superclass, thus overriding it (as in example above, toString() method is
overridden).
We can write a new static method in the subclass that has the same signature as the
one in the superclass, thus hiding it.
We can declare new methods in the subclass that are not in the superclass.
We can write a subclass constructor that invokes the constructor of the superclass,
either implicitly or by using the keyword super.