Results 1 to 2 of 2
  1. #1
    DrMath is offline Member
    Join Date
    Sep 2009
    Rep Power

    Default Question on Object Orientation Topic

    public class Redwood extends Tree {
    4. public static void main(String[] args) {
    5. new Redwood().go();
    6. }
    7. void go() {
    8. go2(new Tree(), new Redwood());
    9. go2((Redwood) new Tree(), new Redwood());
    10. }
    11. void go2(Tree t1, Redwood r1) {
    12. Redwood r2 = (Redwood)t1;
    13. Tree t2 = (Tree)r1;
    14. }
    15. }
    16. class Tree { }

    Question: Why would this ended up with only exception being thrown?
    go2(new Tree(), new Redwood()); -- allowed in Java?
    (sorry i'm quite weak on this)
    Thanks in advanced.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Rep Power


    The go method takes a Tree and a Redwood.

    Java Code:
    go2((Redwood) new Tree(), new Redwood());
    call is allowed by the compiler because a Redwood is a Tree. That's all the compiler can do for you.
    At runtime, the JVM is being asked to change a Tree that is not a Redwood to a Redwood so it doesn't like that.

Similar Threads

  1. Multiple columns right to left orientation
    By marjanzfz in forum New To Java
    Replies: 2
    Last Post: 11-12-2009, 04:33 AM
  2. change viewing platform orientation
    By Bit2_Gosu in forum New To Java
    Replies: 1
    Last Post: 02-15-2009, 09:46 AM
  3. How to publish jms topic on server
    By pankaj_salwan in forum Advanced Java
    Replies: 0
    Last Post: 10-06-2008, 10:59 AM
  4. Interesting topic in JComboBox
    By success21061985 in forum NetBeans
    Replies: 0
    Last Post: 08-21-2008, 01:43 PM
  5. object lock question
    By simon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts