Inheritance
Navigate Classes and Objects topic: ) |
Inheritance is one of the most powerful mechanisms of the Object Oriented Programming. It allows the reuse of the members of a class (called the superclass or the mother class) in another class (called subclass, child class or the derived class) that inherits from it. This way, classes can be built by successive inheritance.
In Java, this mechanism is enabled by the extends
keyword.
Example:
Code listing 4.9: Vehicle.java
public class Vehicle {
public int speed;
public int numberOfSeats;
}
|
Code listing 4.10: Car.java
public class Car extends Vehicle {
public Car() {
this.speed = 90;
this.numberOfSeats = 5;
}
}
|
In the Code listing 4.10, the class Car
inherits from Vehicle
, which means that the attributes speed
and numberOfSeats
are present in the class Car
, whereas they are defined in the class Vehicle
. Also, the constructor defined in the class Car
allows to initialize those attributes.
In Java, the inheritance mechanism allows to define a class hierarchy with all the classes. Without explicit inheritance, a class implicitly inherits from the Object
class. This Object
class is the root of the class hierarchy.
Some classes can't be inherited. Those classes are defined with the final
keyword. For instance, the Integer
class can't have subclasses. It is called a final class.
The Object
class
[edit | edit source]At the instantiating, the child class receives the features inherited from its superclass, which also has received the features inherited from its own superclass and so on to the Object
class.
This mechanism allows to define reusable global classes, whose user details the behavior in the derived more specific classes.
In Java, a class can only inherit from one class. Java does not allow you to create a subclass from two classes, as that would require creating complicated rules to disambiguate fields and methods inherited from multiple superclasses. If there is a need for Java to inherit from multiple sources, the best option is through interfaces, described in the next chapter.
The super keyword
[edit | edit source]The super
keyword allows access to the members of the superclass of a class, as you can use this
to access the members of the current class.
Example:
Code listing 4.11: Plane.java
public class Plane extends Vehicle {
public Plane() {
super();
}
}
|
In this example, the constructor of the Plane
class calls the constructor of its superclass Vehicle
. You can only use super
to access the members of the superclass inside the child class. If you use it from another class, it accesses the superclass of the other class.
This keyword also allows you to explicitly access the members of the superclass, for instance, in the case where there is a method with the same name in your class (overriding, ...).
Example :
Code listing 4.12: Vehicle.java
public class Vehicle {
// ...
public void run() throws Exception {
position += speed;
}
}
|
Code listing 4.13: Plane.java
public class Plane extends Vehicle {
// ...
public void run() throws Exception {
if (0 < height) {
throw new Exception("A plane can't run in flight.");
} else {
super.run();
}
}
}
|
Question 4.1: Consider the following classes.
Question 4.1: Class1.java
public class Class1 {
public static final int CONSTANT_OF_CLASS_1 = 9;
public int myAttributeOfClass1 = 40;
public void myMethodOfClass1(int i) {
}
}
|
Question 4.1: Class2.java
public class Class2 extends Class1 {
public int myAttributeOfClass2 = 10;
public void myMethodOfClass2(int i) {
}
}
|
Question 4.1: Class3.java
public class Class3 {
public static final int CONSTANT_OF_CLASS_3 = 9;
public void myMethodOfClass3(int i) {
}
}
|
Question 4.1: Question1.java
public class Question1 extends Class2 {
public static final int CONSTANT = 2;
public int myAttribute = 20;
public void myMethod(int i) {
}
}
|
List all the attributes and methods that can be accessed in the class Question1
.
CONSTANT_OF_CLASS_1
myAttributeOfClass1
myMethodOfClass1(int)
myAttributeOfClass2
myMethodOfClass2(int)
CONSTANT
myAttribute
myMethod(int)
Question1
inherits from Class1
and Class2
but not from Class3
.
- See also the Object Oriented Programming book about the inheritance concept.