Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphism? Inheritance getContentPane

 
Asif Mansoori
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below are A, B, C and TestABC classes. B extends A, C extends B and TestABC is used to test A, B and C.



Above TestABC class is compiled but gives a following error on runtime.

A is created
B is created
C is created
This is Class A's Method
This is Class B's Method
This is Class C's Method
A is created
Exception in thread "main" java.lang.ClassCastException: A cannot be cast to C
at TestABC.main(TestABC.java:9)

============================

My question is if above scenario doesn't work then why following works. Assume frame is declared



Technically, it should generate same cast error message at runtime, since JFrame's getcontentPane method returns reference to Cotainer object. How we can upcast the Container to JPanel and use JPanel's method ? I had previously asked same question at this thread

http://www.coderanch.com/t/416126/GUI/java/Mystery-getContentPane-JFrame

I thought may be i was not clear about inheritance when I had asked same question before. Therefore, I read about inheritance and polymorphism. I even bought the book Head First Java 2nd Edition. I am still confused about the logic that works here.
Please help. Thanks
Asif
 
Peter Johnson
author
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I works because even though the signature of getContentPane says that it returns an object of type Container, the actual object returned is a JPanel; which is a subclass of Container.

On the other hand, you created an object of type A, which of course cannot be cast to C. Consider, on the other hand, this code:

A a = new C();
C c = (C)a;

This works without problems and is the same thing going on with the getContentPane example.
[ December 21, 2008: Message edited by: Peter Johnson ]
 
Asif Mansoori
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the answer. If it returns JPanel I have some understanding. How do we precisely know the actual type of JFrame's method getContentPane() or any other? Thanks
Asif
 
Asif Mansoori
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see in source code of JFrame, getContentPane() method uses JRootPane's method getContentPane() which in fact returns reference to Container object that is variable name contentPane type Container and defined as protected Container contentPane in JRootPane class

I am still bit confused Please help

Thanks
Asif
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not related to JBoss, so moving to Swing forum.
 
Peter Johnson
author
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way to know the exact type of a returned object is to ask the returned object. One way to do this is via the Object.getClass() method.

Also, just because contentPane is of type Container does not mean it is restricted to only Container - look back at my example, variable 'a' is of type A, but actually held an object of type C.

This goes back to basic object-oriented principles: a JFrame is a Container, thus a Container variable can hold a JFrame.
 
Asif Mansoori
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Peter for your help. I am totally cleared on this topic.
After executing following statements.



Which gives the following output.

class javax.swing.JPanel

Sometimes seeing is believing .

Thank you once again
Asif
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!