MENU

Fun & Interesting

Polymorphism in java | Run Time binding Compile time binding late binding | up casting down casting

Muhammad Umar Farooq 478 lượt xem 3 years ago
Video Not Working? Fix It Now

#Polymorphism #LateBinding #Upcasting #Downcasting

Polymorphism literally means “of multiple shapes/forms” and in the context of OOP,
polymorphism means “having multiple behaviors”.
Look at the example of Inheritance in video

Normally, when we want to create an object of a class we call its constructor. To
create an object of class Person we write
Person per = new Person ( );
There is nothing strange in the above line of code as we create objects of a class by
calling its any of the available constructor.
ƒ BUT, polymorphism allows us also to write the following line of code
Person per = new Employee ( );
Don’t be surprised; just revisit the concept of inheritance i.e. is a relationship. Since
we can say that Employee is a Person. It means that a variable of type Person can
point to an object of type Employee. That’s what we have translated above in the
form of code. What do you think about the following line?
Person ahmad = new Teacher ( );
Employee usman = new Manager ( );

These are 100 percent correct and rightly so, because Teacher is a Person as well as
Manager is an Employee.

Comment