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();
}
}
RANREV INFOTECH