JAVA Polymorphism Simplified: - COFPROG

JAVA Polymorphism Simplified:

Polymorphism in Java :

Defination of Polymorphism: one functionality with multiple (changing) forms , static & compile time with early binding which is resolved by javac.

Polymorphism in Java has two types: Compile time polymorphism (static binding) and Runtime polymorphism (dynamic binding). 

Method overloading is an example of static polymorphism, while method overriding is an example of dynamic polymorphism.



1. Compile time Polymorphism (Achieved via method overloading) : 

It is also known as Static binding( occurs during Compile time while Dynamic binding occurs during Runtime), EARLY BINDING and overloading as well. It is also known as Dynamic binding, Late binding and overriding as well. Overloading is compile time Polymorphism where more than one methods share the same name with different parameters or signature and different return type.

EARLY BINDING: Early binding(In Method Overloading your method calls to the methods are decided by the compiler in the sense that which function is going to be called is decided by your compiler at compile time. Hence being EARLY BINDING


Overloading : If a class have multiple methods by same name but different parameters, it is known as Method Overloading.If we have to perform only one operation, having same name of the methods increases the readability of the program. Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b (int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs. So, we perform method overloading to figure out the program quickly.


Dynamic polymorphism (late binding --- dynamic method dispatch ---resolved by JRE): 

Dynamic polymorphism is also called as the run time polymorphism.
As we mentioned earlier while defining polymorphism that polymorphism means there are multiple definitions of a single method. That means a method with same name is defined multiple times in a program. In dynamic polymorphism, if we make a call to such a multiple times defined method in our code then which definition of that method is to be called actually and executed is resolved at run time only.

We can define it as “In dynamic polymorphism the actual method call is resolved dynamically that is at run time only and not at the compile time. “ The compiler only checks the compile time error and does not bind the method call with any actual method. This method binding if is done at runtime by runtime environment then we say dynamic polymorphism is being implemented in the program.

Dynamic polymorphism is achieved by a concept called as method overriding in OOP languages like C++ and Java. Method overriding is redefining the same method again and again in the derived classes in an inheritance hierarchy of the classes.

In next section we discuss in detail themeaning of the term method overriding that is responsible for the behaviour of dynamic polymorphism. And then we further discuss how the code should be written to actually make this method overriding work properly.

Polymorphism rules in Java: 
-- can be in same class or in sub classes.
-- same method name signature 
-- different (no/type/both) ret type 
-- ignored by compiler.

eg --- void test(int i,int j){...}
void test(int i) {..}
void test(double i){..}
void test(int i,double j,boolean flag){..}
int test(int a,int b){...}   ---> compiler err.



RULE -- while polymorphism when java compiler doesn't find exact match then it tries to resolve it by the closest argument type (just wider than the specified argument).


2. 
Dynamic method dispatch: It tells JVM which form of method to send for execution, This decision can't be taken by java compiler BUT taken by JRE
Achieved via method overriding.

Method Overriding: Means of achieving run-time polymorphism

NO "virtual" keyword in java.

All java methods can be overridden : if they are not marked as private,static,final

Super-class form of method - --- overridden method

sub-class form --- overriding form of the method


#Rules : to be followed by overriding method in a sub-class

1. same method name, same signature, ret type must be same or its sub-type(co-variance)
eg of co-variance
class A {
    A getInstance()
{
                    return new A();
}
}

class B extends A
{
    B getInstance()
{
                    return new B();
}
}


2. scope---must be same or wider.

3. Will be discussed in exeception handling.
Can not add in its throws clause any new or broader checked exceptions.
BUT can add any new unchecked excs.
Can add any subset or sub-class of checked excs.
class A
{
  void show() throws IOExc
  {...}
}
class B extends A
{
  void show() throws Exc
  {. 


Previous
Next Post »