Results 1 to 7 of 7

Thread: confused code

  1. #1
    Join Date
    Jun 2007
    Posts
    11
    Rep Power
    0

    Default confused code

    Can anyone explain abt the output of this code..
    class A
    {
    int x=1;
    public void show()
    {
    System.out.print("hello");
    }
    }

    public class B extends A
    {
    int x=2;
    public void show()
    {
    System.out.println("hi");
    }

    public static void main(String args[])
    {
    A a;
    B b=new B();
    a=b;
    a.show();
    System.out.println(a.x);
    }
    }

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    First of all it gives the following output:

    hi
    1
    I think this can be explained like:

    When you declare a with "A a;", you define a object pointer of type A. So whatever this object pointer points is a type of A and considered to be an A. Actually if you try to assign some other object which does not inherent from A, compiler will return an error.

    So in the following line, when you assign b to a with "a=b;", b object is considered to be an instance of A and when you access x variable, the compiler gets its value from A object.

    What do you think?

  3. #3
    Join Date
    Jun 2007
    Posts
    11
    Rep Power
    0

    Default

    Then y doesn't it print "hello"

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    Then y doesn't it print "hello"
    Hmm, i did not notice that. Interesting..

  5. #5
    Join Date
    Jun 2007
    Posts
    11
    Rep Power
    0

    Default

    Its the rule of INHERITANCE

  6. #6
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    Its the rule of INHERITANCE
    So is this valid for C++ too? Explain it in detail.. ;)

  7. #7
    Join Date
    Jun 2007
    Posts
    11
    Rep Power
    0

    Default

    Unless it is declared as virtual, the binding is done at compile time in C++. This is not the case in Java.

    However, static members are bind at compile time in Java.

Similar Threads

  1. [SOLVED] Confused by the textField
    By jamesfrize in forum New To Java
    Replies: 4
    Last Post: 03-28-2008, 05:30 PM
  2. a lot confused
    By vineethraj in forum New To Java
    Replies: 4
    Last Post: 01-18-2008, 12:36 AM
  3. what does it mean:confused:
    By sivasayanth in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 04:52 AM
  4. Java confused over Generics?
    By Bibendum in forum New To Java
    Replies: 3
    Last Post: 12-26-2007, 06:23 AM
  5. bit confused on this question
    By money123 in forum New To Java
    Replies: 2
    Last Post: 07-27-2007, 12:14 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
  •