I always read it when I explain it, so I summarize it
** What systematically separates and organizes commonalities and differences **
--Mechanism for grouping common parts of a class into a parent class --Defining properties common to parent classes and properties unique to child classes ――One of the mechanisms to eliminate code duplication and create highly versatile parts --One of the three major object-oriented elements --Represents the relationship between the whole set and the subset (superset and subset in set theory)
** Explain the classification system **
--For example, "animals", "humans" and "birds" --"Birds" in "Animal" (upper) --"Human" (lower) is one of "animals" -"Bird" is one of "animals" (lower)
** You have defined all the variables and methods of the parent class just by declaring inheritance **
--Established as a method for each superclass and subclass --For example, "Animal", "human" and "bird" ―― "Animal" "eats" both "human" and "bird", but "human walks" and "bird flies" ―― "Animal" "eats" both "human" and "bird", but "human does not fly" and "bird can walk ..." (this time it is not possible to walk) --Define only common properties in the same class
Animal.java
public abstract class Animal {
public void eat() {
System.out.println("Pakupaku");
}
}
Human.java
public class Human extends Animal {
public void walk() {
System.out.println("Teku Teku");
}
}
Bird.java
public class Bird extends Animal {
public void fly() {
System.out.println("Bass");
}
}
Main.java
public class Main {
public static void main(String[] args) {
Bird bird = new Bird();
bird.eat();
bird.fly();
Bird human = new human();
human.eat();
human.walk();
}
}
--Since inheritance is declared (extends) in the child class, when the child class is instantiated (new), the method of the parent class can be used even if the child class does not have a method (the eat method can be called). ――Inheritance is also one of the functions to realize polymorphism.
Summary of why we make it object-oriented
Akira Hirasawa (Author) Why make it object-oriented, 2nd edition https://amzn.to/2VSrzwe
By the way, birds can walk. Sometimes birds can't fly.
If you try to apply this to the real world, it will be that, so there is no problem in terms of specifications. Since the "requirement" that we want to meet in the "technical aspect" this time is "explanation that the parent method can be called after child generation", there is no problem in terms of specifications that the "bird" does not walk.
Recommended Posts