0
0

More than 3 years have passed since last update.

Java 継承(inheritance)

Posted at

1,継承(inheritance)とは

あるクラスが他のクラスから属性(attributes)メソッド(methods)を取得するプロセスです

2,ソースコード

Main.java
public class Main {

    public static void main(String[] args) {

        // inheritance = the process where one class acquires,
        //               the attrivutes and methods of another.

        Car car = new Car();
        car.go();

        Bicycle bike = new Bicycle();
        bike.stop();

        System.out.println(car.speed);
        System.out.println(bike.speed);

        System.out.println(car.doors);
        System.out.println(bike.pedals);

    }

}

継承元👇

Vehicle.java
public class Vehicle {

    double speed;

    void go() {
        System.out.println("This vehicle is moming");
    }

    void stop() {
        System.out.println("This vehicle is stopped");
    }

}

クラスからクラスへ継承する場合、”子クラス extends 親クラス” と表記します👇

Car.java
public class Car extends Vehicle {

    int wheels = 4;
    int doors = 4;

}
Bicycle.java
public class Bicycle extends Vehicle {

    int wheels = 2;
    int pedals = 2;

}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0