dcsimg
java ploymorphism
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   som_sAHI
Posted On:   Wednesday, October 20, 2004 05:39 AM

in java ploymorphism is achieved by overloading,or overriding or both

Re: java ploymorphism

Posted By:   sujata_samal  
Posted On:   Friday, November 26, 2004 02:16 AM

Polymorphism achieved by both overloading and overriding.


Overloading is static where as Overridding is dynamic.


In case of overloading which method will be called is known to compiler at compile time but in case of overriding only in run-time the appropriate method will be called.

Re: java ploymorphism

Posted By:   java_pro  
Posted On:   Friday, October 29, 2004 04:37 AM

java polymorphism can be achieved by overloading and overriding.


overriding example :


class base
{
void fun()
{
System.out.println("base");
}
}



class derived extends base
{


public void fun()
{
System.out.println("derived");
}

}


public class Overriding
{

public static void main(String str[])
{

derived d = new derived();
base b = d;
d.fun();
}
}



In the above example the assignment base b=d leads to overriding(derived class function called instead of base class).



overloading example :



class base
{
void fun(int a,int b)
{
System.out.println("hai");
}


void fun(int a)
{
System.out.println("hello");
}


}




public class Overriding
{

public static void main(String str[])
{


base b = new base();
b.fun(1);
b.fun(1,2);


}
}

In the above example the function overloading takes place due to the different number of arguments passed to function fun().

About | Sitemap | Contact