• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

subclass-superclass Doubt

 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Dan Chisholm's

public class Test20 extends Base2{

public static void main(String args[])throws Exception{
Test20 t= new Test20();
}
public Test20()throws Exception {
System.out.println("subclass default constructor");
}
}
class Base2{
Base2() throws Exception{
System.out.println("superclass default constructor");
}
}
I am getting output as
superclass default constructor
subclass default constructor
I am clear with this.But for the following code


public class Test20 extends Base2{
Test20 t= new Test20();
public static void main(String args[]){//throws Exception{
}
public Test20()throws Exception {
System.out.println("subclass default constructor");
}
}
class Base2{
Base2() throws Exception{
System.out.println("superclass default constructor");
}
}
I am not getting any output
Some one please explain the difference.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the second case, there's nothing in the main method body. You're not getting any output because no objects are created (no constructors are called).
 
bronco
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now the fun question is: what would happen if you added:

Test20 t2 = new Test20();

...to the main() method in your 2nd example?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...or added the "static" modifier to your class member...

static Test20 t = new Test20();

reply
    Bookmark Topic Watch Topic
  • New Topic