jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Question on inheritance! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question on inheritance!" Watch "Question on inheritance!" New topic
Author

Question on inheritance!

Vanchi Nathan
Ranch Hand

Joined: Feb 24, 2004
Posts: 107
It is declared in the API that the class Graphics2D extends from Graphics class. In an example given in the tutorials on java.sun.com, i came accross a code that is as follows:

public void paint(Graphics g) {
...
Graphics2D g2 = (Graphics2D) g;
...
}

How is this possible? i belive that casting to a Sub class from a Base clase is not possible because i tried my own example.

Any explain how this is possible. Thanks in advance.

vanchin


Best regards,<br /> <br />vanchin
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

what example did you try that didn't work?

This isn't quite an advanced question, so I'm moving it to the intermediate forum, but I'll follow up with you there once you post your example that didn't work.
[ December 16, 2004: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Vanchi Nathan
Ranch Hand

Joined: Feb 24, 2004
Posts: 107
the example i tried is attached below:

When i execute the above code i get an ClassCastException at <b>Pet doggy = (Pet) wild;</b> line.

thanks
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
I sure hope I beat Jessica to this...

That cast doesn't work, becuase wild ISN'T a Pet. You constructed it as an Animal... it doesn't have the facilities to work as a Pet. If you had said:

Animal wild=new Pet();

you could cast down, becuase wild was constructed to be a Pet. Similarly, in your Graphics example, the author is assuming that the Graphics in question is a Graphics2d. If it turns out that it isn't and was cunstructed some other way, you'll get your ClassCastException.

dig?
[ December 16, 2004: Message edited by: Nick George ]

I've heard it takes forever to grow a woman from the ground
Vanchi Nathan
Ranch Hand

Joined: Feb 24, 2004
Posts: 107
So, does it mean that the Graphics object used in the paint() method is created by the Graphics2D constructor and is down casted as Graphics object?

say like:

Graphics g = new Graphics2D();

thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11168
    
  16

In whatever method calls your paint method, you could just be passing in a Graphics2D object. Since it IS a Graphics object, the contract is valid, and nobody (i.e. the compiler) will complain.

but then once you are IN the paint method, you need to cast it back to a Graphics2D to regain access to that functionality.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

looks like a few people beat me to the punch! Hopefully that answers your question Vanchi.

You may find it useful to check out this Campfire story: How my Dog learned Polymorphism.
[ December 17, 2004: Message edited by: Jessica Sant ]
Vanchi Nathan
Ranch Hand

Joined: Feb 24, 2004
Posts: 107
Hello Jessica,

thanks for the link... i read and tried a few examples based on the classes used, it works fine.

As a whole it is like:

A Dog object can be referred as a Dog,
A Dog object can be reffered as an Animal,
An Animal object can be reffered as an Animal and lastly
An Animal object cannot be reffered as a Dog object.

Am I right?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on inheritance!
 
Similar Threads
MouseDragg
casting
when to use casting
JSlider
Regarding Graphics Context in Diff Browser in remote client