File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Method Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method Overloading" Watch "Method Overloading" New topic

Method Overloading

Krishnan Sreeraman

Joined: Jun 01, 2007
Posts: 12

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;

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

Joined: Apr 26, 2007
Posts: 652
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.

Rajeev Ja
Ranch Hand

Joined: May 06, 2007
Posts: 38
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

Joined: Feb 07, 2005
Posts: 2398
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

Joined: Nov 30, 2004
Posts: 86
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

Joined: Jun 01, 2007
Posts: 148
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

Joined: Jun 01, 2007
Posts: 12
I Thank all for Helping me resolve this
Narendra Kumar Pinnaka

Joined: Jul 05, 2007
Posts: 9
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();
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

Joined: Jul 08, 2003
Posts: 24199


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!

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Method Overloading
It's not a secret anymore!