Learn Java Tutorial for Beginners (Video), Part 22: Inheritance

A tutorial on the important topic of inheritance in Java -- one of the fundamental building blocks of OOP (Object Oriented Programming). (Apologies for the dodgy sound on this video).

When the video is running, click the maximize button in the lower-right-hand corner to make it full screen.



Code for this tutorial:

App.java:

 

public class App {

    public static void main(String[] args) {
        Machine mach1 = new Machine();
        
        mach1.start();
        mach1.stop();
        
        Car car1 = new Car();
        
        car1.start();
        car1.wipeWindShield();
        car1.showInfo();
        car1.stop();
        
        
    }

}

 

Machine.java:

 

public class Machine {
    
    protected String name = "Machine Type 1";
    
    public void start() {
        System.out.println("Machine started.");
    }
    
    public void stop() {
        System.out.println("Machine stopped.");
    }
}

 

Car.java:

 


public class Car extends Machine {
    
    
    @Override
    public void start() {
        System.out.println("Car started");
    }

    public void wipeWindShield() {
        System.out.println("Wiping windshield");
    }
    
    public void showInfo() {
        System.out.println("Car name: " + name);
    }
}

 
Machine started.
Machine stopped.
Car started
Wiping windshield
Car name: Machine Type 1
Machine stopped.