Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on inheritance!

 
Vanchi Nathan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vanchi Nathan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vanchi Nathan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12202
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic