The document explains polymorphism in Java, a fundamental concept in object-oriented programming that allows objects to take multiple forms, illustrated through real-life examples like a woman fulfilling different roles and animals making distinct sounds. It describes two main types of polymorphism: compile-time (method overloading) and runtime (method overriding), with corresponding examples demonstrating how methods can be defined and invoked based on different parameters or object types. The document emphasizes how polymorphism enhances code flexibility and reusability by allowing multiple methods with the same name in a single class.
Explains polymorphism as a core OOP principle, illustrated through real-world examples like a woman in various roles and animals with distinct sounds.
Details methods to achieve polymorphism in Java: method overloading (same name, different parameters) and method overriding (subclass implementing parent class methods).
Discusses compile-time polymorphism through method overloading with examples of methods differing by parameters, showcasing how the compiler selects the correct one.
Describes runtime polymorphism via method overriding, highlighting dynamic method dispatch and how methods are resolved at runtime based on object type.
JAVA ROADMAP Java Polymorphism– Types And Examples Polymorphism is one of the 4 pillars of Object-Oriented Programming. It is a combination of two Greek words: poly and morphs. “Poly” means “many,” and “morphs” means “forms.” So in Java, polymorphism means many forms. Polymorphism is defined as the ability of a message to be displayed in more than one form. Let’s understand the meaning of polymorphism by an example: Consider a woman in society. The same woman performs different roles in society. The woman can be the wife of someone, the mother of her child, can be at the role of a manager in an organization, and many more at the same time. But the Woman is only one. So, the same woman performing different roles is polymorphism. Another best example is your smartphone. The smartphone can act as a phone, camera, music player, alarm, and whatnot, taking different forms and hence polymorphism
2.
Introduction Polymorphism in Javarefers to an object’s ability to take on multiple forms or types. It enables the treatment of objects of different classes as objects of a common superclass or interface. In simpler terms, polymorphism allows us to represent different types of objects using a single variable or method. We can relate polymorphism in real life by the following example. Consider different types of animals. Each animal makes a distinct sound. By leveraging polymorphism, you can define a common “makeSound” method in a superclass called “Animal,” which is overridden in each subclass with the specific sound implementation. // Base class Animal class Animal { public void makeSound() { System.out.println("Animal making a generic sound..."); } } // Subclass Dog class Dog extends Animal { // Override the makeSound method in the Dog class public void makeSound() { System.out.println("Dog barking: Woof!");
3.
} } // Subclass Cat classCat extends Animal { // Override the makeSound method in the Cat class public void makeSound() { System.out.println("Cat meowing: Meow!"); } } // Subclass Cow class Cow extends Animal { // Override the makeSound method in the Cow class public void makeSound() { System.out.println("Cow mooing: Moo!"); } } public class Main { public static void main(String[] args) { Animal dog = new Dog(); Animal cat = new Cat(); Animal cow = new Cow(); dog.makeSound(); cat.makeSound(); cow.makeSound(); } } Output: Dog barking: Woof! Cat meowing: Meow! Cow mooing: Moo! In this example, we have a base class Animal with a makeSound method. The subclasses Dog, Cat, and Cow inherit from the Animal class and override the makeSound method with their specific sound. By creating objects of each subclass and calling the makeSound method, we observe polymorphic behavior where each animal produces its unique sound. This example demonstrates how polymorphism allows for the flexibility of handling different objects through a common interface or superclass, enabling code reuse and promoting flexibility in real-life scenarios.
4.
How polymorphism canbe achieved? Polymorphism in Java can be achieved through two concepts i.e. method overloading and method overriding. Let’s dive into each of these concepts: Method Overloading Polymorphism via method overloading enables a class to have multiple methods with the same name but different parameters. The methods can perform similar actions but with different data types or parameters. The Java compiler determines which method to invoke based on the method call arguments. Example: class Calculator { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } } public class Main { public static void main(String[] args) { Calculator calculator = new Calculator(); System.out.println(calculator.add(5, 10)); // Output: 15 System.out.println(calculator.add(3.5, 2.5)); // Output: 6.0 } } In the example above, the Calculator class has two add() methods. One accepts two integers as parameters, while the other accepts two doubles. Depending on the arguments passed during the method call, the compiler determines the appropriate method to invoke. This allows us to use the same method name add() for different data types, enhancing code readability and flexibility. Method Overriding Polymorphism through method overriding allows a subclass to provide its own implementation of a method defined in its parent class. It involves creating a method in the subclass with the same name, return type, and parameters as the method in the parent class. The method in the subclass overrides the implementation of the method in the parent class, allowing the subclass to exhibit its unique behavior.
5.
Example: class Animal { publicvoid makeSound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { @Override public void makeSound() { System.out.println("Dog barks"); } } public class Main { public static void main(String[] args) { Animal animal = new Dog(); animal.makeSound(); // Output: Dog barks } } In the example above, we have a superclass called Animal with a makeSound() method. The Dog class extends the Animal class and overrides the makeSound() method with its own implementation. When we create an object of type Animal and assign it to a Dog object, and then call the makeSound() method, it invokes the overridden method in the Dog class, displaying “Dog barks” as the output. Types Of Polymorphism In Java There are two types of polymorphism in Java: compile-time polymorphism (also known as method overloading) and runtime polymorphism (also known as method overriding). 1. Compile-Time Polymorphism (Method Overloading) This type of polymorphism in Java is also called static polymorphism or static method dispatch. It can be achieved by method overloading. In this process, an overloaded method is resolved at compile time rather than resolving at runtime. ● Method overloading allows a class to have multiple methods with the same name but different parameters.
6.
● Based onthe number and type of the arguments passed the compiler determines which method to call. ● The methods may have different return types, but this alone is insufficient to distinguish overloaded methods. Example 1: Method Overloading with Different Number of Parameters public class Calculator { public int add(int a, int b) { return a + b; } public int add(int a, int b, int c) { return a + b + c; } public static void main(String[] args) { Calculator calculator = new Calculator(); System.out.println(calculator.add(2, 3)); System.out.println(calculator.add(2, 3, 4)); } } Output: 5 9 In this example, the Calculator class has two add methods. The first add method takes two parameters, a and b, and returns their sum. The second add method takes three parameters, a, b, and c, and returns their sum. The two methods have the same name but different numbers of parameters. Depending on the number of arguments passed the compiler selects the appropriate method. Example 2: Method Overloading with Different Types of Parameters public class Converter { public String convertToString(int number) { return String.valueOf(number); } public String convertToString(double number) { return String.valueOf(number); }
7.
public static voidmain(String[] args) { Converter converter = new Converter(); System.out.println(converter.convertToString(42)); System.out.println(converter.convertToString(3.14)); } } Output: 42 3.14 In this example, the Converter class has two convertToString methods. The first convertToString method takes an int parameter and converts it to a String. The second convertToString method takes a double parameter and converts it to a String. The two methods have the same name but different types of parameters. Based on the type of the argument passed the compiler determines which method to invoke. 2. Runtime Polymorphism (Method Overriding) Dynamic method dispatch is another name for runtime polymorphism. This polymorphism is achieved by method overriding. The overridden method is resolved at runtime rather than at compile time. In Java, runtime polymorphism occurs when two or more classes are related through inheritance. We must create an “IS-A” relationship between classes and override a method to achieve runtime polymorphism. ● Method overriding occurs when a subclass implements a method that is already defined in the parent class. ● The must rule of method overriding is that the subclass method must have the same name, return type, and parameter list as the parent class method. ● The method to invoke is determined at runtime based on the actual type of the object. ● The @Override annotation (optional but recommended) is frequently used to indicate that a method is intended to override a superclass method. Example 1: Method Overriding class Animal { public void makeSound() {
8.
System.out.println("Animal is makinga sound"); } } class Cat extends Animal { @Override public void makeSound() { System.out.println("Cat is meowing"); } } class Dog extends Animal { @Override public void makeSound() { System.out.println("Dog is barking"); } } public class Main { public static void main(String[] args) { Animal animal1 = new Cat(); Animal animal2 = new Dog(); animal1.makeSound(); animal2.makeSound(); } } Output: Cat is meowing Dog is barking VISIT BLOG.GEEKSTER.IN FOR THE REMAINING