• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Doubt in overloading

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


which method would be invoke if the code at line 5 is
method.invoke(null,null);
if i disable the method 4, method.invoke(null,null)would invoke which method or occur a compile err?

i found a way to get the answer but im not sure it will always do the right things. For example,
first list out the inhirency chain and mark them a value like following:

animal 2
|___cat 1
null 0

second caculate them with the methods param list.
result
method.invoke(null,null); / 0 / 0
1.void invoke(animal a,animal b){} / 2 / 2 / (2-0)+(2-0)
2.void invoke(animal a,cat b){} / 2 / 1 / 3
3.void invoke(cat a,animal b){} / 1 / 2 / 3
4.void invoke(cat a,cat b){} / 1 / 1 / 2

the No.4 result is min, so the code method.invoke(null,null)will invoke the method invoke(cat, cat)
if i disable the method 4,then there will be two same result 3 and 3,so it occur compile err becauseof ambigious method param list.

Is it a right way? and some one can show me the rules for invoking a method by the param list?thanx.
[ June 07, 2005: Message edited by: aleyx chow ]
 
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess your approach is right but someone should give a more specific explanation. Anyone???
 
Ranch Hand
Posts: 3851
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method which is more specific, always get called but if compiler feel problem in deciding specific one then gives 'ambigous method' error.

Hope it helps.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class animal{}
class cat extends animal{}
class methods{
void invoke(animal a,animal b){System.out.println(" A A");} //method 1
void invoke(animal a,cat b){System.out.println(" A C");} //method 2
void invoke(cat a,animal b){System.out.println(" C A");} //method 3
void invoke(cat a,cat b){System.out.println(" C C");} //method 4
}
public class Test14{
methods method=new methods();
//insert code here Line 5
method.invoke (new animal, new animal);
}

why this error is comming...??


D:\java_prac>javac Test14.java
Test14.java:12: <identifier> expected
method.invoke (new animal, new animal);
^
1 error
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit,
I could be missing a larger issue here but you should have parentheses on your 'animal' constructors as in: method.invoke(new animal(), new Animal());

HTH,
Tony
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by amit taneja:

public class Test14{
methods method=new methods();
//insert code here Line 5
method.invoke (new animal, new animal);
}

why this error is comming...??

1 error

How can one invoke a method from a class?

Shouldn't it be something like

public class Test14{
methods method = new methods();
public static void main(String[] args)
{
Test14 test = new Test14();
test.method.invoke(new animal(), new animal());
}
}

?

 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nikhil Pancholi:

Originally posted by amit taneja:
[qb]
public class Test14{
methods method=new methods();
//insert code here Line 5
method.invoke (new animal, new animal);
}

why this error is comming...??

1 error

How can one invoke a method from a class?

Shouldn't it be something like

public class Test14{
methods method = new methods();
public static void main(String[] args)
{
Test14 test = new Test14();
test.method.invoke(new animal(), new animal());
}
}

?[/QB]




ya ur right..i don't know how i ignore such silly thing..



 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldnt understand the reasoning, I apologize.
It would be great if anyone can explain in layman terms.

Thanks
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



How can one invoke a method from a class?

Shouldn't it be something like

public class Test14{
methods method = new methods();
public static void main(String[] args)
{
Test14 test = new Test14();
test.method.invoke(new animal(), new animal());
}
}




This is wrong. This will produce a compile time error. Since we are not dealing with any inner classes we don't need a instance of Test14.

method.invoke (new animal(), new animal());
The above method call is correct and will produce output as :
A A
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic