• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Clarification on : Overloading vs polymorphism

 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please confirm that polymorphism is a different beast from method overloading.
Thanks
Pho
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pho-
They are different.
Method overloading is when you have methods with the same name, but different paramater lists. Such as:
public void myTest(){}
public void myTest(int a){}
public int myTest (String s){return 1;}

Polymorphism is the ability of an object to be declared that of a super object type and maintain the methods in the actual type class.
i.e.
Object obj = new String("Hi");
obj.equals("Hi");//true because the equals method is used from the String class
Kyle
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once upon a time we had a lOOOOONG conversation about this in Cer Study. YOu might get something out of it.
http://www.javaranch.com/ubb/Forum24/HTML/009465.html
 
Pho Tek
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a simple polymorphism problem.
I want to create a method of this signature:
public void printType( Object o );
e.g.
Boolean b = new Boolean(true);
Someclass.printType( b ); // prints "Boolean"
boolean b2 = true;
Someclass.printType( b2 ); // prints "boolean"
The second invocation gives me a compile error:
printType(java.lang.Object) in Someclass cannot be applied to (boolean)
What am I missing ?
Thanks
Pho
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> boolean b2 = true;
You are trying to pass a primitive to a method that accepts an object. That's why you are getting a compile error. The first invocation is legal because "Boolean" is an object.

Bosun
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic