‫Hello, my friends, and welcome back.
‫In this video, we're going to learn about a very important concept of object oriented programming,
‫which is abstraction.
‫Abstraction allows us to hide unnecessary details and only show the needed information.
‫This allows us to manage complexity by hiding the details.
‫A practical example of abstraction can be the vehicles we can in the car apply, start or stop methods.
‫But we're not going to deeply dive into the working mechanism of starting and stopping the engine of
‫this vehicle.
‫The major advantage of hiding the working of the start and stop mechanism is that the manufacturer can
‫implement the stop and start from different vehicles like cars, trucks and so on.
‫Let's take an example that helps us to better understand Java abstraction to create an abstract class,
‫you need to use the abstract keyword so you can see the abstract keyword is before the class public
‫abstract class vehicle.
‫Now, the vehicle class won't allow you to create an instance from it.
‫So if we go to the main here, if I create a vehicle V three equal to new vehicle, you can see I have
‫an error and it's not a concrete class like this.
‫I need to replace it with one of its children, like the car or the truck back to the vehicle.
‫I create an abstract method called break.
‫Whenever you create an abstract method, you don't define any body.
‫But what's the usage of this abstract method?
‫Let's move to the car and truck.
‫Siblings of the vehicle and understand the usage of the abstract method.
‫Here we have an error because if we hover the mouse here over this red line, we can see that the car
‫class must either be declared abstract or implement the abstract method Break in vehicle.
‫The abstract method here break should be implemented in the car so alt+ enter.
‫Or you can simply implement the method and click okay.
‫You can see that we override it.
‫The break.
‫In this case we are forcing the class car to implement the break method and if you notice that the stop
‫method will not be implemented and we didn't force the car method to implement it.
‫But whenever you define an abstract, you should implement it in the children classes.
‫Now.
‫Now I'll create here System.out.print.
‫I'm going to print applying mechanical break whenever I call the break method.
‫Let's move to the truck and you can see truck also has an error.
‫We need to implement the break method.
‫Click okay to implement it.
‫And here I am applying hydraulic and this type of breaks is used in the trucks.
‫Car will apply the mechanical break while the truck will apply the hydraulic break and you can see that
‫there is a different implementation of breaks.
‫So all the subclasses of vehicle class would have different implementation of a break method.
‫Let's test this program here.
‫I created V one as a car and v two as a truck.
‫Also, whenever we declare a method as an abstract, we can't create objects of this abstract class.
‫In this example, either we create a car or a vehicle and this is the polymorphism concept.
‫Let's start by v one and v two starting the engine of v one and the v two vehicles and v one dot apply
‫the break and v two dot break.
‫Let's test the break and here we go.
‫The car starts, the truck starts.
‫And here applying the mechanical break for the vehicle number one, which is the car, and applying
‫the hydraulic break for vehicle number two, which is the truck.
‫So you can see that abstract class and the abstract implementation of the break method that it is used
‫differently in cars and trucks, which are the siblings and the children of the vehicle.
‫So abstraction in Java allows you to use classes and objects without worrying.
‫About the details of their implementation, making programming easier and more fun, just like using
‫the brake in the vehicles and the different types of the children of vehicles like the car and the truck
‫in this example.