This question already has answers here:
Overloading method calls with parameter null [duplicate]
(5 answers)
Closed 7 years ago.
I fail to understand that why in the following code the output is "String Version". As everything is Derived from Object then why it matches to String version?
public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[]) throws Exception
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Output:
String Version
Java Code
You have to pass an object for it to take object verion
public class TestProgram {
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
TestProgram question = new TestProgram();
question.method(question);
}
}
Related
This question already has answers here:
When do I use super()?
(11 answers)
Closed 6 years ago.
public class polymorphism {
public void show()
{
System.out.println();
}
public void show(int i)
{
System.out.println("6");
}
public class B extends polymorphism
{
}
/** * #param args the command line arguments */
public static void main(String[] args)
{
// TODO code application logic here B obj=new B(); obj.show();
}
}
To call a method in an object's parent class, you can use the 'super' keyword.
Ex. super.show();
public class Test {
public static void main(String[] args) throws ClassNotFoundException {
new Test().f("dfffg"); // it is running perfectly
new Test().f(1, 1); // it is giving ambiguity
}
public void f(int a,long b){
System.out.println("in int and long");
}
public void f(long a,int b){
System.out.println("in long and int");
}
public void f(String s) {
System.out.println("in String");
}
public void f(StringBuffer o) {
System.out.println("in String bufer");
}
public void f(Object o){
System.out.println("in object");
}
}
when i execute this new Test().f("dfffg"); it is running perfectly although we have overload method with StringBuffer and Object as parameter
while f(1,1) giving ambiguity, which I can understand.
new Test().f("dfffg") matches the signatures of both public void f(String s) and public void f(Object o). When you are passing an object parameter, the method having the most specific argument type is chosen: in this case public void f(String s). String is more specific than Object since String is a sub-class of Object.
public void f(StringBuffer o) is not relevant in this example, since a StringBuffer is not a super-class of String, so you can't pass a String to a method that expects a StringBuffer.
This question already has answers here:
How to do method overloading for null argument?
(7 answers)
Closed 6 years ago.
I am very surprised why output is very much different from what I am expecting , I have two overloaded methods, one having one String and the other an Object as parameter, while calling this method with null parameter, output is just printing
"String"
and not calling method having object as parameter.
Why does Java selects the method having String as parameter, how java determines which overloaded method to call ?
class TestingClass {
public void test(String s) {
System.out.println("String");
}
public void test(Object o) {
System.out.println("Object");
}
public static void main(String[] args) {
TestingClass q = new TestingClass();
q.test(null);
}
}
There is no overriding in this code, only method overloading. When the compiler has to choose which of the two test methods to execute (since null can be passed to both), it chooses the one with the more specific argument type - test(String s) - String is more specific than Object since it is a sub-class of Object.
The other method can be called using :
q.test((Object) null);
Without adding a possible new answer, let me suggest you to extend your code like following,
public class Overload {
public static void main(String[] args) {
new Overload().test(null);
}
public void test(String s) {
System.out.println("String");
}
public void test(Object o) {
System.out.println("Object");
}
public void test(Integer s) {
System.out.println("Integer");
}
}
Comment out any one of Object or Integer version any see it for yourself. The answer being already provided by others.
This question already has answers here:
Can someone explain a void return type in Java?
(5 answers)
Closed 7 years ago.
I get the following error in the print method
Void methods cannot return a value
how to correct the code in method print?
abstract class Motor{
int fuel;
int getFuel(){
return this.fuel;
}
abstract void run();
}
public class Player extends Motor {
void run(){
print("wroooooom");
// public static void main(String[] args) {
}
private void print(String string) {
return string; // Void methods cannot return a value
}
}
You have written the following code:
private void print(String string) {
return string;
}
But void here is the return type. It thus means you cannot return anything. If you write String instead of void then it will mean that the return type will be a string which is the case and the error will go away.
It should be like this:
private String print(String string) {
return string;
}
Or as it is a print function if you want to keep it void then print the string there itself.
As requested in comment here is how the code should be. The main
function should be inside Player class. Then we define an object of
the Player class in the main function to call its methods.
abstract class Motor{
int fuel;
int getFuel(){
return this.fuel;
}
abstract void run();
}
public class Player extends Motor {
public void run(){
print("wroooooom");//calling print method to print passed string
}
public void print(String string) {
System.out.print(string);
}
public static void main(String []args){
Player p1 = new Player();//creating a object of Player class to access its methods
p1.run();//calling the run method
}
}
This question already has answers here:
Strange Java null behavior in Method Overloading [duplicate]
(4 answers)
Closed 9 years ago.
public class Test {
public void method(String param)
{
System.out.println("String version");
}
public void method(StringBuffer param)
{
System.out.println("String Buffer");
}
public static void main(String args[])
{
Test test=new Test();
test.method(null);
}
}
This code result is compilation error says “reference to method is ambiguous”
public class Test
{
public void method1(Object param)
{
System.out.println("Object Version ");
}
public void method1(String param)
{
System.out.println("String Version ");
}
public static void main(String[] args)
{
Test test=new Test();
test.method1(null);
}
}
This code result is “String Version”
Actually I can’t understand the result of second piece of code. Why don’t both piece of code has the same result
In first case,
null is a subtype of every other reference type.
So, compiler finds ambiguity in deciding which method to call..
In second case, it finds more specific object for null which happens to be String. Hence it calls method1 and prints String Version