This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this and super

 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class SuperClass
{
public SuperClass(){
System.out.println(getClass().getName());
amethod();
}
public void amethod(){
// System.out.println("super class a method");
System.out.println(getClass().getName());
}
public static void main(String[] args)
{ SubClass sub = new SubClass();
}
}
class SubClass extends SuperClass
{
public SubClass(){
System.out.println(getClass().getName());
}
public void amethod(){
super.amethod();
System.out.println(super.getClass().getName());
}
}

What would be the outcome of the above code?
To add some spice:
A) SubClass SuperClass SubClass SuperClass
B) SubClass SubClass SuperClass SuperClass
C) SubClass SubClass SubClass SubClass
D) SubClass SubClass SubClass

[This message has been edited by mohit joshi (edited December 02, 2000).]
 
Hemal Mehta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to be D
 
pankaj agarwal
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose none of the options listed is correct. :-)
When you create an object of a subclass, the base class constructor gets called first. So, the first output has to be Superclass. About the rest.....
 
saumil shukla
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tested the output it is C
 
Vidya Selvaraj
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I modified the code a little bit for clear understanding:
class SuperClass {
public SuperClass(){
System.out.println("Super Constructor");
System.out.println(getClass().getName());
amethod();
}
public void amethod() {
System.out.println("super class a method");
System.out.println(getClass().getName());
}
public static void main(String[] args) {
SubClass sub = new SubClass();
}
}
class SubClass extends SuperClass {
public SubClass() {
System.out.println("Subclass Constructor");
System.out.println(getClass().getName());
}
public void amethod() {
super.amethod();
System.out.println("Subclass amethod");
System.out.println(super.getClass().getName());
}
}
/* Ouptut is:
C:\jdk1.3\bin>java SuperClass
Super Constructor
SubClass
super class a method
SubClass
Subclass amethod
SubClass
Subclass Constructor
SubClass
*/
Thanx
Vidya
Correct answer is "C"
 
Vidya Selvaraj
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With SuperClass sup = new SuperClass();, O/P is
C:\jdk1.3\bin>java SuperClass
Super Constructor
SuperClass
super class a method
SuperClass
With SuperClass sup = new SubClass();, O/P is
C:\jdk1.3\bin>java SuperClass
Super Constructor
SubClass
super class a method
SubClass
Subclass amethod
SubClass
Subclass Constructor
SubClass
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic