Skip to content

Java Inheritance

2026-03-29 11:15

Tags: #java

Author: Duke Hsu


java_inheritance

Key Concepts

  • Inheritance is the process where one class acquires
  • Acquires the attributes and methods of another.

Real Life Code Example

Main Class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
 * 
 */
package prjInteritance;

/**
 * Java Inheritance 
    2026-03-29 11:15
    Tags: #java 
    Author:  Duke Hsu
 */

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {

        //create a object 
        Car car= new Car();


        //inheritance parent class attributes and method 
        car.start();
        car.brand= "Toyota";
        car.color = "Silver";
        car.speed = 120;
        System.out.println("Car details: \nBrand: "+ car.brand +" \nColor: "+ car.color+" \nSpeed: "+ car.speed);

        //sub class unique(owns) attributes  and method
        System.out.println();
        System.out.println("Sub class unique attributes:");

        System.out.println("Car door: " + car.numDoors);
        System.out.println();

        car.openTrunk();
        System.out.println();
        System.out.println();


        //***********************************//

        //create a object 
        Motorcycle honda = new Motorcycle();

        //inheritance parent class attributes and method
        honda.start();
        honda.brand = "Honda";
        honda.color = "Red";
        honda.speed = 60;
        System.out.println("Motorcycle details: \nBrand: "+ honda.brand +" \nColor: "+ honda.color+" \nSpeed: "+ honda.speed);

        //sub class owns attributes and method 
        System.out.println("*************************");
        System.out.println("Motorcycle PlateNum:  "+honda.plateNum);
        honda.popWheelie();


    }

}

Parent Class

Vehicle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * 
 */
package prjInteritance;

/**
 * 
 */
public class Vehicle {

    //class vehicle attributes
    double speed;
    String color;
    String brand;


    //class vehicle methods
    void start() {

        System.out.println("This Vehicle is start!");
    }

    void move() {
        System.out.println("This Vehicle is move!~~");
    }

    void stop() {
        System.out.println("This Vehicle is stopped!~");
    }

}

Sub class

Car and Motorcycle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * 
 */
package prjInteritance;

/**
 * 
 */
//use extends keyword to inheritance parents class Vehicle 
//vehicle is parents class, car is sub class
public class Car extends Vehicle {

    //sub class unique attributes 
    int numDoors = 4;
    int car_wheel = 4; 

    void openTrunk() {

        System.out.println("The car trunk was opened!!");
    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
/**
 * 
 */
package prjInteritance;

/**
 * 
 */

public class Motorcycle extends Vehicle {

    boolean hasSidecar = false;
    int plateNum = 22581;

    void popWheelie() {

        System.out.println("The motorcycle is doing a wheelie right now! haha that’s crazy!");

    }
}

Output

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
This Vehicle is start!

Car details:

Brand: Toyota

Color: Silver

Speed: 120.0



Sub class unique attributes:

Car door: 4



The car trunk was opened!!





This Vehicle is start!

Motorcycle details:

Brand: Honda

Color: Red

Speed: 60.0

*************************

Motorcycle PlateNum: 22581

The motorcycle is doing a wheelie right now! haha that’s crazy!

References