aspose file tools*
The moose likes Java in General and the fly likes OOPS Concept calling method with String and Object type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "OOPS Concept calling method with String and Object type" Watch "OOPS Concept calling method with String and Object type" New topic
Author

OOPS Concept calling method with String and Object type

Sunny Kumar Kalra
Greenhorn

Joined: Oct 10, 2010
Posts: 15
I have two questions related to OOPS concepts but need help ...

Q1)
class sample {
void callme3(String ob) {
System.out.println("String called");
}
}
public class OOPSConcept extends sample{

void callme3(Object ob) {
System.out.println("Object called");
}

public static void main(String args[]) {
sample ob3= new OOPSConcept();
ob3.callme3(""); // String called
ob3.callme3(null); // String called
}
}
Why always the method from super class gets called with ob3.callme3("") and ob3.callme3(null). Even if I interchange i.e put Object argument in Supers callme3 and String argument in sub's callme3 .. then also method from super class gets called . Is this OverLoading or Overriding ?


Q2)
public class OOPSConcept{
void callme4(Object ob){
System.out.println("callme4 : Object called");
}
void callme4(String ob){
System.out.println("callme4 : String called");
}

public static void main(String args[]) {
OOPSConcept ob2= new OOPSConcept();
ob2.callme4(""); //callme4 : String called
ob2.callme4(null); //callme4 : String called

}
}

Here in above, Why always method with String arguments gets called and not Object even with null ? this is overloading
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36598
    
  16
1: You have not got an overridden method. You have an overloaded method. Go through the Java Language Spoecifcation with ctrl-F-overload and you find 4 sections: 1 2 3 4. Those should explain how the compiler deals with overloading. I do not think you can call that polymorphism, because it is all sorted out at compile time.

When the compiler tries to disambiguate overloaded methods, it looks at the argument and tries to get the most specific fit possible Obviously "" is a String, so that is more specific than Object.
The null argument might be a String, so the compiler tries the more specific argument type, so it calls the (String) method.
Try overloading it again withYou cannot now disambiguate the Integer call from the String call when you pass null and the compiler will complain bitterly.

In future please use the code button, so we can read the indented code properly.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36598
    
  16
2: I think that is the same question, and I think you get the same answer as No 1.
Siva Vulchi
Ranch Hand

Joined: Nov 17, 2011
Posts: 30
Why always method with String arguments gets called and not Object even with null ? this is overloading


When compiler senses ambiguity, a method with subclass argument would get a preference over the method with super class argument.

For better understanding refer Choosing the Most Specific Method - Tricky Overloading
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36598
    
  16
Quite a good link. The only thing about it is, it only uses static members, unlike your example which appears to use overriding, but actually uses only overloading.
Sunny Kumar Kalra
Greenhorn

Joined: Oct 10, 2010
Posts: 15
Campbell Ritchie wrote:1: You have not got an overridden method. You have an overloaded method. Go through the Java Language Spoecifcation with ctrl-F-overload and you find 4 sections: 1 2 3 4. Those should explain how the compiler deals with overloading. I do not think you can call that polymorphism, because it is all sorted out at compile time.

When the compiler tries to disambiguate overloaded methods, it looks at the argument and tries to get the most specific fit possible Obviously "" is a String, so that is more specific than Object.
The null argument might be a String, so the compiler tries the more specific argument type, so it calls the (String) method.
Try overloading it again withYou cannot now disambiguate the Integer call from the String call when you pass null and the compiler will complain bitterly.

In future please use the code button, so we can read the indented code properly.


Even if you place object argument in super class and string argument in subclass, then also method of superclass will be called . But How its happening?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36598
    
  16
Sunny Kumar Kalra wrote: . . .

Even if you place object argument in super class and string argument in subclass, then also method of superclass will be called . But How its happening?
Please don’t quote posts in their entirety; it simply makes the thread longer to no good purpose.

Are you sure it isn’t the other way round, that the String version of the method is called? And forget about subclasses and superclasses. You are not overriding anything, so the usual rules of overloading apply.
Sunny Kumar Kalra
Greenhorn

Joined: Oct 10, 2010
Posts: 15
@Campbell Ritchie

Yes I am sure. You can try yourself by running the code. Dont know whats the basis of this
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 213

Even if you place object argument in super class and string argument in subclass, then also method of superclass will be called . But How its happening?

@Sunny- Take a chill man.. if you swap your methods that is if you put the method containing the "String" arg in the Sub class and method having "Object" argument in the SuperCLass then always it will call the Super class method..
And here you are OVERLOADING methods(that is because of different kinds of arguments viz. String and Object).. So methods are called on their "REFERENCE" types..



Here obj3 is a reference type. so the method in Sample class only will be called.

Now taking the another scenario if you you are taking both methods in the Sample class.. Then it will always call method having "String" arguments. This is because String is the subclass of Object(as you know) and compiler will choose the SPECIFIC one(that is subclass and not the super class).

SubClass and Superclass mostly comes into the picture when you are OVERRIDING the methods. your scenario of code is only OVERLOADING the methods.

Hope this helps.

OCPJP
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36598
    
  16
You obviously understood the question better than I did.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 213

Campbell Ritchie wrote:You obviously understood the question better than I did.


Its no that campbell i just gave a brief understanding on it. As he was getting confuse between Overloading and Overriding.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OOPS Concept calling method with String and Object type
 
Similar Threads
Object's method
Pattern and matcher
Parameter passing into a method-Examlab
Passing null as reference
Class Already Defined