"Inheritance" that even beginners of object-oriented programming can understand

Introduction

Do you know the three major elements of object-oriented programming?

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)

Supplement

@shiracamus commented that "superclass is the minimum set of common functions".

Superclass and subclass

Why is inheritance important?

Specific programming example of inheritance

Program overview

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?

Why is inheritance important?

References

If you want to learn more about object-oriented programming, I recommend you to read it. You can also buy it on Amazon.

Recommended Posts

"Inheritance" that even beginners of object-oriented programming can understand
Polymorphism that even beginners of object-oriented programming can understand
Explaining "object-oriented" that even elementary school students can understand in 5 levels
Scala's Implicit that even Java shops can understand
[JQuery] A guy that even beginners could understand well
Summary of object-oriented programming using Java
JSP + Eclipse + Jetty development environment construction that even Java beginners can do
I tried learning Java with a series that beginners can understand clearly
Object-oriented that can be understood by fairies
Object-oriented programming
Awareness of object-oriented programming for half a year
Understand while reading the article! Summary of contents that Elasticsearch beginners want to suppress
How to use Java lambda expressions
[For beginners] About lambda expressions and Stream API
Use Java lambda expressions outside of the Stream API
"Inheritance" that even beginners of object-oriented programming can understand
Polymorphism that even beginners of object-oriented programming can understand
Understand Java 8 lambda expressions
About Java lambda expressions
Explain Java 8 lambda expressions
Have fun programming with lambda expressions and a fluid interface