Results 1 to 2 of 2
  1. #1
    himanshu19's Avatar
    himanshu19 is offline Member
    Join Date
    Feb 2010
    Rep Power

    Default constructor calling sequence.

    I have a doubt in following code.
    class Super
    Super(int x)
    class Sub extends Super
    Sub(int k)
    System.out.println("Inside sub class");
    class Main
    public static void main(String []args)
    Sub obj = new Sub(5);

    We have been told that before sub class constructor super class constructor runs.And if that does happen how come sub class constructor is supplying parameter to super class constructor.Also if we skip the whole constructor of sub class then compiler flashes error.

    Please help.


  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power


    The super class constructor is called on the line super(k). If you don't have this line then it will call the default (no parameters) constructor of the parent. If the parent does not have a no parameters constructor then you'll get a compiler error.

    So, the constructor for Sub is called, which then calls the constructor for its parent (Super), before returning to the Sub constructor.

    There are some more steps to this, but that should do you for now.

Similar Threads

  1. Calling a constructor from another one.
    By Somelauw in forum New To Java
    Replies: 1
    Last Post: 11-28-2009, 04:42 PM
  2. calling sequence
    By rocky in forum Web Frameworks
    Replies: 0
    Last Post: 04-27-2009, 09:35 PM
  3. Constructor calling
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-22-2007, 07:53 PM
  4. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 10:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts