Muscle Java Object Oriented Day 2 ~ Inheritance ~

What is inheritance?

--Inheriting the definition of the class --Use as is without redefining method fields: Convenient

Inheritance in Java

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

Image of inheritance

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

Inheritance term

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

Inheritance code example: How to define

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

}

Inheritance code example

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

"Super" keyword

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

Muscle Java Object Oriented Day 2 ~ Inheritance ~
Muscle Java Object Oriented Day 1
Muscle Java Basics Day 1
Java Day 2018
[Java] Inheritance
Java inheritance
Java inheritance
java (inheritance)
[Java] Class inheritance
Java learning day 5
Java Day Tokyo 2017
Oracle Java Day 2017
About java inheritance
Oracle Java Day 2017
[Java] Object class
java learning day 2
java learning day 1
[Java] Overload / Override / Inheritance
JAVA object mapping library
Java Silver Study Day 1
Java Kuche Day memo
[Beginner] Java Object Oriented / Instance Field / Instance Method / Overload [Note 24]
Summarize Java inheritance (Java Silver 8)
Java object size feeling
About inheritance (Java Silver)
[Java] Implicit inheritance memo
Java learning memo (inheritance)
Java Object Serialization why & when
Advanced inheritance abstract, interface -java
Maybe this is object oriented
JAVA learning history interface inheritance
4th day of java learning
Java starting from beginner, inheritance
[Java] What is class inheritance?
java (inheritance of is-a principle)
Java is the 5th day
Kantai Collection Java # 0 What is Object Oriented Programming (OOP)? [For beginners]