‫Welcome back.
‫In this video we'll talk about polymorphism in Java.
‫Polymorphism is a concept that allows objects of different classes to be treated as objects of a common
‫parent class.
‫It enables a single interface to represent multiple forms, types of objects, making the code more
‫flexible and reusable.
‫Let's continue with the vehicle example.
‫This is the parent class vehicle.
‫I have a car class that it's a child of vehicle extends from vehicle.
‫Let's create a new Java class named as truck and this truck will extend from vehicle also.
‫And I'll create a constructor matching soup because it will match the vehicle and I need to pass the
‫number of wheels I need to override the start method.
‫If I type start, I override it and here I need to print.
‫The truck starts now.
‫Let's go back to our main method in the my class and here I'll create a vehicle.
‫So let me show you vehicle V one equal to new.
‫And you can see that Android studio will suggest to create a vehicle or a truck.
‫We'll suppose for me, the car and the truck.
‫I'll create V one as a car and here I need to pass the model of car.
‫I'll name it as BMW and I'll create a vehicle v two object equal to new truck with six wheels.
‫So did you notice that a vehicle here is replaced as or assigned as a car while vehicle V two is assigned
‫as a truck?
‫This is the power of polymorphism.
‫Moreover, polymorphism allows us to create consistent code.
‫So in this example, polymorphism in Java allows us to create a single method, start or stop that will
‫behave differently for different vehicles like truck and car.
‫Back to my class v one dot start and v two dot start.
‫Same method but different objects and different classes run and you can see that the start method behaves
‫differently for different vehicles.
‫The first one the car starts behave as a car while the second will behave as a truck.
‫So this is the start of the truck and this is the start method of the car.