--Inheriting the definition of the class --Use as is without redefining method fields: Convenient
--Extension: Add your own methods and fields in the inherited class (child class) --Redefinition (override): The inherited class (child class) can change the definition of the original method field.
--Children are stronger than parents --Master Roshi (parent class) and disciple Goku (child class) --Disciple Goku Kamehameha (parent's definition can be used as it is) + 3 times Kamehameha (override) + Maikujutsu (extension)
--Original class (inherited class) --Parent class --Super class --Inheritance source
--A class that inherits the original class --Child class --Subclass --Derived class --Inheritance class --Inheritance destination
--extended keyword --In the child class, you can use it without writing the fields and methods of the parent class. --Only one parent class can be specified
class Child class name extends Parent class name{
}
--Parent class
public class Animal{
  public void eat(String f){
    System.out.println(f+"Eat");
  }
  public void sleep(){
    System.out.println("Sleep");
  }
  public void wakeUp(){
    System.out.println("Get up");
  }
}
--Child class --Bird class can call sleep method and wakeUp method of parent class Animal class -①: Expansion -②: Override
public class Bird extends Animal{
  
  // ①:Expansion
  public static int wing = 2;
  pulic void fly(){
    System.out.println("Fly");
  }
  // ②:override(Overwrite)
    //Same method name as parent
  pulic void sleep(){
    System.out.println("Sleep on a tree");
  }
}
--Parent class definition can be used when overriding --Used when you want to access the methods and fields of the parent class from the methods of the overridden child class.
public class Bird extends Animal{
  pulic void sleep(){
    System.out.println("Sleep on a tree");
    
    // super
    super.sleep();
}
        Recommended Posts