Results 1 to 4 of 4
  1. #1
    crazy is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Working of Upcasting

    this is the program of Upcasting can anyone tell me about how this line "Base4 c=new Child4();" in the following program works............

    class Base4
    {
    void show()
    {
    System.out.println("Base");
    }
    }
    class Child4 extends Base4
    {
    void show()
    {
    System.out.println("Child");
    }
    void disp()
    {
    System.out.println("Child Display");
    }
    public static void main(String... ss)
    {
    Base4 c=new Child4();
    c.show();
    }
    }
    Last edited by crazy; 07-07-2013 at 06:14 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: Working of Upcasting

    The code doesn't work because a Base4 object doesn't have a disp() method.

    kind regards,

    Jos

    ps. and next time please indent your code properly and stick [code] ... [/code] tags around it.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    crazy is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: Working of Upcasting

    yes i agree and now i have edited it then can you tell me how the line "Base4 c=new Child4();" works!!!!!!!!!!!!!!!!!!!1

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: Working of Upcasting

    Quote Originally Posted by crazy View Post
    yes i agree and now i have edited it then can you tell me how the line "Base4 c=new Child4();" works!!!!!!!!!!!!!!!!!!!1
    That line works because a Child4 object is a Base4 object.

    kind regards,

    Jos

    ps. A question ends with one single question mark; not with a bunch of exclamation mark and a one.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Collisions are working & not working
    By Jayayoh in forum Java 2D
    Replies: 1
    Last Post: 06-24-2011, 06:21 PM
  2. Replies: 2
    Last Post: 01-04-2011, 05:30 AM
  3. How to do Upcasting of an Object ?
    By Shyam Singh in forum New To Java
    Replies: 3
    Last Post: 08-17-2008, 07:02 PM
  4. Replies: 8
    Last Post: 05-28-2008, 08:00 AM
  5. Working With ANT
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 09:16 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
  •