wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what determines an object's methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what determines an object Watch "what determines an object New topic

what determines an object's methods

Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, I need to know what determines an instance's methods, the reference type or the actual instance type? I have heard that it was both. Quite confused.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Instance methods are called based on the type of the object being referenced.
Static methods are called based on the type of the variable not the object being referenced.
This piece of code should helpd you out:

Sun Certified Programmer for the Java� 2 Platform

Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Dave, can you look at the following code?
interface a {
void methodOne();
class b implements a{
void methodOne(){
void methodTwo(){
public static void main(String [] args){
a an_A = new b();
This program does not compile because javac says an_A, being of reference type a, does not have methodTwo. Even thoough an_A's actual instance type is b, which has methodTwo. And methodTwo is an instance method no?
chin josei

Joined: Jun 22, 2001
Posts: 25
In the program an_A is of reference type A.
At compile time compiler will look for methodTwo() in A..which infact is defined in b.Hence it gives a compile time error.
leena rane
Ranch Hand

Joined: Aug 13, 2001
Posts: 280
Will u (or anyone else )tell me in details.
I am not clear with this.
Thanx in advance.
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2373

There r two kinds of methods to be considered --- static methods and non-static methods! Static methods r identified in compile- time while non-static methods r identified at runtime -- possibly both(:confused static/non-static methods resides in a memory area called stack...
Non-static methods r identified at runtime and if overloaded the more specific method is chosen i.e. the method of the object's class type(or may be of subclass) not of the object's reference type (or may be of super class) is chosen. Hope it helps!

Muhammad Ashikuzzaman (Fahim)
Sun Certified Programmer For Java 2 Platform
--When you learn something, learn it by heart!

Ashik Uzzaman
Senior Software Engineer, TubeMogul, Emeryville, CA, USA.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks Dave! Great piece of code
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
But methodTwo, which is an instance method, was determined not resolvable by the reference type, isn't it? What I like to know is to what extent does the reference type determine and to what extent does the acual instance type determine.
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Moderators please help...
chin josei

Joined: Jun 22, 2001
Posts: 25
Consider this piece of code
1 class Shapes
2 {
3 public void draw()
4 {
5 System.out.println("Shapes");
6 }
7 }
9 class Circle extends Shapes
10 {
11 public void draw()
12 {
13 System.out.println("Circle");
14 }
15 }
17 public class Main
18 {
19 public static void main(String args[])
20 {
21 Shapes sh = new Shapes();
23 Circle cir = new Circle();
25 Shapes sh1 = new Circle();
27 sh.draw(); // This will print shapes
29 cir.draw(); // This will print circle
31 sh1.draw(); // This will again print circle
32 }
33 }

1. At compile time compiler checks whether method is present
in the reference type.If not it gives a compile time error.
For example comment the draw() method in Shapes and try to compile
Compiler will crib saying -- can't resolve symbol draw() --

2. At runtime the exact object's method is executed(Not Always).
In the above case at line -21- reference 'sh' points to a Shapes object so ..draw()
method in Shapes class is executed.Similarly at line -23- circ points to a Circle
objet hence "Circle" will be printed.
Now take the third case...line -25-
Reference Type is of Shapes and it points to Circle object.Since there is a method
draw() defined in Shapes it will compile properly(Try commenting that compiler gives
a compile error).Now at run time the reference type will be pointing to Circle object
hence draw method of circle will be executed...
Try commenting the draw() method in Circle ...
draw() method of Shapes will be invoked...Here it executes the reference's(Shapes) method.
In this case eventhough the object is of type Circle , Shape's method is executed.
Hope this helps
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
So the reference type determines whether a method should be available, and the actual instance type decided which method should be called.
Thank you chin josei!
I agree. Here's the link: http://aspose.com/file-tools
subject: what determines an object's methods
It's not a secret anymore!