Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

this and super

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems to be D
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tested the output it is C
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic