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