Results 1 to 16 of 16
  1. #1
    amosexy is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default problem with output of code

    i write this code just for learn
    and i cant understand the output.

    the code:
    Java Code:
    public class A{
    public A()
    {
    f();
    h();
    }
    public void f() {
    
    System.out.println("f from A");
    g();
    }
    public void g() {
    System.out.println("g from A");
    }
    public static void h() {
    System.out.println("h from A");
    }
    
    }
    
    class B
    
    
    public class B extends A {
    public B()
    {
    f();
    }
    public void f() {
    System.out.println("f from B");
    }
    public void g() {
    System.out.println("g from B");
    }
    public static void h() {
    System.out.println("h from B");
    }
    
    }
    
    MAIN
    public class Program {
    
    /**
    * @param args
    */
    public static void main(String[] args) {
    A x=new B();
    System.out.println(x.a);
    }
    
    }
    the output is:
    f from B
    h from A
    f from B

    why the output is no
    f from A
    g from A
    h from A
    f from B


    tnx a lot
    Last edited by amosexy; 07-05-2010 at 03:06 PM. Reason: make post readable :-)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello and welcome to the forum. I think that to help us all, you will want to edit your post above to make some changes so that your question is more easily answered:

    1) Please repost formatted code and use code tags to help the forum software show the code's formatting. To do this, simply place the tag [code] above your block of code and [/code] below the block.

    2) You'll want to tell us what the output is, and why it's not what you would expect.

    Good luck and again welcome.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think the code is more readable if indented correctly. e.g.,
    Java Code:
    class A {
       public A() {
          f();
          h();
       }
    
       public void f() {
    
          System.out.println("f from A");
          g();
       }
    
       public void g() {
          System.out.println("g from A");
       }
    
       public static void h() {
          System.out.println("h from A");
       }
    
    }
    
    class B extends A {
       public B() {
          f();
       }
    
       public void f() {
          System.out.println("f from B");
       }
    
       public void g() {
          System.out.println("g from B");
       }
    
       public static void h() {
          System.out.println("h from B");
       }
    }
    
    class Program {
    
       public static void main(String[] args) {
          A x = new B();
          // System.out.println(x.a); //!! this line doesn't compile
       }
    
    }
    Note that the line indicated near the bottom doesn't compile and has been commented out.

    Your program first creates a new B object and so first calls B's constructor. Since B extends A, B's constructor will call the A constructor before calling its own code. Inside the A constructor are two method calls, f() and h(). Since f is not a static method, the one associated with the B object will be called. Since h is a static method, the one inside of the A class will be called.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think the code is more readable if indented correctly. e.g.,
    Java Code:
    class A {
       public A() {
          f();
          h();
       }
    
       public void f() {
    
          System.out.println("f from A");
          g();
       }
    
       public void g() {
          System.out.println("g from A");
       }
    
       public static void h() {
          System.out.println("h from A");
       }
    
    }
    
    class B extends A {
       public B() {
          f();
       }
    
       public void f() {
          System.out.println("f from B");
       }
    
       public void g() {
          System.out.println("g from B");
       }
    
       public static void h() {
          System.out.println("h from B");
       }
    }
    
    class Program {
    
       public static void main(String[] args) {
          A x = new B();
          // System.out.println(x.a); //!! this line doesn't compile
       }
    
    }
    Note that the line indicated near the bottom doesn't compile and has been commented out.

    Your program first creates a new B object and so first calls B's constructor. Since B extends A, B's constructor will call the A constructor before calling its own code. Inside the A constructor are two method calls, f() and h(). Since f is not a static method, the one associated with the B object will be called. Since h is a static method, the one inside of the A class will be called.

  5. #5
    amosexy is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    yes but why A cant finish to init himself?
    i cant understand
    if i init some vars of A if i have in the class
    in situation like this
    the vars cannot be init

    its a BUG

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by amosexy View Post
    yes but why A cant finish to init himself?
    i cant understand
    if i init some vars of A if i have in the class
    in situation like this
    the vars cannot be init
    You are creating a B object, not an A object

    its a BUG
    Do you seriously believe that this is a bug in java itself rather than an error in your understanding on how Java works? Do you know the chances chances that you're right and the creators of Java are wrong here? Really now, let's stick to reality.

  7. #7
    mangstadt is offline Member
    Join Date
    Jun 2010
    Location
    USA
    Posts
    19
    Rep Power
    0

    Default

    The method f() in class A is overridden by class B, so when f() is called in A's constructor, B.f() is called. Static methods can not be overridden, so when h() is called in A's constructor, it calls A.h().

  8. #8
    amosexy is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    i create B ok
    but B extends A
    and the A constractur must to finish init imself

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    and the A constructor does finish itself, but note that when it is being called on a B object, then
    Java Code:
       public A() {
          f();  // B's f method will be called since f is an instance method.
          h(); // A's h method will be called since h is a static method.
       }

  10. #10
    amosexy is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by mangstadt View Post
    The method f() in class A is overridden by class B, so when f() is called in A's constructor, B.f() is called. Static methods can not be overridden, so when h() is called in A's constructor, it calls A.h().
    yes i know all things about override

    but why B.f() calls
    and not A.f()

    A need to init imself because of this
    the super() call create aumomaticly

    and now
    A cannot init imself
    because f is override

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

    Default

    Quote Originally Posted by amosexy View Post
    its a BUG
    No it is not; it are the results when you call an overridable method from the constructor. Lots of people new to Java fall for that trap and you are no exception to the rule. Now that you know that you are warned: don't call a method from a constructor that can be overridden in a subclass; it is silly if done without giving it a very thorough second thought.

    kind regards,

    Jos

  12. #12
    amosexy is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    how can i know it
    if i extends some class i never know how the ctr implements

    so how should i know it

    i cant understand why is like that

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

    Default

    Quote Originally Posted by amosexy View Post
    how can i know it
    if i extends some class i never know how the ctr implements

    so how should i know it

    i cant understand why is like that
    If you want the constructor of your class to call your method f() and you don't want it to be overridden you should make your method f() a private method. It's called 'design', an ancient art most people have forgotten today.

    kind regards,

    Jos

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    To be honest, if you feel you have to know how the constructor of a parent class works then you've probably written it wrong.

    You shouldn't be able to override stuff used by the parents constructor, IMO. There might be the odd exception, but I'd look on it in a code review as a probable problem with the code.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Most of the time I've seen bad code like this, it's from a Java certification practice test or review book and is written solely to demonstrate a point.

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

    Default

    Quote Originally Posted by Fubarable View Post
    Most of the time I've seen bad code like this, it's from a Java certification practice test or review book and is written solely to demonstrate a point.
    True, and this OP even finds it a bug; if I remember correctly C++ does it the way this OP would've expected it to happen: i.e. if the object for the subclass isn't constructed yet no method (function) from the subclass can be called from the constructor of the superclass. I think I like it that way; we can't refer to member variables from the subclass in such circumstances either.

    kind regards,

    Jos

Similar Threads

  1. needed code for this output
    By rajivjoshi in forum New To Java
    Replies: 4
    Last Post: 05-31-2010, 05:54 PM
  2. xml output problem
    By tsili in forum XML
    Replies: 2
    Last Post: 05-24-2010, 12:27 AM
  3. What will be output by the following code? using trim method.
    By racewithferrari in forum New To Java
    Replies: 4
    Last Post: 11-17-2009, 09:40 PM
  4. Need help formatting output and some code
    By A5i19 in forum New To Java
    Replies: 6
    Last Post: 11-09-2009, 04:58 AM
  5. Output problem
    By jvasilj1 in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 06:39 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
  •