Chapter 8 - Polymorphism Flashcards
Definiton of polymorphism
Polymorphism in Java is like having a universal remote control. Imagine you have different types of electronic devices (TV, DVD player, sound system) with their own unique functions. The universal remote, in this case, is like a common interface or superclass.
Polymorphism is a concept by which we can perform a single task in different ways. That is, when a single entity (object) behaves differently in different cases, it is called polymorphism.
allows objects of different classes to be treated as objects of a common superclass. It enables you to write flexible and extensible code by providing a way to work with objects of multiple types without needing to know their specific implementations.
With polymorphism, you can use this universal remote to control any device, even though each device has its specific features. You don’t need to know the inner workings of each device; you can simply press the buttons on the universal remote, and it sends the right commands to the device you’re currently using.
In Java, this means you can write code that works with objects from various classes as long as they share a common superclass or interface. You can treat these objects uniformly, even though they may have different implementations behind the scenes. This flexibility simplifies your code and makes it more versatile.
Compile time polimrphism
Method Overloading:
Method overloading is a form of compile-time (static) polymorphism.
It allows you to define multiple methods in the same class with the same name but different parameters (different method signatures).
The Java compiler determines which method to call based on the number, type, and order of arguments provided during the method invocation.
Runtime polimrphism
Method Overriding:
Method overriding is a form of runtime (dynamic) polymorphism.
It occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.
The overridden method in the subclass should have the same method signature (method name, return type, and parameter list) as the method in the superclass.
When you call an overridden method on an object of the subclass, the subclass’s implementation is executed.
Polymorphic Reference
Polymorphic reference is a concept in Java that allows you to create a reference variable of a parent class type and then assign it an object of a child class.
Animal myAnimal;
myAnimal = new Dog();
myAnimal.makeSound();
Late Binding (Dynamic Binding)
In polymorphism, the method to be executed is determined at runtime (late binding), not at compile time.
This allows for flexibility in choosing the appropriate method implementation based on the actual object type.
How is Inheritance useful to achieve Polymorphism in Java?
Inheritance represents the parent-child relationship between two classes and polymorphism take the advantage of that relationship to add dynamic behavior in the code (or to make the program more dynamic).
What are the differences between Polymorphism and Inheritance in Java?
Inheritance helps in code reusability in child class by inheriting behavior from parent class. On the other hand, polymorphism enables child class to redefine already defined behavior inside parent class.
What is Binding in Java?
The connecting (linking) between a method call and method definition is called binding in java.
What are the types of binding in Java?
There are two types of binding in java. They are as follows:
a. Static Binding (also known as Early Binding).
b. Dynamic Binding (also known as Late Binding).
What is Static binding in Java?
The binding that happens during compilation is called static binding in java. This binding is resolved at the compiled time by the compiler.
How Java compiler performs static binding?
Java compiler just checks which method is going to be called through reference variable and method definition exists or not.
Why static binding is also called early binding in Java?
Static binding is also called early binding because it takes place before the program actually runs.
Give an example of static binding.
An example of static binding is method overloading.
What is Dynamic binding in Java?
The binding which occurs during runtime is called dynamic binding in java. This binding is resolved based on the type of object at runtime.
How JVM performs dynamic binding in Java?
In the dynamic binding, the actual object is used for binding at runtime. JVM resolved the method calls based on the type of object at runtime. The type of object cannot be determined by the compiler.