About inheritance

image.png

I always read it when I explain it, so I summarize it

What is inheritance?

** 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)

Feature

Conceptual aspect (real world)

** 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)

Technical aspect (programming)

** 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

When using "abstraction" in programming technology

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();
    }
}

point

--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.

Related article

Summary of why we make it object-oriented

Book information

Akira Hirasawa (Author) Why make it object-oriented, 2nd edition https://amzn.to/2VSrzwe

Miscellaneous feelings

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

About inheritance
About class inheritance.
About Ruby inheritance
About java inheritance
Inheritance
Inheritance
About inheritance (Java Silver)
About encapsulation and inheritance
About =
About Kotlin
About attr_accessor
About Hinemos
About params
About Docker
About Rails 6
About form_for
About Spring ③
[Java] Inheritance
About enum
About polymorphism
About Optional
About hashes
About JitPack
About Dockerfile
Java inheritance
About this ()
About devise
About encapsulation
Java inheritance
About Docker
About JAVA_HOME
About active_hash
About static
About exceptions
About object-oriented inheritance and about yield Ruby
Advanced inheritance
About scope
java (inheritance)
[Maven] About Maven
Review about bundler
About Java interface
[Java] About Java 12 features
About Rails routing
[Java] Class inheritance
About cyclomatic complexity
About exception handling
About AsyncTask Generics
About Ruby symbols
About array multiplication
[Java] About arrays
About HotSpot VM
About ruby ​​form
[Swift] About generics
About Active Storage
About Spring AOP
About Ruby Hashes
About singular methods
About getter setter
About build tools
About MacinCloud introduction
[Note] About nil