Results 1 to 4 of 4
- 07-24-2008, 08:41 AM #1
Member
- Join Date
- Jul 2008
- Posts
- 2
- Rep Power
- 0
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!
- 07-24-2008, 09:27 AM #2
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'!
AlanThere are 10 types of people - those who understand binary and those who don't!!
Today is the Beta version of Tomorrow!!
- 07-24-2008, 03:46 PM #3
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.
- 07-25-2008, 04:50 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes do it as Alan and Norm says. If you stuck with something ask your question as much as clearly you can.
Similar Threads
-
Please explain Java
By MarkWilson in forum New To JavaReplies: 7Last Post: 07-02-2008, 08:38 AM -
the code dosent work
By pcman in forum Java 2DReplies: 1Last Post: 03-20-2008, 08:20 PM -
Need Help Can anyone explain what this means
By Clemenza1983 in forum New To JavaReplies: 6Last Post: 02-16-2008, 03:13 AM -
Iam new in Java Please explain to me
By vinaytvijayan in forum AWT / SwingReplies: 1Last Post: 12-30-2007, 11:35 AM -
need to explain this code
By reached in forum New To JavaReplies: 3Last Post: 12-03-2007, 10:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks