• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Overloading

 
Krishnan Sreeraman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI

I am new to java. I am really new Object Oriented Programming please help me

resolve this issue.I compiled the following code

class Super {
public String getNum(float a) {
return("You have landed in super class");
}
}

class Sub extends Super {

public String getNum(int a)
{
return ("You have landed in sub class" );
}
}

class Test

{

public static void main(String args[]){

String a;
Super s= new Sub();
int i=45;
a=s.getNum(i);
System.out.println(a);
}
}


But I was expecting the result as you have landed in sub class but

I got the result as you have landed in super class

Can any one reason out why this had happened?

Is this not Method Overloading
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi krishnan,
Its method overloading. In overloading the method called will be of reference type. On your code reference type is of Super so you are getting that output.



Regards
Nik
 
Rajeev Ja
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya thats right its overloading but i still have doubt because overloading occurs within a single class and overriding between classes.
it also shows polymorphism.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Nik pointed out, you are using a supertype reference.

In the subclass you have inherited a method from the superclass.

So you have two methods in the subclass.

However, when you use a supertype reference to refer to an instance of the subclass, you can only call methods that are visible to the superclass.

The superclass doesn't know about the method that accepts an int.
 
Bridget Kennedy
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with the comments so far regarding visibility using the Super object reference. But, there is more to learn from your example.

First, there is something to be learned about automatic casting of primitive data types in your original example. Even though you passed an int argument value, it was converted to a float for the purposes of the Super:getNum(float a) method. As per prior responses, this happened because your object reference was of type Super.

Try playing with your Test class as follows to demonstrate some of the concepts of method overriding vs overloading and what happens when you change the object reference type.

First try using the Sub object reference instead of Super:



Then try using different input argument types:


Then try changing the method signature of Super:getNum to accept an int instead of a float. Now Super and Sub identify getNum with the same argument type. This switches to an example of method overriding.

Then try:


and then try:

[ June 01, 2007: Message edited by: Bridget Kennedy ]
 
Bill Cruise
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith is right. The original code you posted isn't using method overriding because the Super class doesn't have a method that accepts an int. In the Test code, the implicit cast from int to float takes precedence. The reference to the super type never sees the Sub method.

Try changing the input parameter in the Super class method to an int to see an example of method overriding.
 
Krishnan Sreeraman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I Thank all for Helping me resolve this
 
Narendra Kumar Pinnaka
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A
{
}
class B extends A
{
}

public class OverLoadTest
{
void printMe(B b)
{
System.out.println(" I am B and My Value is "+b);
}
void printMe(A a)
{
System.out.println(" I am A and My Value is "+a);
}

public static void main(String []args)
{
A a = new A();
B b = new B();
OverLoadTest o = new OverLoadTest();
o.printMe(a);
o.printMe(b);
o.printMe(null);
}
}
can some one help me how "I am B and My Value is " is printing when i pass null
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

When you make a call to an overloaded method and the compiler has to choose between several methods that all apply, the rule is that the compiler picks the "most specific" one. If there are two methods, and one applies to a subclass and one to a superclass, then the subclass one is "more specific". Here, "null" matches both methods, since you can have a null reference of any class type, so the compiler picks the more specific one.

In the future, please start a new thread of your own to ask your own questions!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic