aspose file tools
The moose likes Beginning Java and the fly likes Clarification on : Overloading vs polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Clarification on : Overloading vs polymorphism" Watch "Clarification on : Overloading vs polymorphism" New topic
Author

Clarification on : Overloading vs polymorphism

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 757

Please confirm that polymorphism is a different beast from method overloading.
Thanks
Pho

Regards,

Pho
kyle amburn
Ranch Hand

Joined: Jul 29, 2001
Posts: 64
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
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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


"JavaRanch, where the deer and the Certified play" - David O'Meara
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 757

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
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
>> 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


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Clarification on : Overloading vs polymorphism
 
Similar Threads
polymorphism and overloading
Overloading is Polymorphism?
polymorphism,overloading and overiding seems to be the same thing for me
is overloading example of static or dynamic polymorphism
Method Overloading and overriding