Java Video Tutorial 15

Java Video Tutorial 15In this part of the Java Video Tutorial I cover interfaces, abstract classes, abstract methods and more.

You need interfaces and abstract classes because Java doesn’t allow you to inherit from more than one other class. You would use an interface when you want to force the user of the interface to create every method in the interface.

You use an abstract method when you need some flexibility. You’ll learn more in the video below.

If you like videos like this share it

Code from the Video

DRIVABLE.JAVA

/* Java doesn't allow you to inherit from more than one 
 * class. So, what do you do when you want do you do
 * when you want to add additional functionality?
 * You create an interface. Interfaces are empty 
 * classes. They provide all of the methods you must
 * use, but none of the code.
 */

// This is how you define an interface. They normally
// have a name that is an adjective. Adjectives modify
// nouns. Most objects have noun names
public interface Drivable {
	
	// You can put fields in an interface, but understand 
	// that their values are final and can't be changed
	double PI = 3.14159265;
	
	// All methods in an interface must be implemented
	// They are public and abstract by default
	// An abstract method must be defined by any class 
	// that uses the interface
	int getWheels();
	
	// You can't define a method as final and abstract
	// final means the method can't be changed and 
	// abstract means it must be changed
	void setWheels(int numWheels);
	
	double getSpeed();
	
	void setSpeed(double speed);
	
	
	
}

CRASHABLE.JAVA

/* If you want to create a class in which every method
 * doesn't have to be implemented use abstract classes.
 */

// Create an abstract class with the abstract keyword
public abstract class Crashable{
	
	boolean carDrivable = true;
	
	public void youCrashed(){
		this.carDrivable = false;
	}
	
	public abstract void setCarStrength(int carStrength);
	
	public abstract int getCarStrength();
	
}

VEHICLE.JAVA

/* You define that you want a class to use an interface
 * with the implements keyword. This class must create
 * a method for each method defined in Drivable
 * You can implement more than 1 interface like this
 * public class Vehicle implements Drivable, Crashable
 */
// You make a class part of an abstract class by using 
//the extends keyword
public class Vehicle extends Crashable implements Drivable {
	
	int numOfWheels = 2;
	double theSpeed = 0;
	
	int carStrength = 0;
	
	// All methods must be as visible as those in the 
	// interface. If they are public in the interface
	// they must be public in the subclass
	public int getWheels(){
		return this.numOfWheels;
	}
	
	public void setWheels(int numWheels){
		this.numOfWheels = numWheels;
	}
	
	public double getSpeed(){
		return this.theSpeed;
	}
	
	public void setSpeed(double speed){
		this.theSpeed = speed;
	}
	
	public Vehicle(int wheels, double speed){
		this.numOfWheels = wheels;
		this.theSpeed = speed;
	}
	
	public void setCarStrength(int carStrength){
		this.carStrength = carStrength;
	}
	
	public int getCarStrength(){
		return this.carStrength;
	}
	
}

LESSONFIFTEEN.JAVA

public class LessonFifteen {
	
	public static void main(String[] args){
		
		Vehicle car = new Vehicle(4, 100.0);
		
		// Using methods from the interface Drivable
		System.out.println("Cars Max Speed: "+car.getSpeed());
		System.out.println("Cars Number of Wheels: "+car.getWheels());
		
		// Using methods from abstract method Crashable
		car.setCarStrength(10);
		System.out.println("Car Strength: "+car.getCarStrength());
		
	}
	
}

31 Responses to “Java Video Tutorial 15”

  1. Lucky says:

    Derek, thanks again for this video, i am just curious how u completed ur Computer programming, i mean are u a self taught programmer if so how much time it took u to reach the level ur now, i know it is kind of personal question, but do answer plz thank you.

    • Derek Banas says:

      I’m mainly book taught, but since I started in the 80s that was the only option. I think anyone can get very good in this day if they focus on object oriented design and uml. Then they can easily learn any oop language like java and c++.

      Feel free to ask questions

  2. Lucky says:

    When r you going to upload videos on android development or game development using JAVA

  3. Mohit says:

    You are a good teacher. I am learning at wonderful pace 🙂

  4. Abhishek Gaur says:

    You are a great teacher and inspiration. I wish some day I would be like you, sharing knowledge to world free of cost

  5. John says:

    I am trying to get this to work with the interface and I get this error. What am I doing wrong?

    Exception in thread “main” java.lang.Error: Unresolved compilation problem:
    The type Drivable cannot be a superinterface of Vehicle; a superinterface must be an interface

    at Vehicle.(Vehicle.java:2)
    at LessonFifteen.main(LessonFifteen.java:6)

  6. Jake says:

    Hello,
    The topic of interfaces really seems to bog me down, I can’t seem to get my head to click around it. To me, it seems like you are implementing the interface, which defines certain methods which must be used in your class. But, you are also defining them in the class which implements them. Why write the same code twice? Is it simply to make sure that a sort of ‘checklist’ is fulfilled or am I looking at this from the wrong angle?

    Thanks for great videos

  7. sasuke says:

    thanks a million for your tutorials and code!!!
    Can you recommend or say which book you read and think they are good enough?
    thanks

  8. sathees says:

    It is very help full video. Thank you very much.

  9. badshah says:

    which book should i use to learn java object oriented programming.

  10. Ray says:

    Hey what is the difference between using the interface and the abstract class I am not sure, seems like you can use either or. Can you give me an example in which you’d use one over the other sorry to be a bother.

    • Derek Banas says:

      With an interface every method must be abstract meaning it can’t do anything at all except force the class that implements it to create that method.

      An abstract class can have non-abstract methods in it that contain code. That is the main difference. Over time you’ll find you rarely use abstract classes, but they are useful when you need them.

  11. Giovanni says:

    Hi Derek, I really didn’t understand what you did with Cat tempCat = (Cat) randAnimal; is this a new class? could you please share a link where it is explained thoroughly?

  12. dipti says:

    Your tutorials are really helpful !

  13. Edward says:

    thanks Derek, all your tutorials are good. i’ve learned a lot.

  14. Yusuf says:

    Hi Derek,
    The best explanation of interfaces I have seen anywhere. You have made many of these concepts that are often explained in very confusing ways, very easy to understand.

  15. Rashul Rajbhandari says:

    Hello Derek. Thanks for the tutorials. I am learning a lot. 🙂

    I want to ask you if we can put two vehicles classes inside the lessonfifteen.java class

    For example can i create another class-> Vehicle carTwo = new Vehicle(5, 200.0); inside the class and print out the output respectively?

    Thanks

Leave a Reply

Your email address will not be published.

Google+