Friday, 31 March 2017

Polymorphism-2

In our previous post  previous post,we discussed about static polymorphism i.e,method overloading,Now lets see about Dynamic polymorphism.

What is Dynamic Polymorphism?

Dynamic polymorphism means exhibiting polymorphism at run-time.i.e, the method call is bound to the method body at run time,java compiler do not aware of which method is executed,however JVM knows which method is to be invoked at the run time.Method overriding is an example for dynamic polymorphism.  Method overriding allows us to have two or more methods with same name and same parameter list that resides in two different classes that are inherited.

Example:


class Code
{ 
 void methodA(int a,int b)
 { 
  System.out.println(a+b);
  System.out.println("This computation is performed in Code class");
  }
}
class Titans extends Code
{ 
  void methodA(int a,int b)
  { 
   System.out.println(a+b);
  System.out.println("This computation is performed in Titans  class");
  }
} 
class MainClass
{ 
 public static void main(String args[])
 { 
  //accessing by creating object for derived class
  Titans t=new Titans();
  t.methodA(2,3);
  //accessing by creating object for the base class
  Code c=new Code();
  c.methodA(5,7);
 }
}

Output:

5
This computation is performed in Titans  class
12
This computation is performed in Code class

How JVM decides which method is called ?

In the above example we have two methods with same name and same parameters,then the question arises how JVM is able to invoke the appropriate methods ? The solution of this issue is simple,JVM calls the method depending on the class of the object that is used for calling.

In the above Example:

we have created objects  t for accessing class Titans methodA()
                                       c for accessing class Code methodA()

Can we override static method()?


No! static methods are related to classes rather than objects.So,we access these methods with the help of class names something like this className.staticmethod(),so JVM calls the method based on the reference,not according to the object that shows method call is decided at compile-time. However,it is not illegal to have static methods with same name,parameters that exist in inherited classes but its not considered as method overriding.

Example:

import java.io.*;
class Code
{
 static void add(int a,int b)
 {
  System.out.println(a+b);
  System.out.println("This computation is performed in Code class");
 }
}
class Titans extends Code
{ 
 static void add(int a,int b)
 { 
  System.out.println(a+b);
  System.out.println("This computation is performed in Titans class");
 }
}
class MainClass 
{ 
 public static void main(String args[])
 { 
     
  Code.add(2,5);
   
  Titans.add(2,3);
 }
}

Output:

7
This computation is performed in Code class
5
This computation is performed in Titans class

Can we Override main() method ?


No,we cannot override main method in java,this is because main method is a static method which implies main method belongs to the class ,not for the instances(objects) of the class.So,method overriding becomes possible only for non-static methods.

Can we Override Constructor?

No! we cannot override constructors.Overriding essentially needs inheritance but constructors do not support inheritance.i.e.,constructors cannot be inherited.

No comments:

Post a Comment