permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes this and super Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "this and super" Watch "this and super" New topic
Author

this and super

mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
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

Joined: Nov 16, 2000
Posts: 101
Seems to be D
pankaj agarwal
Ranch Hand

Joined: Oct 13, 2000
Posts: 39
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

Joined: Dec 01, 2000
Posts: 47
I tested the output it is C
Vidya Selvaraj
Greenhorn

Joined: Nov 21, 2000
Posts: 29
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

Joined: Nov 21, 2000
Posts: 29
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: this and super