‫Welcome back.
‫In the previous video, we'll learn about the first type of abstraction in Java, which is the abstract
‫class.
‫In this video, we'll learn about the second type of abstraction, which is the interface.
‫An interface is like a contract or a set of rules that a class can promise to follow.
‫It defines a set of methods, signatures, methods without implementations that any class implementing
‫the interface must provide.
‫An interface provides a way to achieve abstraction and define a common behavior that multiple classes
‫can share in the vehicle class.
‫Here we define an abstract class, abstract methods and non abstract methods.
‫If we look at the stop method in the car, we can override it.
‫You can see that the car is not implementing the top method.
‫But think about if we need to implement all these methods.
‫Two four and force the classes and all the children of the vehicle to implement these methods because
‫all the vehicles should start, stop and brake differently.
‫So I'll create a new interface and I'll name it as vehicle interface.
‫You can see it's a public interface vehicle interface.
‫In this interface, I need to define the method signature for making like starting the vehicle stopping
‫and so on.
‫So all the methods that I should force, all the types of vehicles to implement, I should define them
‫in this interface.
‫So start engine, for example.
‫The second method is stop the engine.
‫The third method is accelerate and the fourth method is brake.
‫Let's move to the car now.
‫Let's remove the extends vehicle for now and I remove all of these methods and these errors and here
‫I'll implement the vehicle alt plus enter to implement the methods and we can see that we have four
‫methods that we should implement in this car class.
‫Click okay.
‫And here we have these four methods that are inherited from this abstract interface.
‫As you can see, by implementing the vehicle interface, the car class has agreed to follow the rules
‫defined in the vehicle interface.
‫So you can define now like, for example, stopping the engine accelerating with the engine and the
‫brake.
‫I'll implement the same with the truck and you can see I can implement the interface after the extending
‫of the class implements vehicle interface alt plus enter, implement the methods and we can see that
‫we have three because brake method is ready.
‫And let me remove the brake.
‫You can see that there is an error here because the vehicle contains an abstract void.
‫I remove this for now and I'll keep this in the brake for the vehicle interface.
‫And similarly, here in the truck class, I'll define these messages, the starting the truck's engine,
‫stopping the truck's engine and accelerating and applying the brake with the truck.
‫Now let's move to the Y class.
‫And here I need to define a car because car is now not extending from the vehicle.
‫V one dot start engine v two dot start engine or start because it's inherited from the vehicle and I
‫need to change the the vehicle here to a truck in order to use the methods that are implemented from
‫the interface.
‫The vehicle didn't implement the vehicle interface while the truck implemented, so I need to use the
‫truck instead of vehicle here.
‫V two dot accelerate.
‫Let me run and you can see that starting the engine with V one, which is the car that truck starts
‫and accelerating with the truck interfaces are very useful in Java as they enable multiple inheritance,
‫allowing a class to implement multiple interfaces and inherit behavior from different sources.
‫A very important note that in Java there is only one mono extend from the classes.
‫So for example extends vehicle here and I can't create, for example, that extends from a class called
‫machine because multiple inheritance using extends is not allowed in Java.
‫While we can make a trick to make it happen in Java.
‫The multiple inheritance by creating multiple interface so I can create implements many interfaces,
‫for example machine interface and other interfaces.
‫So multiple inheritance is achieved in Java using the interfaces.