• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Function Overloading and Polymorphism

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can function overloading in java be considered as polymorphism?
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really. Overloading and overriding are two different things.
You overload a function when you declare it more than once, with different parameter lists. In this case, you're re-using the function name, but since the function signature is different each version is technically a different function.
You override a function when you declare a function (with an identical signature) in a subclass of a class that contains the same function.
Polymorphism comes into play when you have an object reference that's of the superclass type, but is referring to an instance of the subclass. In this case, if you call a function that's overridden, the overridden version from the subclass will be called.
Example
class A
{
public void doSomething()
{
System.out.println( "doing something in class A" );
}
}
class B extends A
{
public void doSomething()
{
System.out.println( "doing something in class B" );
}
}
class Tester
{
public static void main( String[] args )
{
A a = new B();
a.doSomething();
}
}
running this will print:
doing something in class B
because the B version of doSomething() was called polymorphically.
Watch out for test questions that mix overloading and overriding. In the example above, if the class B version of doSomething() was modified to be
doSomething( String arg ), the would no longer be a doSomething() in B to be called polymorphically (because the functions have different signatures now).
HTH
 
Anand Kapadi
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Phil,
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic