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


2 comments:

  1. I believe whаt you saіd waѕ aсtually very lοgical.
    But, ωhat abоut this? supposе you were to creаte a awesοme title?
    I aіn't suggesting your information is not solid., but suppose you added a title to maybe get folk's attention?

    I mean "Polymorphism in Java Using Overloading and Overridden methods" is kindа plain.
    You could look at Yahoo's home page and see how they create post titles to get viewers to click. You might try adding a video or a related pic or two to get readers excited about everything'νe
    written. In my opinion, it would bгing your blog a little bіt morе interesting.
    Review my homepage ; samsung galaxy s3

    ReplyDelete

Thanks for your valuable comments