Polymorphism
1. Compile-Time Polymorphism (Method Overloading)
we follow some below rules like that:-
a. Different number of parameters
b. Different types of parameters
c. Different method return type
Syntax:
void ABC()
{ } //assume memory allocated size 3kb
void ABC(two arguments)
{ } //assume memory allocated size 4kb
void ABC(different arguments)
{ } // assume memory allocated size 3kb
The total no memory allocate size is 4 kb. Because the overloading concept has been used there for the method to take the highest memory allocated size.
The largest example of method overloading is println() method. We can check using the command javap java.io.PrintStream
Code Example
class A
{
void show()
{
System.out.println("show method");
}
void show(int x)
{
System.out.println("show method with single parameter "+x);
}
void show(double y)
{
System.out.println("show method with single parameter "+y);
}
public static void main(String arg[])
{
A d=new A();
d.show();
d.show(23);
d.show(24.5);
}
}
2. Run-Time Polymorphism (Method Overriding)
When a subclass defines a method that has the same signature as a parent class method, it is referred to as overriding the parent class's methods. It is one of the ways that polymorphism is implicated.
Code Example
class Over
{
public Over()
{
System.out.println(“ In Over class”);
}
}
class B extends Over
{
}
class C extends B{
public C(){
System.out.println(“In C”);
}
public static void main(String sr[])
{
Over x= new Over();
B y = new B();
C z = new C();
}
}
FAQs
Polymorphism is important in Java because it improves code flexibility, reusability, and maintainability. It allows developers to write generic code that can work with different types of objects, reducing duplication and improving software design.
There are two main types of polymorphism in Java:
Compile-Time Polymorphism (Method Overloading)
Run-Time Polymorphism (Method Overriding)
Compile-time polymorphism occurs when multiple methods have the same name but different parameters. This is called method overloading, and the method to be executed is determined during compilation.
example :
class Example {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String arg[])
{
Example ex=new Example();
ex.add(10,20);
ex.add(10,20,30);
}
}
Run-time polymorphism occurs when a subclass provides a specific implementation of a method already defined in its parent class. This concept is called method overriding, and the method is decided during program execution.
example:
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
public static void main(String arg[])
{
Animal an=new Animal();
an.sound();
Dog dg=new Dog();
dg.sound();
}
}
Method overloading is a feature where multiple methods have the same name but different parameters. It improves program readability and reduces code complexity.
Method overriding happens when a subclass provides its own implementation of a method that is already defined in the parent class. It is used to achieve runtime polymorphism.
Method Overloading and Method Overriding are two important concepts of polymorphism in Java. Method Overloading occurs at compile time, where multiple methods have the same name but different parameters. It does not require inheritance and is mainly used to improve the readability and flexibility of the program by allowing the same method name to perform different tasks with different inputs. On the other hand, Method Overriding happens at run time, where a child class provides its own implementation of a method that is already defined in the parent class. In this case, the method parameters must remain the same, and inheritance is required. Method overriding is mainly used to change or extend the behavior of an existing method in a subclass.
RANREV INFOTECH