[JAVA] "Inheritance" that even beginners of object-oriented programming can understand
Introduction
Do you know the three major elements of object-oriented programming?
-
- Class (encapsulation)
-
- Inheritance
-
- Polymorphism
In this article, I will explain one of the three major elements, "inheritance."
Polymorphism is explained in the article here.
What is inheritance?
In a nutshell, it's a mechanism for organizing commonalities and differences between objects (classes).
This is the so-called "is-a relationship".
The is-a relationship means A is a B., which means "A is a type of B" in Japanese.
Also, in the concept of mathematics, it is the relationship between "whole set and subset".
(Example)
- Mammals are a type of animal (Fish and amphibians are also a type of animal)
- Strawberries are a member of the Rosaceae family (Sakura, Ume, and Peach are also members of the Rosaceae family)
Supplement
@shiracamus commented that "superclass is the minimum set of common functions".
Superclass and subclass
- What is a superclass?
- It is the whole set.
- In the above example, "animal" and "Rosaceae" correspond to superclasses.
- Defines common properties in the whole set.
- What is a subclass?
- A subset.
- In the example above, "mammals" and "strawberry" correspond to subclasses.
- Defines unique properties in subsets.
Why is inheritance important?
- This is to eliminate waste of the program. In other words, program creation efficiency and maintainability are improved.
- For example, suppose class A, class B, and class C have the same method.
- When you need to modify that method, you have to modify it in three places: class A, class B, and class C.
- The above is useless work! (Man-hours for program modification and test implementation will be required)
- If you define a function (method) that you want to have in a superclass, you can give the same function to all subclasses that inherit the superclass.
- The Object class, which is a superclass of all classes, has a toString method defined for stringification.
- This is an overview that also leads to "polymorphism".
Specific programming example of inheritance
Program overview
- This is a rock-paper-scissors program.
- Rock-paper-scissors players are humans and robots.
- Players are superclasses, and people and robots are subclasses.
- The player behavior (rock-paper-scissors janken method) is standardized in the superclass.
- Both humans and robots can play rock-paper-scissors (you can call the janken method).
Super class
Player.java
public class Player {
public void janken() {
//Rock-paper-scissors processing
}
}
Subclass
Format: subclass name + extends + superclass name
Person.java
public class Person extends Player {
//abridgement
}
Robot.java
public class Robot extends Player {
//abridgement
}
Main class
MainClass.java
public class MainClass {
public static void main(String[] args) {
Person person = new Person();
Robot robot = new Robot();
//Rock-paper-scissors
person.janken();
robot.janken();
}
}
Summary
What is inheritance?
- A mechanism for organizing commonalities and differences between classes.
- Super class
- Whole set
- Classes that define commonalities
- Is-a Class corresponding to "B" in relation (A is a B.)
- Subclass
- Subset
- Classes that define the differences
- Is-a Class corresponding to "A" in relation (A is a B.)
Why is inheritance important?
- This is to eliminate waste of the program.
- This is to reduce the cost of modifying and testing the program. This is a very important point of view in practice.
References
- Why make it object-oriented, 2nd edition
If you want to learn more about object-oriented programming, I recommend you to read it.
You can also buy it on Amazon.