Saturday, September 1, 2012

Polymorphism in Java Using Overloading and Overridden methods


Polymorphism
One form and many implementations.
It can be acheived by using inheritence,overloading and Overriding.
Compile time Polymorphism: Over loading
Runtitme Polymorphism: Over riding
Runtime Polymorphism /Dynamic method dispatch:
Its a process in which a call to an overridden methodis resolved at runtime rather than compile itme.
Dynamic Binding: Refers to linking of a procedure call to the code to be executed in response to the call
Also known as late binding :Code associated to procedure call is not known untile the time of the call at runtime.

Overloading.
Two or more methods with same name in the same class with different arguments.

  • Overloaded methods Must change the argument list
  • May change the return type
  • May change the access modifiers.
  • May change the checded exceptions.

Example:
package com.oops;

public class OverLoadingExample {

public int sum(int a,int b){
System.out.println("Int Int");
return a+b;
}

public double sum(double a,double b){
System.out.println("Double Double");
return a+b;
}

public int sum(int a,double b){
System.out.println("Int Double");
return (int) (a+b);
}
public static void main(String args[]){

OverLoadingExample oe=new OverLoadingExample();
System.out.println("Overloading method"+oe.sum(3, 7));
System.out.println("Overloading method"+oe.sum(2.5, 5.7));
System.out.println("Overloading method"+oe.sum(2, 5.5));
}
}

Output:
=======
Int Int
Overloading method10
Double Double
Overloading method8.2
Int Double
Overloading method7


Overriding:

Occurs in the subclass and declares method that has same type arguments as a method and declared by one of its super class.

  • It can be used for defining the behaviour of child class.
  • Argument list same as overriden method.
  • Constructors canot be overridden.
  • final methods canot be overridden.
  • Static method cannot be overridden but can be re-declared.
  • super() can be used for invoking parent class(Super class) methods.

Example
package com.oops;

class OverExample{

public void fun1(){
System.out.println("Super Function1");
}

public void fun2(){
System.out.println("Super Using super() Function2");
}

}
public class OverRiddingExample extends OverExample{

public void fun1(){
System.out.println("Subclass Function1");
}


public void fun2(){
super.fun2();
System.out.println("Subclass Function2");
}

public static void main(String[] args) {
OverExample or=new OverRiddingExample();
OverExample oe=new OverExample();
or.fun1();
oe.fun1();
or.fun2();
}

}

Output:
========

Subclass Function1
Super Function1
Super Using super() Function2
Subclass Function2


Inheritance in Object Orient Programming and Java


Inheritance
Its a process where one object acquire properites another.
Inheritence reduces the code re-usablity.

Inheritence can be achieved by using extends or implemnts keyword

Single Inheritance:
Class A extends B{
statements
}

Example:
=============
Pulsar is subclass of Bike
TVS is subclass of Bike.

package com.oops;

class TwoWheeler{

int count=1;

public void setProp(){
System.out.println("Color: Red & Brand:Own");
}

}
public class SingleInheritence extends TwoWheeler {

public static void main(String args[]){
TwoWheeler two=new TwoWheeler();
SingleInheritence si=new SingleInheritence();

System.out.println(si instanceof TwoWheeler);
System.out.println("Calling method in SuperClass");
si.setProp();
}
}

Output
=======
true
Calling method in SuperClass
Color: Red & Brand:Own

Multiple Inheritance can be acheived by using interface. 

Interface in Java Language (Click on link to know about interfaces)

Below example contains Single Inheritance and Multiple Inheritance Using Interfaces.

package com.oops;

class TwoWheeler{

int count=1;

public void setProp(){
System.out.println("Color: Red & Brand:Own");
}

}

interface Car
{
   public void setBrandname(String name);
}

interface HeaveyVechile{

public void setProperites(String prop);
}

public class Wheeler extends TwoWheeler implements Car, HeaveyVechile{


public static void main(String args[]){

TwoWheeler two=new TwoWheeler();
Wheeler si=new Wheeler();

System.out.println(si instanceof TwoWheeler);
System.out.println("Calling method in SuperClass");
si.setProp();
si.setBrandname("First");
si.setProperites("HeavyLoadVechile");
}

@Override
public void setBrandname(String name) {
System.out.println("Brand name is"+name);

}

@Override
public void setProperites(String prop) {
System.out.println("Heavy Vechile with 4 or 8 Wheels "+prop);


}

}

Output
=========
true
Calling method in SuperClass
Color: Red & Brand:Own
Brand name isFirst
Heavy Vechile with 4 or 8 Wheels HeavyLoadVechile