Results 1 to 4 of 4
  1. #1
    vital parsley is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Question how does this code work...explain me the execution please...

    public class ques4thir
    {
    public static void main(String args[ ])
    {
    SuperClass object=new SuperClass( );
    object.display( );

    object=new SubClass( );
    object.display( );
    }
    }

    class SuperClass
    {
    private int privatevar=10;
    public int publicvar=20;
    protected int protectedvar=30;
    int defaultvar=40;

    public void display( )
    {
    System.out.println(privatevar + ":"+publicvar+" :"+protectedvar+" :"+defaultvar);
    }
    }

    class SubClass extends SuperClass
    {
    public SubClass( )
    {
    this(10);
    }

    public SubClass(int a)
    {
    super( );
    super.publicvar=super.publicvar+1;
    this.protectedvar=this.protectedvar+1;
    this.defaultvar=this.defaultvar+1;
    }
    }

    which method gets executed first...i m a bit confused!..tried using system.out.println to see wat gets executed first..but tat only increased trhe confusion!

  2. #2
    Alan-LB's Avatar
    Alan-LB is offline Member
    Join Date
    Jun 2008
    Location
    Junee, NSW, Australia
    Posts
    19
    Rep Power
    0

    Default

    Ask yourself which statement in the program gets executed first. Then which is the next. Write them down on a piece of paper and work slowly through the whole program.

    By the way - wat is spelled 'what' and tat is spelled 'that'!

    Alan
    There are 10 types of people - those who understand binary and those who don't!!
    Today is the Beta version of Tomorrow!!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    To see who calls the display() method, add an arg to it that indicates who the caller is and show that value in the println();
    For example:
    object.display("main with superclass");
    object.display("main with subclass");
    The output now will show you the execution flow.

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

Similar Threads

  1. Please explain Java
    By MarkWilson in forum New To Java
    Replies: 7
    Last Post: 07-02-2008, 08:38 AM
  2. the code dosent work
    By pcman in forum Java 2D
    Replies: 1
    Last Post: 03-20-2008, 08:20 PM
  3. Need Help Can anyone explain what this means
    By Clemenza1983 in forum New To Java
    Replies: 6
    Last Post: 02-16-2008, 03:13 AM
  4. Iam new in Java Please explain to me
    By vinaytvijayan in forum AWT / Swing
    Replies: 1
    Last Post: 12-30-2007, 11:35 AM
  5. need to explain this code
    By reached in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 10:01 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
  •