Polymorphism - Notes by Shariq SP
Understanding Polymorphism in Java
Polymorphism is a fundamental concept in object-oriented programming that allows objects of different classes to be treated as objects of a common superclass. It enables flexibility and extensibility in the code by allowing methods to behave differently based on the object they are called upon.
How Polymorphism Works in Java
In Java, polymorphism is achieved through method overriding and method overloading.
- Method Overriding: Subclasses can provide a specific implementation of a method defined in their superclass. When a method is called on an object of the subclass, the overridden method in the subclass is executed.
- Method Overloading: Multiple methods with the same name but different parameters can be defined within the same class or across different classes. The appropriate method to be executed is determined at compile time based on the method signature.
Types of Polymorphism
There are two main types of polymorphism in Java:
- Compile-Time Polymorphism: Also known as static polymorphism, it occurs when the method to be invoked is determined at compile time. Method overloading is an example of compile-time polymorphism.
- Runtime Polymorphism: Also known as dynamic polymorphism, it occurs when the method to be invoked is determined at runtime. Method overriding is an example of runtime polymorphism.
Example of Polymorphism in Java
Consider the following example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();
dog.sound(); // Output: Dog barks
cat.sound(); // Output: Cat meows
}
}
In the example above, both Dog
and Cat
classes override the sound()
method defined in the Animal
class. When the sound()
method is called on objects of type Dog
and Cat
, the respective overridden methods are executed, demonstrating runtime polymorphism.
Understanding Compile-Time Polymorphism in Java
Compile-time polymorphism, also known as static polymorphism, occurs when the method to be executed is determined at compile time based on the method signature. It is achieved through method overloading, where multiple methods with the same name but different parameters are defined within the same class or across different classes.
How Compile-Time Polymorphism Works
In Java, compile-time polymorphism is achieved through method overloading. When a method is invoked, the compiler determines which method to call based on the number, type, and order of the parameters passed to the method.
Important Points to Remember
- Method overloading enables compile-time polymorphism by allowing multiple methods with the same name but different parameters.
- The compiler determines which overloaded method to call based on the method signature (number and type of parameters).
- Return type alone is not sufficient to differentiate overloaded methods.
- Overloaded methods can have different access modifiers, but they must not differ only by the return type.
Example of Compile-Time Polymorphism in Java
Consider the following example:
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
int sum1 = calculator.add(5, 10); // Invokes the first add method
double sum2 = calculator.add(3.5, 2.5); // Invokes the second add method
int sum3 = calculator.add(1, 2, 3); // Invokes the third add method
}
}
In the example above, the Calculator
class defines three overloaded add()
methods with different parameter lists. The compiler determines which method to call based on the number and types of arguments passed to the method, demonstrating compile-time polymorphism.
Understanding Runtime Polymorphism in Java
Runtime polymorphism, also known as dynamic polymorphism, occurs when the method to be executed is determined at runtime based on the actual object type rather than the reference type. It is achieved through method overriding, where a subclass provides a specific implementation of a method defined in its superclass.
How Runtime Polymorphism Works
In Java, runtime polymorphism is achieved through method overriding. When a method is called on an object of a subclass, the JVM determines which version of the method to execute based on the actual type of the object at runtime.
Important Points to Remember
- Method overriding enables runtime polymorphism by allowing subclasses to provide specific implementations of methods defined in their superclass.
- The version of the method to be executed is determined based on the actual type of the object at runtime, not the reference type.
- Subclasses can override methods inherited from their superclass to provide custom behavior.
- Method overriding requires the method signature (name, parameters, and return type) to be the same in both the superclass and subclass.
Example of Runtime Polymorphism in Java
Consider the following example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();
dog.sound(); // Output: Dog barks
cat.sound(); // Output: Cat meows
}
}
In the example above, the Dog
and Cat
classes override the sound()
method defined in the Animal
class. When the sound()
method is called on objects of type Dog
and Cat
, the respective overridden methods are executed at runtime, demonstrating runtime polymorphism.
Interview Questions on Polymorphism and its Types in Java
- What is polymorphism in Java?
- Explain the types of polymorphism in Java.
- What is compile-time polymorphism?
- How is method overloading achieved in Java?
- What are the rules for method overloading?
- Explain the concept of method overriding.
- What is dynamic (runtime) polymorphism?
- How is method overriding achieved in Java?
- What are the rules for method overriding?
- What is the difference between method overloading and method overriding?
- Can static methods be overridden in Java?
- Can private methods be overridden in Java?
- Can constructors be overridden in Java?
- Explain the concept of covariant return types.
- What is the significance of @Override annotation in method overriding?
- What is the difference between method overloading and constructor overloading?
- How does Java determine which method to call during method overloading?
- How does Java determine which method to call during method overriding?
- What is polymorphic method invocation?
- Explain the concept of abstract classes and polymorphism.
- How does polymorphism contribute to code reusability and flexibility?
- What are the benefits of using polymorphism in Java?
- How do you prevent method overriding in Java?
- What is the role of interfaces in achieving polymorphism?
- Can you provide an example of polymorphic behavior in Java?
- Explain the concept of method hiding in polymorphism.
- How does polymorphism enhance code maintainability?
- Can you achieve polymorphism without inheritance in Java?
Multiple Choice Questions on Polymorphism and its Types in Java
- What is polymorphism?
- Having multiple constructors in a class.
- The ability of a method to do different things based on the object it is acting upon.
- The ability of a class to have multiple methods with the same name.
- The process of converting one data type to another.
- Which of the following is an example of compile-time polymorphism?
- Method overriding
- Method overloading
- Dynamic polymorphism
- Operator overloading
- What is method overloading?
- Having multiple methods with the same name but different parameters.
- Having multiple methods with the same name and same parameters.
- Having multiple methods with different return types.
- Having multiple constructors in a class.
- Which of the following is true about method overriding?
- It occurs at compile time.
- It occurs at runtime.
- It involves creating multiple methods with the same name in a class.
- It involves creating multiple methods with different names in a class.
- Can a static method be overridden in Java?
- Yes
- No
- It depends on the visibility of the method.
- It depends on the return type of the method.
- What is dynamic polymorphism?
- Having multiple methods with the same name but different parameters.
- Having multiple methods with the same name and same parameters.
- It occurs at compile time.
- It occurs at runtime.
- Which keyword is used to indicate that a method is being overridden?
override
final
super
@Override
- What is the role of interfaces in achieving polymorphism?
- Interfaces cannot be used to achieve polymorphism.
- Interfaces provide a way to implement dynamic polymorphism.
- Interfaces provide a way to implement method overloading.
- Interfaces provide a way to implement compile-time polymorphism.
- Which of the following statements is true about polymorphism?
- Polymorphism allows a class to inherit from multiple classes.
- Polymorphism allows a method to have multiple return types.
- Polymorphism allows objects of different classes to be treated as objects of the same class hierarchy.
- Polymorphism allows a class to have multiple constructors.
- What is the benefit of polymorphism?
- It reduces memory consumption.
- It allows for code reuse.
- It makes the code less readable.
- It increases runtime errors.
- Can a constructor be overridden in Java?
- Yes
- No
- It depends on the visibility of the constructor.
- It depends on the superclass.
- Which of the following is an example of method hiding?
- Method overloading
- Method overriding
- Static method in a subclass with the same signature as a static method in the superclass
- Dynamic polymorphism
- What is the significance of the @Override annotation?
- It indicates that a method is overloaded.
- It indicates that a method is overridden.
- It indicates that a method is abstract.
- It indicates that a method is static.
- Can you achieve polymorphism without inheritance in Java?
- Yes
- No
- It depends on the version of Java.
- It depends on the IDE used.
- Which keyword is used to indicate that a method is being overloaded?
override
final
super
@Override
- What is the main difference between method overloading and method overriding?
- Method overloading occurs at compile time, while method overriding occurs at runtime.
- Method overloading involves creating multiple methods with the same name but different parameters, while method overriding involves creating multiple methods with the same name and parameters.
- Method overloading allows a subclass to provide a specific implementation of a method defined in a superclass, while method overriding allows a subclass to provide a more specific implementation of a method defined in a superclass.
- Method overloading allows a method to have multiple return types, while method overriding does not.
- Which of the following is not a valid access specifier for an overridden method?
public
protected
default
private
- How do you achieve polymorphism in Java?
- By using inheritance
- By using interfaces
- By using method overloading
- All of the above
- What is the main purpose of polymorphism?
- To reduce code complexity
- To increase code readability
- To improve code reusability
- To decrease code size
- Can constructors be inherited in Java?
- Yes
- No
- It depends on the visibility of the constructor.
- It depends on the superclass.