aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Integer inheritence problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Integer inheritence problem" Watch "Integer inheritence problem" New topic
Author

Integer inheritence problem

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Please take look at this guys:

class base {
int base=1;
void print() { .....}
}

class sub extends base {
int sub=2;
int base=99;
void print { System.out.print(sub); }
}

base B1 = new sub(); // <---- Different reference type and object type.

i know only methods are inherited, therefore when i try:

System.out.println(B1.sub); // gives me a error. This is OK, bcos B1 does not know about variable "sub"

But:
B1.print() // prints the 2!! The sub value.
How come B1 knows about variable "sub"?

As far as I know, because the reference type is base, only about variables and methods in "base" are known by B1. So how come B1.print() output "2" ?

Thanks guys..
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

Hi Maduranga Liyanage,

The fact that the reference type is of base and the actual object is a sub object, the sub object can use any member (if access modifer allows it) which the base class offers it. Remember that the sub object is STILL A SUB
object. So when you try to print out a "sub" field, it will.

I hope i expressed myself properly.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
HI..

But then why cant I say:

System.out.print(B1.sub); // gives compile error..
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Basically, you have a "sub" object, that you are using as a "base" object. It is printing 2 because you have a "sub" object. Accessing the sub variable, is a compile time error because you are trying to use it as a "base" object.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nalika Amarasinghe
Greenhorn

Joined: May 17, 2005
Posts: 10
Hi Maduranga,

To clarify more on above replies, here is the rule of thumb :

1. Object type determine which overridden method is invoked. This is decided at runtime time.

I'm adding following rule also eventhough its not relavant here.

2. Reference type (not the object type) determine which overloaded method is invoked. This is decided at compile time.

In your case B1.print() invokes the overridden method. Hence prints 2.Hope this helps.

Regards
Nalika
[ May 31, 2005: Message edited by: Nalika Amarasinghe ]
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Hi Nalika..

Regarding your point 2), can you clarify me a bit what you mean by determining which overloaded method?
Overloaded methods behave as seperate methods and do not have any relavence to super class methods, do they??

Also can you kindly explain the theoretical explanation between the difference of "referrence type" and "object type"? What is actually the difference between below statements:

base_class base_ref_base_object_class = new base_class();
base_class base_ref_sub_object_class = new sub_class();


Thanks...
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11419
    
  16

Think of the reference type as a remote control (this is explained better in some of the tutorials on the Ranch). I can have a remote control to a basic XYZ brand tv. it has a power button, volume up and down, and channel up and down buttons. it will work with ANY XYZ brand TV.

but then my piture tube blows, so i get a second hand, fancier XYZ tv, with built in surround sound functionality. I can still use my basic remote. when i push the 'power' button, the fancy XYZ tv will turn itself on. The remote just says "hey - turn on" and the TV uses it's own logic to figure out how to do that.

but i can't use my basic remote to control the surround sound features - they're simply not on my remote, so i can't press those buttons. the TV could DO all that stuff, i just don't have any way to TELL it to do them - unless i get a new remote that's the right kind.

in your last example, i have two base references. using those references, i can only call the funtions known to the base class. but when you call a method with the base_ref_sub_object_class reference, since the ACTUAL OBJECT is a sub_class, you'll get the method defined in the sub_class.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Hi Fred..
Great explanation.. Thanks a lot.. Really helped me..
 
Consider Paul's rocket mass heater.
 
subject: Integer inheritence problem