Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Difference between compile time and runtime reference/objects

Reply
Thread Tools

Difference between compile time and runtime reference/objects

 
 
lielar
Guest
Posts: n/a
 
      03-10-2008
Hi

I have the following code
-------------------------------------------------
interface I {
int i = 0;
}

class A implements I {
int i = I.i + 1;

}

class B extends A {
int i = I.i + 2;

static void printAll(A obj) {
System.out.println(obj.i);
}

public static void main(String [] args) {
B b = new B();
A a = new B();
printAll(a);
printAll(b);
}

}


-----------------------------------------------------

I get 1, 1. As the second object I passed is of reference and object
type B , why is it that the answer it prints? Without overloading the
method, can I make it print '2'?
 
Reply With Quote
 
 
 
 
Wojtek
Guest
Posts: n/a
 
      03-10-2008
lielar wrote :
> Hi
>
> I have the following code
> -------------------------------------------------
> interface I {
> int i = 0;
> }
>
> class A implements I {
> int i = I.i + 1;
>
> }
>
> class B extends A {
> int i = I.i + 2;
>
> static void printAll(A obj) {
> System.out.println(obj.i);
> }


The signature for the printAll method specifies the 'A' class. Since
'B' extends 'A', 'B' can be passed in, however it will be treated as an
'A'.

> public static void main(String [] args) {
> B b = new B();
> A a = new B();
> printAll(a);
> printAll(b);
> }
>
> }


So printAll(a) is a 'B', which is then treated as an 'A'.
printAll(b) is also treated as an 'A'

> -----------------------------------------------------
>
> I get 1, 1. As the second object I passed is of reference and object
> type B , why is it that the answer it prints? Without overloading the
> method, can I make it print '2'?


No. You must have:

static void printAll(B obj) {
System.out.println(obj.i);

The compiler will then choose the proper method for the class you are
passing in.

Side note: Always use properly named variables, classes. There is some
confusion with:

B b = new B();
A a = new B();

--
Wojtek


 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      03-10-2008

"lielar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi
>
> I have the following code
> -------------------------------------------------
> interface I {
> int i = 0;
> }
>
> class A implements I {
> int i = I.i + 1;
>
> }
>
> class B extends A {
> int i = I.i + 2;
>
> static void printAll(A obj) {
> System.out.println(obj.i);
> }
>
> public static void main(String [] args) {
> B b = new B();
> A a = new B();
> printAll(a);
> printAll(b);
> }
>
> }


You have 2 instance variables both called i, which is almost always a
mistake. printAll() selects the instance variable based on the compile-time
type of the expression, so it always hits the one in A.

You can make printAll print '2' for B by allowing it dispatch through
object. You don't have to overload any methods to do this.
1) add getI () { return i; } to class A
2) replace the duplicate declaration of "i" in B with i = I.i + 2 in its
constructor
3) Make printAll use the method
System.out.println (obj.getI ())

Cheers,
Matt Humphrey http://www.iviz.com/


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-11-2008
"lielar" <(E-Mail Removed)> wrote
>> I have the following code
>> -------------------------------------------------
>> interface I {
>> int i = 0;
>> }


It's bad practice to declare member variables in an interface. There's even a
name for the practice: the Constant Interface Antipattern.

The purpose of an interface is to define behavioral contracts, not implementation.

>> class A implements I {
>> int i = I.i + 1;


Fugly.

Consider defining instance variables as private nearly always. You need an
affirmative justification to relax the visibility even to package-private, as
you've done here.

This instance variable 'i' hides the superinterface static variable 'i'.

>> }
>>
>> class B extends A {
>> int i = I.i + 2;


This instance variable 'i' hides the superclass instance variable 'i'.

>> static void printAll(A obj) {
>> System.out.println(obj.i);
>> }
>>
>> public static void main(String [] args) {
>> B b = new B();
>> A a = new B();
>> printAll(a);
>> printAll(b);
>> }
>> }


Matt Humphrey wrote:
> You have 2 instance variables both called i, which is almost always a
> mistake. printAll() selects the instance variable based on the compile-time
> type of the expression, so it always hits the one in A.


The principle here is that variables do not override, only methods do.

<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3>:
> If the class declares a field with a certain name,
> then the declaration of that field is said to hide any and all accessible
> declarations of fields with the same name in superclasses,
> and superinterfaces of the class.


That explains the value of Matt's advice:
> You can make printAll print '2' for B by allowing it dispatch through
> object. You don't have to overload any methods to do this.
> 1) add getI () { return i; } to class A
> 2) replace the duplicate declaration of "i" in B with i = I.i + 2 in its
> constructor
> 3) Make printAll use the method
> System.out.println (obj.getI ())


--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert time to ruby time to calculate the time difference. Ruwan Budha Ruby 4 03-09-2011 04:43 PM
computation at compile time i.e. compile time functions usingtemplates Carter C++ 2 03-04-2009 06:43 PM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments