- Java Programming Basics
- Java Tutorial
- Java Environment Setup
- Java Separators
- Java Data Types
- Java Variables
- Java Variable Scope
- Java Type Casting
- Java Operators
- Java Increment Decrement
- Java Left Shift
- Java Right Shift
- Java Bitwise Operators
- Java Ternary Operator
- Java Control Statements
- Java if-else statement
- Java for Loop
- Java while Loop
- Java do-while Loop
- Java switch Statement
- Java break Statement
- Java continue Statement
- Java Popular Topics
- Java Arrays
- Java Multidimensional Array
- Java Strings
- Java Methods
- Java Date and Time
- Java Exception Handling
- Java File Handling
- Java OOP
- Java Classes and Objects
- Java Constructors
- Java Constructor Overloading
- Java Object as Parameter
- Java Returning Objects
- Java Encapsulation
- Java Abstraction
- Java Inheritance
- Java Polymorphism
- Java Packages
- Java Import Statement
- Java Multithreading
- Java Suspend Resume Stop Thread
- Java Programming Examples
- Java Programming Examples
Polymorphism in Java with examples
Polymorphism in Java refers to an object's capacity to assume various forms. This promotes greater flexibility and code reuse by allowing objects of various classes to be handled as though they were objects of a single superclass.
Method overloading (compile-time polymorphism) and method overriding (runtime polymorphism) are two mechanisms that can be used to achieve polymorphism.
Polymorphism in Java: method overloading
Method overloading is a technique in which multiple methods with the same name but different parameters exist in a class. The method that is executed is determined at compile time based on the arguments that are passed. This is also known as static polymorphism or compile-time polymorphism.
When an overloaded method is called, Java determines which version to call based on the number, types, and order of the arguments. Here's an example that demonstrates method overloading in Java:
public class MyClass { public int FindSum(int a, int b) { return a + b; } public double FindSum(double a, double b) { return a + b; } public int FindSum(int a, int b, int c) { return a + b + c; } } public class Main { public static void main(String[] args) { MyClass myObject = new MyClass(); int result1 = myObject.FindSum(2, 3); // calls the FindSum(int a, int b) method double result2 = myObject.FindSum(2.5, 3.5); // calls the FindSum(double a, double b) method int result3 = myObject.FindSum(2, 3, 4); // calls the FindSum(int a, int b, int c) method System.out.println(result1); System.out.println(result2); System.out.println(result3); } }
5 6.0 9
The MyClass class in the given program has three methods with the same name FindSum, but each method takes a different number and type of parameters.
- When the FindSum method is called with two integer parameters myObject.FindSum(2, 3), the FindSum(int a, int b) method is invoked and returns the sum of the two integers, 5.
- When the FindSum method is called with two double parameters myObject.FindSum(2.5, 3.5), the FindSum(double a, double b) method is invoked and returns the sum of the two doubles, 6.0.
- When the FindSum method is called with three integer parameters myObject.FindSum(2, 3, 4), the FindSum(int a, int b, int c) method is invoked and returns the sum of the three integers, 9.
In the above example, the FindSum method's ability to take on multiple forms depending on the parameters passed to it at runtime is an example of polymorphism in Java via method overloading.
Polymorphism in Java: method overriding
Method overriding is a type of runtime polymorphism in Java in which a subclass provides its own implementation of a method defined in its superclass.
To override a method in a subclass, it must have the same name, return type, and parameter list as the superclass method. The subclass method's access level cannot be stricter than the superclass method's. For example, you cannot override a public method with a private method. Here is an illustration of Java's method overriding:
public class MyClass { public int FindSum(int a, int b) { return a + b; } public int FindSum(int a, int b, int c) { return a + b + c; } public static void main(String[] args) { MyClass myObject = new MySubClass(); int result1 = myObject.FindSum(2, 3); // calls the overridden FindSum(int a, int b) method int result2 = myObject.FindSum(2, 3, 4); // calls the FindSum(int a, int b, int c) method from the superclass System.out.println(result1); System.out.println(result2); } } public class MySubClass extends MyClass { @Override public int FindSum(int a, int b) { return a + b + 10; // overrides the FindSum(int a, int b) method to add 10 to the result } }
15 9
The superclass MyClass defines two methods with the same name FindSum, but with different parameters. The subclass MySubClass overrides one of these methods by providing a new implementation for the method. When the main method is executed, an object of MySubClass is created and assigned to a variable of type MyClass. This is an example of upcasting.
The FindSum method of MySubClass is called with the arguments (2, 3), which overrides the same method in the superclass, and adds 10 to the result. The FindSum method of MyClass is called with the arguments (2, 3, 4), which is not overridden in the subclass, so the implementation in the superclass is used. The program then prints the two results to the console. This demonstrates the use of polymorphism to call different versions of the same method based on the object's actual type at runtime.
Advantages of polymorphism in Java
- By enabling the use of the same method across various classes and objects, polymorphism encourages code reuse by lowering the amount of code that must be written and maintained.
- Developers can easily modify existing code thanks to polymorphism's flexibility and extensibility without having to change the program's overall structure.
- Because developers can write more generic, abstract code that can be used in a variety of scenarios and use cases, polymorphism makes code easier to read.
- Scalability is made possible by polymorphism because new classes and objects can be added to the codebase without altering the existing code, increasing the program's overall effectiveness and maintainability.
Disadvantages of polymorphism in Java
- Due to the additional runtime checks required to ascertain an object's true type, polymorphism can occasionally result in a performance overhead, which can slow down and inefficiently use the program's resources.
- For inexperienced programmers who might find it challenging to understand the nuances of object-oriented programming, polymorphism can make the code more complex and challenging to understand.
- Due to problems with method overriding brought on by polymorphism, a subclass may unintentionally alter the behavior of a method in the superclass, leading to unexpected outcomes.
- In large and complex codebases, polymorphism can make debugging more challenging because it can be challenging to identify the precise type of an object and the method that is being called at runtime.
« Previous Tutorial Next Tutorial »