Java inheritance

Inheritance

Taking over the fields and methods of an existing class to another class. You can customize the new inherited classes by adding your own fields and methods. The inherited class is called a "superclass", and the new class that can be inherited is called a "subclass". When defining a new subclass using inheritance, use "extends" to define the class.

class subclass name extends superclass name{
}

Inherit the superclass with ** extends superclass name **.

[Example] It is assumed that the contents of Bicycle.java and Car.java are duplicated.

Main.java


class Main {
  public static void main(String[] args) {
~abridgement~
 }
}

Bicycle.java


class Bicycle extends Vehicle {

}

Car.java


class Car extends Vehicle{
  
}

Vehicle.java


class Vehicle {
~abridgement~//Bicycle.java and car.Duplicate part of java
}

Superclass method invocation

Subclasses inherit the fields and methods of superclasses. You can call a superclass instance method on a subclass instance. [Example] Continue from the above

Main.java


class Main {
  public static void main(String[] args) {
    Car car = new Car();
    car.setName("Ferrari");  //I'm specifying the name of the car
    car.setColor("Red");  //I specify the color of the car
    
    Bicycle bicycle = new Bicycle();
    bicycle.setName("Bianchi");  //Specify the name of the bicycle
    bicycle.setColor("Green");  //Specify the color of the bicycle
    
    System.out.println("[Car information]");
    car.printData();

    System.out.println("=================");
    System.out.println("[Bicycle information]");
    bicycle.printData();
  }
}

Subclass fields and methods

Subclass instances can also call methods defined in their class and superclass methods. Superclass instances, on the other hand, cannot call subclass methods. For an instance of a subclass, the called method is called from the subclass if it is defined in the subclass, or from the superclass if it is not defined. [Example] Continue from the above

Main.java


class Main {
  public static void main(String[] args) {
    Car car = new Car();
~abridgement~
    int litre = scanner.nextInt();
    car.charge(litre);
~abridgement~
    bicycle.printData();
  }
}

Car.java


class Car extends Vehicle{
  public void charge(int litre) {
    System.out.println(litre + "L refuel");
    if (litre <= 0) {
      System.out.println("Cannot refuel");
    } else if (litre + this.fuel >= 100) {
      System.out.println("Refuel until full");
      this.fuel = 100;
    } else {
      this.fuel += litre;
    }
    System.out.println("Gasoline amount:" + this.fuel + "L");
  }
}

override

By defining a method with the same name as the method inherited from the superclass in the subclass, the contents of the superclass method can be overridden, which is called ** override **. When you call a method on an instance of a subclass, it first looks for that method in the subclass and calls that method if you have one. In other words, if there is a method with the same name as the superclass in the subclass, it will be executed, and as a result, the contents of the method will be overwritten. [Example] Continue from the above

Car.java


class Car extends Vehicle{
  public void charge(int litre) {

  public void printData() {
  System.out.println("name:" + this.getName());
  System.out.println("color:" + this.getColor());
  System.out.println("Mileage:" + this.getDistance() + "km");
  System.out.println("Gasoline amount:" + this.getFuel() + "L");
  }
~abridgement~
}

I'm omitting it, but Vehicle. Overwriting the contents of java. Vehicle. Delete the part where the contents of java are duplicated. Use "super. Method name ()" to erase. With "super. method name ()", you can call the instance method of the superclass from the instance method of the subclass. [Example] Continue from the above

Car.java


class Car extends Vehicle{
  public void charge(int litre) {

  public void printData() {
  super.printData();  //I'm erasing the duplicated part
  System.out.println("Gasoline amount:" + this.getFuel() + "L");  //This is not duplicated
  }
~abridgement~
}

Next, define the constructor in a subclass. Use "super ()" to call the superclass constructor. [Example] Continue from the above (call name and color with "super ()")

Bicycle.java


class Bicycle extends Vehicle
  Bicycle(String name, String color) {  //Defines a constructor for the Bicycle class
    super(name, color);   //I'm using super to call the superclass constructor
  }
}

Car.java


class Car extends Vehicle{

  Car(String name, String color) {
    super(name, color);
  }
~abridgement~
}

Vehicl.java


class Vehicle {
  private String name;
  private String color;
  private int distance = 0;
  
  Vehicle(String name, String color) { //Defines the constructor for the Vehicle class
    this.name = name;
    this.color = color;
  }
~abridgement~
}

Recommended Posts

Java inheritance
Java inheritance
java (inheritance)
[Java] Class inheritance
About java inheritance
[Java] Overload / Override / Inheritance
Inheritance
Inheritance
Summarize Java inheritance (Java Silver 8)
About inheritance (Java Silver)
Java
Java
[Java] Implicit inheritance memo
Java learning memo (inheritance)
Java starting from beginner, inheritance
[Java] What is class inheritance?
java (inheritance of is-a principle)
Java learning (0)
Studying Java ―― 3
[Java] array
Muscle Java Object Oriented Day 2 ~ Inheritance ~
[Java] Annotation
[Java] Module
Java array
Java tips, tips
Java methods
Java method
java (constructor)
About inheritance
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
java (array)
Java static
Java serialization
JAVA paid
Java (set)
java shellsort
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
Java features
java beginner 3
Java memo
java (encapsulation)
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes