Polymorphism:
Polymorphism is nothing but perform an single action by different ways.
Ex: A man will behave different in office ,home etc ,person is same but behaviour is different based on place.
We can achieve polymorphism in 2 ways:
MethodOver loading(compile time polymorphism)
Method Overring(Run time polymorphism)
Method Overloading:
If you overload static method in java , it is example for compile time polymorphism.
If a class have multiple methods by same name but different parameters in same class(single class), it is known as Method Overloading.
Sample Code:
public class MethodOverLoading {
void test()
{
System.out.println("without any parameter");
}
void test(int a)
{
System.out.println("with one parameter");
}
void test(int a,int b)
{
System.out.println("without two parameter");
}
public static void main(String[] args)
{
MethodOverLoading mol=new MethodOverLoading();
mol.test();
mol.test(10);
mol.test(10,20);
}
}
Output:
without any parameter
with one parameter
without two parameter
Main Points:
1) We cannot create a method inside a method
2) With same method name and same parameters are not allowed
+++++++++++++++++++++++++++++
Method Overriding:
Same method name with same signature in 2 dissrent calsses in method overriding.
Rule 1: you can only override method in sub class
Rule 2: You can not override method in same class.
Sample code:
class Cbz extends MethodOverRiding_Bike
{
void splender()
{
System.out.println("am using splender bike from child class:");
}
}
public class MethodOverRiding_Bike {
void splender()
{
System.out.println("am using splender bike now:");
}
public static void main(String[] args) {
MethodOverRiding_Bike morride=new Cbz();
morride.splender();
}
}
Output: am using splender bike from child class:
Polymorphism is nothing but perform an single action by different ways.
Ex: A man will behave different in office ,home etc ,person is same but behaviour is different based on place.
We can achieve polymorphism in 2 ways:
MethodOver loading(compile time polymorphism)
Method Overring(Run time polymorphism)
Method Overloading:
If you overload static method in java , it is example for compile time polymorphism.
If a class have multiple methods by same name but different parameters in same class(single class), it is known as Method Overloading.
Sample Code:
public class MethodOverLoading {
void test()
{
System.out.println("without any parameter");
}
void test(int a)
{
System.out.println("with one parameter");
}
void test(int a,int b)
{
System.out.println("without two parameter");
}
public static void main(String[] args)
{
MethodOverLoading mol=new MethodOverLoading();
mol.test();
mol.test(10);
mol.test(10,20);
}
}
Output:
without any parameter
with one parameter
without two parameter
Main Points:
1) We cannot create a method inside a method
2) With same method name and same parameters are not allowed
+++++++++++++++++++++++++++++
Method Overriding:
Same method name with same signature in 2 dissrent calsses in method overriding.
Rule 1: you can only override method in sub class
Rule 2: You can not override method in same class.
Sample code:
class Cbz extends MethodOverRiding_Bike
{
void splender()
{
System.out.println("am using splender bike from child class:");
}
}
public class MethodOverRiding_Bike {
void splender()
{
System.out.println("am using splender bike now:");
}
public static void main(String[] args) {
MethodOverRiding_Bike morride=new Cbz();
morride.splender();
}
}
Output: am using splender bike from child class:
No comments:
Post a Comment