About java inheritance

Inheritance

Inheriting variables and methods that are the contents of a class to another class.

Features of object-oriented programming languages such as java, ruby, and python

The original class is the parent class (super class / base class) The inherited class is a child class (subclass / derived class)

--- Image of inheritance ---

Animal (parent class) variable ・ Name ・ Age Method ・ Eat ・ Walk

↓↓

Dog (child class) Additional methods ・ Bark (barking) ・ Run

human (child class) Additional methods ・ Talk ・ Work


As shown in the image above, ** Create multiple classes with common items and properties ** Sometimes use inheritance.


//Animal class to be the parent class
public class Animal{
  public String name;  //name
  public int age; //age
  public void eat(String something) {
      //eat
     System.out.println(something + "I ate");
  }
  public void walk(int a){ 
     //walk
     System.out.println(a.toString(); + "I walked a meter");
  }
}


//A dog class created by inheriting the animal class
public class Dog extends Animal {
  //bark
  public void bark(){
    System.out.println("one");
  }
 //Run
 public void run() {
 }
}

//A human class created by inheriting the animal class
public class Human extends Animal {
  //Speak
 public void talk(String tk){
   System.out.println("「" + tk + "」");
 }
 //I do the work
 public void work(){
 }
}

Benefits of using inheritance

1. The number of programming lines is reduced, which saves time and effort.

If you don't use inheritance

//Dog class made without inheriting animal class
public class Dog {
  public String name;  //name
  public int age;//age

  public void eat(String something) {
      //eat
      System.out.println(something + "I ate");
  }
  public void walk(int a){ 
     //walk
     System.out.println(a.toString(); + "I walked a meter");
  }
  //bark
  public void bark(){
    System.out.println("one");
  }
 //Run
 public void run() {
 }
}

//Human class created without inheriting animal class
public class Human extends Animal {
  public String name;  //name
  public int age;//age

 public void eat(String something) {
      //eat
      System.out.println(something + "I ate");
  }
  public void walk(int a){ 
     //walk
     System.out.println(a.toString(); + "I walked a meter");
  }

  //Speak
 public void talk(String tk){
   System.out.println("「" + tk + "」");
 }
 //I do the work
 public void work(){
 }
}

Variables and methods of the parent class are duplicated in both child classes. ❌ Furthermore, if you want to change the parent class, you have to change it in two places. ❌ If you combine the parent classes into one, you can change it in one place. ⭕️

2. Classes of the same inheritance source can be handled together

For example, when you want to put all animals on a truck, you can treat them as variables as long as they inherit the same class of animals.


//An array that stores objects of type animal class
Animal[] animals;
//Both dogs and humans can be put together
animals = { new Dog(); new Human()};

Inheritance features

You can use the fields (variables, methods) of the parent class in the child class

Animal names and eats also have dogs. In other words, the function of the parent class is invisible, but it also has the child class.

Child class can overwrite (override) method of parent class

You can overwrite the inherited method. This allows the same method to have different functions. However, the arguments need to be aligned.

Inheritance can be inherited (grandchild class)

You can create a grandchild class with a child class as a parent.


public class Animal {

}

public class Dog extends Animal {

}

pubic class WhiteDog extends Dog {
  public String color = "white";
}

The above creates a white dog as a grandchild class

Notes on inheritance

Do not overwrite fields that use final as the parent class

If final is added, the method cannot be overwritten. It is used when you do not want to change the process without permission.

Two classes cannot be inherited at the same time

class Tuna extends Animal, Fish  

The above is trying to inherit animals and fish to tuna but can't ❌

Constructor is not inherited and is executed implicitly

[* What is a constructor](https://www.javadrive.jp/start/constructor/index1.html#:~:text=%E3%82%B3%E3%83%B3%E3%82%B9%E3 % 83% 88% E3% 83% A9% E3% 82% AF% E3% 82% BF% E3% 81% A8% E3% 81% AF% E3% 80% 81% E3% 82% AF% E3% 83 % A9% E3% 82% B9% E3% 81% 8B% E3% 82% 89,% E3% 81% AE% E4% B8% BB% E3% 81% AB% E8% A1% 8C% E3% 81% 84% E3% 81% BE% E3% 81% 99% E3% 80% 82 & text =% E3% 83% A1% E3% 83% B3% E3% 83% 90% E5% A4% 89% E6% 95% B0 % E3% 81% AE% E5% AE% 9A% E7% BE% A9% E6% 99% 82% E3% 81% AB,% E3% 81% 93% E3% 81% A8% E3% 81% 8C% E5% 87% BA% E6% 9D% A5% E3% 81% BE% E3% 81% 9B% E3% 82% 93% E3% 80% 82) A method that is automatically executed when an object is created from a class


public Dog extends Animal {
  //constructor
  Dog(){
  }
}

public Dog extends Animal {
  //constructor
  Dog(){
    super();
  }
}

The top and bottom codes have the same meaning. If you try to override the constructor in a child class, the parent class's constructor is also implicitly executed. Even if the child class does not have a constructor, the parent class's constructor is executed.

Recommended Posts

About java inheritance
About inheritance (Java Silver)
About inheritance
[Java] Inheritance
Java inheritance
Java inheritance
java (inheritance)
About Java interface
[Java] About Java 12 features
[Java] Class inheritance
[Java] About arrays
About class inheritance.
Something about java
Where about java
About Java features
About Java threads
About Java arrays
About interface, java interface
About List [Java]
About java var
About Java literals
About Java commands
About Java log output
About Java functional interface
[Java] Overload / Override / Inheritance
Java, about 2D arrays
About class division (Java)
About [Java] [StreamAPI] allMatch ()
About Java StringBuilder class
[Java] About Singleton Class
Summarize Java inheritance (Java Silver 8)
About Java method binding
[Java] About anonymous classes
About method splitting (Java)
[Java Silver] About initialization
About Java Array List
About Java Polymorphism super ()
About Java String class
About Java access modifiers
About encapsulation and inheritance
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
[Java] About enum type
All about Java programming
[Java] Implicit inheritance memo
Java learning memo (inheritance)
About java abstract class
A note about Java GC
Advanced inheritance abstract, interface -java
What I researched about Java 8
About an instance of java
JAVA learning history interface inheritance
What I researched about Java 6
Inheritance
[Gradle] About Java plug-in tasks
Inheritance
What I researched about Java 9
About Java class loader types
[Java Silver] About equals method