• 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

Upcasting

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need some information from you Java boffins :
View the following code from Bruce Eckel's - Thinking in Java :
//: c06:FinalOverridingIllusion.java
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
import com.bruceeckel.simpletest.*;
class WithFinals {
// Identical to "private" alone:
private final void f() {
System.out.println("WithFinals.f()");
}
// Also automatically "final":
private void g() {
System.out.println("WithFinals.g()");
}
}
class OverridingPrivate extends WithFinals {
private final void f() {
System.out.println("OverridingPrivate.f()");
}
private void g() {
System.out.println("OverridingPrivate.g()");
}
}
class OverridingPrivate2 extends OverridingPrivate {
public final void f() {
System.out.println("OverridingPrivate2.f()");
}
public void g() {
System.out.println("OverridingPrivate2.g()");
}
}
public class FinalOverridingIllusion {
private static Test monitor = new Test();
public static void main(String[] args) {
OverridingPrivate2 op2 = new OverridingPrivate2();
op2.f();
op2.g();
// You can upcast:
OverridingPrivate op = op2; // !!!
// But you can't call the methods:
//! op.f();
//! op.g();
// Same here:
WithFinals wf = op2;
//! wf.f();
//! wf.g();
monitor.expect(new String[] {
"OverridingPrivate2.f()",
"OverridingPrivate2.g()"
});
}
} ///:~
Question : Refering to the line marked / !!! /
can anyone tel me nuts and bolts level how the virtual machine intreperts
this line : OverridingPrivate op = op2;
As i see it , op2 is a reference to an object of a certain type ie
xyz@abc and op and object reference (I think) of an object of another type
ie abc@qwe. Thus : I have different objects with the different references which are set equal ! Now I'm off the wagon . I always thought inheritance involved getting money from Gramps !
Can anyone please advise
Regards
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Please note that op2 is an object of type OverridingPrivate2. If you take a look at the class OverridingPrivate2 you will see that that class extends from OverridingPrivate. This means that OverridingPrivate2 has "isa" relationship with OverridingPrivate. just like when a rectangle extends from shape, a rectangle 'is a' shape.
The same applies here. because OverridingPrivate2 'is a' OverridingPrivate one can see OverridingPrivate2 as a OverridingPrivate! just like one can see a rectangle as a shape.
In java this results in the fact that you can 'upcast' objects of a subtype to a supertype.
Hope this helps?
Rikko
[ October 02, 2003: Message edited by: Rikko Verrijzer ]
 
Deon Botha
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rikko
Thanks a lot !
I have lost track of the "is a " relation ship , its now a lot more clearer
Regards
Deon
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Boffin??
OK, had to look it up
World Wide Words:Boffin

His argument was that boffin is in common usage a jaded word that borders on the offensive; the word �conjures up images of weird old men in flapping lab coats, pouring strange chemicals into test tubes�


(Significant sections omitted to make the quote appear more radical )
 
reply
    Bookmark Topic Watch Topic
  • New Topic