Object Related

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bsonline
    New Member
    • Oct 2007
    • 21

    Object Related

    I have a code snippet like this ... :-

    class A
    {
    public void getA()
    {
    System.out.prin tln("In A...");
    }
    }
    class B extends A
    {
    public A getB()
    {
    C c1 = new C();
    return (A) c1;
    }
    }

    class C extends B
    {
    public void getC()
    {
    System.out.prin tln("In C...");
    }
    }

    public class D
    {
    public static void main(String args[])
    {
    B b1 = new B();
    A a1 = b1.getB();
    a1.getA();
    }
    }

    and I get result from that code snippet like....
    "In A...".

    But Now I change this code like following....

    class A
    {
    public void getA()
    {
    System.out.prin tln("In A...");
    }
    }
    class B extends A
    {
    //here I change
    C c1 = new C();

    public A getB()
    {
    return (A) c1;
    }
    }

    class C extends B
    {
    public void getC()
    {
    System.out.prin tln("In C...");
    }
    }

    public class D
    {
    public static void main(String args[])
    {
    B b1 = new B();
    A a1 = b1.getB();
    a1.getA();
    }
    }

    and I get some runtime error...


    Why?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by bsonline
    and I get some runtime error...
    Please be more specific; I'm sure the JVM didn't just barf up "some runtime error".

    kind regards,

    Jos

    Comment

    Working...