Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OOPS Concept calling method with String and Object type

 
Sunny Kumar Kalra
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2: I think that is the same question, and I think you get the same answer as No 1.
 
Siva Vulchi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You obviously understood the question better than I did.
 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic