Results 1 to 6 of 6
  1. #1
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default [SOLVED] Confused with output sequence

    I am a bit confused with the output given for this code. Can anyone help me?
    Java Code:
    class Alpha{
     public void foo(String.. agr)
     { Sysout("Alpha:foo");}
    public void bar(String a)
     { Sysout("Alpha:bar");}
    }
    public class Beta extends Alpha{
     public void foo(String.. agr)
     { Sysout("Beta:foo");}
    public void bar(String a)
     { Sysout("Beta:bar");}
    public static void main(String argv[]){
    Alpha a=new Beta();
    Beta b =  (Beta)a;
    a.foo(test);b.foo(test);
    a.bar(test);b.bar(test);
    }
    }
    I felt the ans should be Alpha:foo Beta:foo Alpha:bar Beta:bar
    but the ans given was
    Alpha:foo Beta:foo Beta:bar Beta:bar

    Am i going wrong somewhere?
    To finish sooner, take your own time....
    Nivedithaaaa

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Why are you confusing. It's fine. On line,

    Java Code:
    Alpha a=new Beta();
    you define a type Alpha to the object reference a, but assign to type Beta. Think about from there.

    I don't see any confusion there at all.

  3. #3
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

    Default

    Hi Niveditha,

    When running the code which you have posted i got the following output:

    Beta:foo
    Beta:foo
    Beta:bar
    Beta:bar



    Just check your program output..

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes Nidveditha. Seems to me you are conflict with reference and overridden there. Start from the simple step. As I said earlier, you always refer the Beta class, not Alpha class.

  5. #5
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Ok..
    That was my confusion. If it refers to Beta class then even the firsto/p shud have been Beta:foo as baskar mentioned but the ans given was Alpha:foo
    Beta:foo
    Beta:bar
    Beta:bar


    So i felt i was understanding in a wrong way,i cant expect internet to be correct always rite?
    Anyways thank u guys... :)
    To finish sooner, take your own time....
    Nivedithaaaa

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 0
    Last Post: 06-28-2008, 05:54 AM
  2. Using escape sequence with like clause (%)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 10:57 AM
  3. Using escape sequence with like clause (_)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 10:55 AM
  4. Escape sequence problem
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-21-2008, 11:15 PM
  5. Create entity field SEQUENCE oracle
    By Marcus in forum JDBC
    Replies: 1
    Last Post: 06-27-2007, 04:31 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
  •