• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what determines an object's methods

 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Thanks.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cameron
BOTH
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:



------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave, can you look at the following code?
interface a {
void methodOne();
}
class b implements a{
void methodOne(){
System.out.println("methodOne");
}
void methodTwo(){
System.out.println("methodTwo");
}
public static void main(String [] args){
a an_A = new b();
an_A.methodTwo();
}
}
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?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cameron,
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.
chin
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chin,
Will u (or anyone else )tell me in details.
I am not clear with this.
Thanx in advance.
 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
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!
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave! Great piece of code
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Thanks.
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moderators please help...
 
chin josei
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider this piece of code
1 class Shapes
2 {
3 public void draw()
4 {
5 System.out.println("Shapes");
6 }
7 }
8
9 class Circle extends Shapes
10 {
11 public void draw()
12 {
13 System.out.println("Circle");
14 }
15 }
16
17 public class Main
18 {
19 public static void main(String args[])
20 {
21 Shapes sh = new Shapes();
22
23 Circle cir = new Circle();
24
25 Shapes sh1 = new Circle();
26
27 sh.draw(); // This will print shapes
28
29 cir.draw(); // This will print circle
30
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
chin
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic