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(){
}
}
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. ⭕️
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()};
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.
You can overwrite the inherited method. This allows the same method to have different functions. However, the arguments need to be aligned.
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
If final is added, the method cannot be overwritten. It is used when you do not want to change the process without permission.
class Tuna extends Animal, Fish
The above is trying to inherit animals and fish to tuna but can't ❌
[* 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