Java for Complete 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.

 

Learn to program Java Swing with my complete video course – desktop programming and applets. Includes 7 free videos. Click here for details.

All pages on this site are copyright © 2013 John W. Purcell

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Posted in Java, Java Video Tutorials (Beginners) | Tagged , , , |