Welcome back! We're continuing our journey into object-oriented programming (OOP) with a new and exciting topic: Polymorphism. You've already learned about classes, objects, and inheritance, which are essential building blocks of OOP. Now, it's time to explore how polymorphism can make your code more flexible and reusable.
Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. This provides a way to use a single interface to represent different types of objects. Here’s what we’ll cover in this lesson:
- Understanding Polymorphism: We'll discuss what polymorphism is and why it's a powerful concept in Java programming.
- Runtime Polymorphism: You'll learn how to use interfaces and method overriding to achieve runtime polymorphism.
- Compile Time Polymorphism: You'll understand method overloading and how it's used for compile-time polymorphism.
Polymorphism in Java allows you to call derived class methods through a base class or interface reference. This can make your code more dynamic and general. Polymorphism in Java can be achieved through 2 distinct methods:
- Method Overriding (Runtime Polymorphism): This is when a subclass provides a specific implementation of a method that is already defined in its superclass. The method in the superclass is overridden by the method in the subclass.
- Method Overloading (Compile-Time Polymorphism): This is when multiple methods in the same class have the same name but different parameters. They may have different return types, but using different parameter lists allows Java to distinguish between them.
Polymorphism allows a single action to be performed in different ways. The ability to redefine methods in derived classes and have a unified method call mechanism via base classes or interfaces is a strength of Java OOP.
Runtime polymorphism is achieved through method overriding. Here are the rules for method overriding:
- Same Method Signature: The method in the derived class must have the same name, return type, and parameters as the method in the base class.
- Inheritance: The class must be a subclass of the base class where the original method is defined.
- Access Modifier: The overriding method cannot have a more restrictive access modifier than the method in the base class. For example, if the base class method is
public
, the overriding method cannot beprotected
orprivate
. - Invocation: JVM determines which method to invoke at runtime based on the object's actual class, not the reference type.
- Annotations: It's a good practice to use the
@Override
annotation to ensure that you are indeed overriding a method.
Example:
Java1class Animal { 2 public void sound() { 3 System.out.println("Animal makes a sound"); 4 } 5} 6 7class Dog extends Animal { 8 @Override 9 public void sound() { 10 System.out.println("Dog barks"); 11 } 12} 13 14class Cat extends Animal { 15 @Override 16 public void sound() { 17 System.out.println("Cat meows"); 18 } 19} 20 21public class Main { 22 public static void main(String[] args) { 23 Animal myDog = new Dog(); 24 myDog.sound(); // Output: Dog barks 25 26 Animal myCat = new Cat(); 27 myCat.sound(); // Output: Cat meows 28 } 29}
In this example, the sound
method in Dog
and Cat
overrides the sound
method in Animal
. When sound
is called on the Animal
reference pointing to a Dog
or Cat
object, the respective sound
method of Dog
or Cat
is invoked.
Compile-time polymorphism is achieved through method overloading. Here are the rules for method overloading:
- Same Method Name: The methods must have the same name but different parameter lists.
- Different Parameters: The parameter lists must differ in type, number, or both. This is how the compiler distinguishes between the overloaded methods.
- Return Type: The return type can be different or the same; however, the return type alone is not sufficient to distinguish overloaded methods.
- Access Modifier: The access modifier can be different for overloaded methods.
- Static Methods: Static methods can be overloaded, but they cannot be overridden. Overloading is resolved at compile time based on the method signature.
Example:
Java1class MathOperation { 2 public int add(int a, int b) { 3 return a + b; 4 } 5 6 public double add(double a, double b) { 7 return a + b; 8 } 9} 10 11public class Main { 12 public static void main(String[] args) { 13 MathOperation math = new MathOperation(); 14 System.out.println(math.add(2, 3)); // Output: 5 15 System.out.println(math.add(2.5, 3.5)); // Output: 6.0 16 } 17}
In this example, the add
method is overloaded. The compiler determines which add
method to call based on the type of the arguments.
Polymorphism is crucial because it introduces flexibility and scalability to your code:
- Code Flexibility: Polymorphism allows you to write functions that can operate on objects of different types through a common interface.
- Reusability: You can extend and reuse your code more efficiently by leveraging polymorphism.
- Simplified Code Management: Polymorphism helps you manage and understand your code better, as similar operations are handled in a unified manner.
Now that you've grasped the concepts of polymorphism, it's time to put your knowledge to the test. Proceed to the practice section for some hands-on practice exercises!