Java Inheritance
The Concept of Inheritance
Inheritance is a cornerstone of Java's object-oriented programming technology because it allows the creation of hierarchical classes.
Inheritance means a subclass inherits the characteristics and behaviors of its parent class, so that a subclass object (instance) has the instance fields and methods of the parent class, or the subclass inherits methods from the parent class, giving the subclass the same behavior as the parent class.
Inheritance in daily life:

Rabbits and sheep belong to the herbivore class, while lions and leopards belong to the carnivore class.
Herbivores and carnivores, in turn, belong to the animal class.
So the relationship that inheritance must satisfy is: is-a, where the parent class is more general and the subclass is more specific.
Although both herbivores and carnivores belong to animals, they differ in attributes and behaviors, so a subclass will have the general characteristics of the parent class as well as its own characteristics.
Class Inheritance Format
In Java, the extends keyword is used to declare that a class is inherited from another class, in the following general form:
Class Inheritance Format
class
Parent Class
{
}
class
Subclass
extends
Parent Class
{
}
Why Inheritance Is Needed
Next, we will illustrate this requirement through an example.
Develop an animal class, where the animals are penguins and mice, with the following requirements:
- Penguin: attributes (name, id), methods (eat, sleep, self-introduction)
- Mouse: attributes (name, id), methods (eat, sleep, self-introduction)
Penguin class:
public
class
Penguin
{
private
String
name
;
private
int
id
;
public
Penguin
(
String
myName
,
int
myid
)
{
name
=
myName
;
id
=
myid
;
}
public
void
eat
(
)
{
System
.
out
.
println
(
name
"
is eating
"
)
;
}
public
void
sleep
(
)
{
System
.
out
.
println
(
name
"
is sleeping
"
)
;
}
public
void
introduction
(
)
{
System
.
out
.
println
(
"
Hello everyone! I am
"
id
"
number
"
name
"
.
"
)
;
}
}
Mouse class:
public
class
Mouse
{
private
String
name
;
private
int
id
;
public
Mouse
(
String
myName
,
int
myid
)
{
name
=
myName
;
id
=
myid
;
}
public
void
eat
(
)
{
System
.
out
.
println
(
name
"
is eating
"
)
;
}
public
void
sleep
(
)
{
System
.
out
.
println
(
name
"
is sleeping
"
)
;
}
public
void
introduction
(
)
{
System
.
out
.
println
(
"
Hello everyone! I am
"
id
"
number
"
name
"
.
"
)
;
}
}
From these two code segments, we can see that the code is duplicated, resulting in a large and bloated codebase with low maintainability (mainly manifested as needing to modify a lot of code later, which is error-prone). To fundamentally solve the problems of these two code segments, we need inheritance: extract the common parts of the two code segments into a parent class:
Common parent class:
public
class
Animal
{
private
String
name
;
private
int
id
;
public
Animal
(
String
myName
,
int
myid
)
{
name
=
myName
;
id
=
myid
;
}
public
void
eat
(
)
{
System
.
out
.
println
(
name
"
is eating
"
)
;
}
public
void
sleep
(
)
{
System
.
out
.
println
(
name
"
is sleeping
"
)
;
}
public
void
introduction
(
)
{
System
.
out
.
println
(
"
Hello everyone! I am
"
id
"
number
"
name
"
.
"
)
;
}
}
This Animal class can serve as a parent class. After the penguin class and the mouse class inherit from it, they gain the attributes and methods of the parent class; the subclass will no longer have duplicated code, maintainability is improved, the code is cleaner, and code reusability is improved (reusability mainly means it can be used multiple times without rewriting the same code). Code after inheritance:
Penguin class:
public
class
Penguin
extends
Animal
{
public
Penguin
(
String
myName
,
int
myid
)
{
super
(
myName
,
myid
)
;
}
}
Mouse class:
public
class
Mouse
extends
Animal
{
public
Mouse
(
String
myName
,
int
myid
)
{
super
(
myName
,
myid
)
;
}
}
Types of inheritance
Note that Java does not support multiple inheritance, but it does support multilevel inheritance.

Characteristics of Inheritance
-
A subclass has the non-private attributes and methods of its parent class.
-
A subclass can have its own attributes and methods, meaning the subclass can extend the parent class.
-
A subclass can implement the parent class's methods in its own way.
-
Java's inheritance is single inheritance, but it can be multilevel inheritance. Single inheritance means a subclass can inherit only one parent class; multilevel inheritance means, for example, class B inherits from class A, and class C inherits from class B, so by relationship B is the parent of C and A is the parent of B. This is a characteristic that distinguishes Java inheritance from C++ inheritance.
-
It increases the coupling between classes (a disadvantage of inheritance: high coupling causes tighter connections between code, reducing code independence).
Inheritance Keywords
Inheritance can be implemented using the extends and implements keywords. All classes inherit from java.lang.Object. When a class uses neither of these two keywords, it inherits Object by default (this class is in java.lang package, so there is no need to import) ancestor class.
extends Keyword
In Java, class inheritance is single inheritance, meaning a subclass can have only one parent class, so extends can inherit only one class.
extends Keyword
public
class
Animal
{
private
String
name
;
private
int
id
;
public
Animal
(
String
myName
,
int
myid
)
{
//
Initialize attribute values
}
public
void
eat
(
)
{
//
specific implementation of the eat method }
public
void
sleep
(
)
{
//
specific implementation of the sleep method }
}
public
class
Penguin
extends
Animal
{
}
implements Keyword
Using the implements keyword can indirectly give Java the characteristics of multiple inheritance. It applies to classes implementing interfaces, and multiple interfaces can be implemented at the same time (interfaces are separated by commas).
implements Keyword
public
interface
A
{
public
void
eat
(
)
;
public
void
sleep
(
)
;
}
public
interface
B
{
public
void
show
(
)
;
}
public
class
C
implements
A
,
B
{
}
super and this Keywords
**super keyword:** We can use the super keyword to access members of the parent class, referring to the parent class of the current object.
**this keyword:** Points to its own reference, referring to the current object, i.e., the object instance to which its enclosing method or constructor belongs.
Example
class
Animal
{
void
eat
(
)
{
System
.
out
.
println
(
"
animal : eat
"
)
;
}
}
class
Dog
extends
Animal
{
void
eat
(
)
{
System
.
out
.
println
(
"
dog : eat
"
)
;
}
void
eatTest
(
)
{
this
.
eat
(
)
;
//
this calls its own method
super
.
eat
(
)
;
//
super calls the parent class method
}
}
public
class
Test
{
public
static
void
main
(
String
[
]
args
)
{
Animal
a
=
new
Animal
(
)
;
a
.
eat
(
)
;
Dog
d
=
new
Dog
(
)
;
d
.
eatTest
(
)
;
}
}
The output is:
animal : eat
dog : eat
animal : eat
final Keyword
final can be used to modify variables (including class attributes, object attributes, local variables, and parameters), methods (including class methods and object methods), and classes.
final means "final".
Declaring a class with the final keyword defines it as a final class that cannot be inherited, or it can be used to modify a method so that it cannot be overridden by a subclass:
-
Declare class:
final class 类名 {//类体} -
Declare method:
修饰符(public/private/default/protected) final 返回值类型 方法名(){//方法体}
Note: For a class defined with final, its attributes and methods are not final.
Constructor
A subclass does not inherit the constructor (construction method or constructor) of its parent class; it only calls it (implicitly or explicitly). If the parent class's constructor has parameters, the subclass constructor must explicitly call the parent class's constructor via the super keyword with an appropriate parameter list.
If the parent class constructor has no parameters, the subclass constructor does not need to use the super keyword to call the parent class constructor; the system will automatically call the parent class's no-argument constructor.
Example
class
SuperClass
{
private
int
n
;
//
No-argument constructor
public
SuperClass
(
)
{
System
.
out
.
println
(
"
SuperClass()
"
)
;
}
//
Parameterized constructor
public
SuperClass
(
int
n
)
{
System
.
out
.
println
(
"
SuperClass(int n)
"
)
;
this
.
n
=
n
;
}
}
//
SubClass class inheritance
class
SubClass
extends
SuperClass
{
private
int
n
;
//
No-argument constructor, automatically calls the parent class's no-argument constructor
public
SubClass
(
)
{
System
.
out
.
println
(
"
SubClass()
"
)
;
}
//
Parameterized constructor, calls the parameterized constructor in the parent class
public
SubClass
(
int
n
)
{
super
(
300
)
;
System
.
out
.
println
(
"
SubClass(int n):
"
n
)
;
this
.
n
=
n
;
}
}
//
SubClass2 class inheritance
class
SubClass2
extends
SuperClass
{
private
int
n
;
//
No-argument constructor, calls the parameterized constructor in the parent class
public
SubClass2
(
)
{
super
(
300
)
;
System
.
out
.
println
(
"
SubClass2()
"
)
;
}
//
Parameterized constructor, automatically calls the parent class's no-argument constructor
public
SubClass2
(
int
n
)
{
System
.
out
.
println
(
"
SubClass2(int n):
"
n
)
;
this
.
n
=
n
;
}
}
public
class
TestSuperSub
{
public
static
void
main
(
String
[
]
args
)
{
System
.
out
.
println
(
"
------SubClass class inheritance------
"
)
;
SubClass
sc1
=
new
SubClass
(
)
;
SubClass
sc2
=
new
SubClass
(
100
)
;
System
.
out
.
println
(
"
------SubClass2 class inheritance------
"
)
;
SubClass2
sc3
=
new
SubClass2
(
)
;
SubClass2
sc4
=
new
SubClass2
(
200
)
;
}
}
The output is:
------SubClass Class Inheritance------
SuperClass()
SubClass()
SuperClass(int n)
SubClass(int n): 100
------SubClass2 Class Inheritance------
SuperClass(int n)
SubClass2()
SuperClass()
SubClass2(int n): 200