Polymorphism Using JAVA

Polymorphisam 
Simply "One interface many forms" .
Dispatcher decide which method will invoke in the program.

  • If compiler dispatching(static) the method it will course to method overloading.
  • If JVM dispatching(dynamic) the method it will course to method overriding.
 

(a)Method Overloading(Compile time Polymorphisam/Static Polymorphisam)
Compiler is the Dispatcher in the 
Same method name but different signatures.
If two method of a class have the same name but signatures  not override equivalent ,then the method name is said to be overloaded.

public class AppInitializer {

    public static void myMethod(){
        System.out.println("My method 1()");
    }

    public static void myMethod(int a){
        System.out.println("My method 2(int)");
    }

    public static void myMethod(String a, int b){
        System.out.println("My method 3(String, int)");
    }

    public static void main(String[] args) {
        myMethod();//My method 1()
        myMethod(10);//My method 2(int)
        myMethod("MyString", 10);//My method 3(String, int)
    }
}

Method
This is a simple method in java.

Methods in Java - GeeksforGeeks
Method Validity

//Same method signatures are not allowed in the same class whether it is static or not
public class AppInitializer {

    public static void myMethod(){
        System.out.println("My method 1()");
    }

    public void myMethod(){
        System.out.println("My method 1()");
    }

public static void main(String[] args) {
        myMethod();
        
    }
}

//Same method signatures are not allowed in the same class whether their throw clauses are different
public class AppInitializer {

    public static void myMethod(){
        System.out.println("My method 1()");
    }

    public void myMethod() throws Exception{
        System.out.println("My method 1()");
    }
public static void main(String[] args) {
        myMethod();
        
    }
}

//Same method signatures are not allowed in the same class whether they have different type meta data
public class AppInitializer {
  public static void myMethod(ArrayList<String> items){
        System.out.println("My method 1()");
    }

    public static <T extends Object & Serializable> void myMethod(ArrayList<Integer> items){
        System.out.println("My method 1()");
    }
public static void main(String[] args) {
        myMethod();
        
    }
}

Method signature
Method signature in Java=Method name+Parameter list  

Override equivalent method Signature
Override equivalent- Two method m 1 & m 2 are override equivalent if either m 1 is sub signature of 
m 2 or m 2 is a sub signature of m 1.

The signature of a method m 1 is a sub signature of the signature of a method m 2 if either:

1.m 2 has same signature as m 1.
eg:-
public class AppInitializer {

    public static void myMethod(int a){//m1
        System.out.println("My method 1()");
    }

    public void myMethod(int b){//m2
        System.out.println("My method 1()");
    }

public static void main(String[] args) {
        myMethod();
        
    }
}

m 1(s.s.)=m 2(s.s.)---->method overloading false

2.The signature of m 1 is the same as the type erasure of the signature of m 2.
type erasure==type meta data(extra information)
eg:-
public class AppInitializer {
  public static void myMethod(ArrayList<String> items){
        System.out.println("My method 1()");
    }

    public static <T extends Object & Serializable> void myMethod(ArrayList<Integer> items){//m1
        System.out.println("My method 1()");
    }
public static void main(String[] args) {//m2
        myMethod();
        
    }
}
m1 erasure:public static void myMethod(ArrayList items)
m2 erasure:public static void myMethod(ArrayList items)---->method overloading false

jvm will delete wthe type variable & replace extended type

(b)Method Overriding(Runtime Polymorphisam/dynamic Polymorphisam)

Comments

Popular posts from this blog

Hibernate-2

Hibernate-1

Avoid Round-off error in large & Small numbers using BigDecimal Class in Java