wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method innvocation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method innvocation" Watch "Method innvocation" New topic
Author

Method innvocation

Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Hi,

(I have asked a similar question to this previously, but I want to be clear...that I have it clear today)

If we have an object reference, it is of a certain reference type and refers to a certain object, which has a certain type.

With regard to invoking methods can someone just say if I am wrong with any of the following (don't pull any punches):

If the method is overridden, then the method chosen is determined by the type of the OBJECT, NOT the type of the reference.

If the method is overloaded, then the method chosen is determined by the type of the REFERENCE, NOT the type of the object.

If the method is neither overloaded NOR overidden, then the method will be chosen based on the reference type, which is why a3.attack(); fails in the code below:



Output:

Eating dog food.
Drinking milk.
Eating attack-dog food.
Attacking!
Eating attack-dog food.
Eating attack-dog food.
Eating burglar

If I have made any bloopers please let me know.

Cheers,

Si.


SCJP 1.4 (93%)<br />SCJD (In progress. It can run, but it can't hide...)
Chad Naber
Greenhorn

Joined: Mar 25, 2005
Posts: 8
Hi Simon,

This is my first post here, so take it with a grain of salt. I too am preparing for the SCJP.

You are correct that a3.attack(); would fail. The way I like to think about reference variables is that they are remote controls to the object, and the buttons on the remote are the methods and instance variables of the reference type.

therefore

Animal a3 = new AttackDog();

would create a remote with one button on it...eat(). When you try to press the attack() button on it, even though an AttackDog() can attack, an Animal reference doesn't know this, so the reference variable has no way to activate the attack() method.

This would work with

AttackDog a3 = new AttackDog();

but that's pretty obvious

Good luck with the SCJP!
[ March 25, 2005: Message edited by: Chad Naber ]
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
hi

have it in a simple form

Variables,private methods,static methods uses references

where

public & Protected uses objects

I hope its easy to have it in mind

With Kind Heartly..........M.VIDYASAGAR
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729


Your first two lines of comment are irrelevant, your third comment line says it all. The above fails to compile simply because a3, declared to be an Animal (which can only eat()), cannot attack(). It is nothing to do with overriding or overloading, it's just a matter of what an Animal can do.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729


The first comment is irrelevant, you are telling an AttackDog to eat(). It is declared to be able to do that.

We know that d1 really refers to an AttackDog because of:

[ March 26, 2005: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729


Again, this fails only because eat(String) is undefined in Dog. Nothing to do with an AttackDog overriding or overloading the eat method.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729


Your comments are correct if you are sure that:
d1 refers to an AttackDog which is declared to be able to eat(String). It is also declared to be able to eat(). So AttackDog has overloaded the method eat.

First analyse the references and then look for the inheritance, overriding, or overloading of the methods.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method innvocation
 
Similar Threads
Dynamic dispatch
Polymorphism - brain to jelly
difference between redefining and overriding
Interface question??
method invocation problem