Thursday, June 22, 2017

Java-Polymorphism

Polymorphism
------------
Poly means many
morphism--forms

Representing same method in many forms is known as polymorphism.

There are 2 types of polymorphsim
i)compile time.
ii)run time.

Compiletime polymorphism
------------------------
If method binding happens at the time of compiling a program itself,it is known as compiletime polymorphism.

what is method binding
----------------------
The process of matching a method call to its appropriate method is known as method binding

void m1()//1
{

}
void m1(int x,int y)//2
{
}
void m1(int x,int y,int z)//3
{
}


m1(10,20,30)//1
m1(10,20)//2
m1()//3
m1(10,20,30,40)//4

Here the following mapping gives order of matching betwteen method and method call.

1-3
2-2
3-1.

we implement compiletime polymorphism by using method overloading.

compiletime ploymorphism occurs based on reference.

Method overloading
------------------
The process defining same method with different parameters is known as method overloading.


Rules for overloading methods
-----------------------------
i)The name of the method must  be same
ii)The parameters should be different i,e the datatype of parameters must change or no of parameters must change.


Program
-------
class Greatest
{
   public void max(int x,int y)
   {
     if(x>y)
     {
       System.out.println(x +" is greater");
     }
     else{
       System.out.println(y +" is greater");
     }
   }
  public void max(double x,double y)
   {
     if(x>y)
     {
       System.out.println(x +" is greater");
     }
     else{
       System.out.println(y +" is greater");
     }
   }
   public static void main(String args[])
   {
        Greatest t=new Greatest();
        t.max(10,20);
        t.max(10.5,20.5);

     
   }
}

The purpose of overloading is, to the same method we can pass different parameters which reduces introducing number of new method definitions in an API[Application programming interface].

While calling a method,compiler will try find exact match of method,if the match is not found compiler try to promote parameters to the next level,still if the match  is not found,we get compilation error.

ex:
   public void max(int x,int y)
   {
    //statements
   }

   byte a=10,b=20;
   t.max(a,b);

In the above code we are calling max method by passing byte values but we dont have a max method declared with byte parameters so the compiler will promote byte parameters to int parameters.


we can pass different parameters like byte,short,int,.. to a method with double parameters,we know all the primitives are promted to double(except boolean).But here the problem is ,it requires datatype promotion which reduces performance of application.


ex:
  public void max(double x,double y)
   {
   }


  //case1
  byte a=10,b=20;
  t.max(a,b);

  //case2
  short a=10,b=20;
  t.max(a,b);

  //case3
  int a=10,b=20;
  t.max(a,b);

  ..
  ..
 
In the above code we are passing byte,short,int,.. parameters to max method,here all parameters are promoted to double type but this conversion is going to cost,it reduces performance so finally it is recommended to declare a method with exact match.

while calling a method a compiler considers the following characteristics.

i)Name of the method.
ii)No of parameters.
iii)Type of parameters.


Metod overloadin is declaring------ methid in -----forms.

While overloading parameters of method must------.

what is puprose of overloading?

while binding a method compiler consider the following parameters?

CP accurs based on ------.


----doesn't participate in overloading.


Runtime polymorphism
--------------------
If method binding happens at the time of executing a program is called as runtime ploymorphism.

we implement runtime polymorphism using method overriding.

Runtime polymorphism occurs based on object.

Method overriding
-----------------
The process of declaring methods of super class in sub class with same signature(method definition) but with different implementation is known as method overriding.

ex:
class A
{
  void marriage()
  {
    System.out.println("subba lakshmi");
  }
}
class B extends A
{
  void marriage()
  {
    System.out.println("Lekya");
  }
}


rules of overriding
-------------------
i)while overriding the signature of a method must be same in both super class and sub class.

signature means(returntype+methodname+parameters).

we may have a change in return types while overriding a method,such change in return type is called as covariant return type.

ex:
   class A
   {
      public Object m1()
      {
         return 10;
      }
   }

   class B extends A
   {
      public String m1()
      {
         return "10.5";
      }
   }

In the above code the return type in the super class method m1()  is object,the return of subclass method m1() which is overridden is String,here string is a subclass of Object because we known every class in java is inherited from a super class called object.

This change in return types from parent class to that of child class where the return type of parent class method is a superclass(Object) whereas the return of the subclass method is a child(String) of the returntype of superclass method,is known as co variant return types.


void m1()
{
}
int m1()
{
}

accessmodfiers in overriding
----------------------------
While overriding,the  method in the super class is known as overridden method.

The method in the subclass is called overriding method.

ex:
class A
{
  void m1()//overridden
  {
  }
}
class B extends A
{
  void m1()//overriding
  {
  }
}

*while overriding a method,we should keep the accessmodifier of overriding method same as overridden method otherwise, if possible we can increase the accessmodfier of overriding method, but we cannot decrease the accesslevel of overriding method.

ex:
what is the output?
class A
{
  void m1()//overridden
  {
  }
}
class B extends A
{
  void m1()//oveirriding
  {
  }
}


i)compiles
ii)CE
iii)None

what is the output?
class A
{
  void m1()//overridden
  {
  }
}
class B extends A
{
  public void m1()//oveirriding
  {
  }
}
i)compiles
ii)CE
iii)None

Here we had increased the accesslevel of the overriding method to public from default which is the accesslevel of overridden method.

what is the output?
class A
{
  public void m1()//overridden
  {
  }
}
class B extends A
{
  void m1()//oveirriding
  {
  }
}
i)compiles
ii)CE
iii)None

Here we are trying to decrease the accesslevel of overriding method compared to that of overridden method.we get compilation error saying

m1() in B cannot override m1() in A.
attempting to assign weaker access privileges.


overriding private methods
--------------------------
we cannot override private methods because private methods are not visible to sub classes.

ex:
class A
{
  private void m1()
  {
    System.out.println("A");
  }
}
class B extends A
{
  void m1()
  {
    System.out.println("B");
  }
 
}
Here the method in the sub class is a sub class specific method but not overriding method because overriding is not applicable for private methods.

overriding variables
--------------------
overrding is not applicable for variables.


ex:
class A
{
  int a=10;
}
class B extends A
{
  int a=20;
}

Here we are not overriding variables.


runtime polymorphism[dynamic method dispatch]
---------------------------------------------
In java a super class reference can refer to a subclass object but reverse is not possible i,e a sub class reference cannot refer to a super class object.

ex:
class A
{
   void m1()
  {
    System.out.println("A");
  }
 
}
class B extends A
{

  void m1()
  {
    System.out.println("B");
  }
  public static void main(String args[])
  {
     A a=new A();
     B b=new B();
     a.m1();//
     b.m1();//
   
     A a2=new B();
     a2.m1();//
   

  }
 
}

dynamic method dispatch
-----------------------
The process of binding a method call to a method at runtime by jvm is known as dynamic binding or dynamic method dispatch.

In the above example we are initializing a sub class object to a super class reference,here compiler cannot decide which method to bind(call) whether a method from sub class or super class so here jvm will take care of method resolution based on object.This approach is called as dynamic method dispatch.

overriding static methods
-------------------------
we cannot override static methods,if we override static methods it is called as method hiding i,e the method of sub class will hide the method of super class.

Program
-------
class A
{
  static void m1()//overridden
  {
    System.out.println("A");
  }
}
class B extends A
{
   static void m1()//overriding
  {
    System.out.println("B");
  }
  public static void main(String args[])
  {
     A a=new A();
     B b=new B();
     a.m1();//A
     b.m1();//B

     A a2=new B();
     a2.m1();//1
  }
}


output
------
A
B
A

In the above code we are overriding static method m1(),here it is not overriding but method hiding.Here method binding happens based on reference.when we call  m1() at line1 the method from super class A is called but not from subclass,here method binding occured based on reference.

comiple time polymorphism is also called as static polymorphism.

Runtime polymorpshism is also called as dynamic polymorphism.


declaring abstract and static with a method is illegal,leads to compilation error.

which of the following declarations are valid?

i)abstract void m1();
ii)public abstract void m1();
iii)public static abstract void m1();
iv)static abstract void m1();
v)abstract public void m1();
vi)abstract public static void m1();
vii)public static void m1();


No comments: