• 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

Constructors

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why this following code compiles and runs even though we don't have default no argument constructor in the superclass??
class Super{
public Super(int x){
System.out.println("Super");
}}
public class Child extends Super{
public static void main(String args[]){
Super s1=new Child(9);
}
public Child(int y){
super(y);
System.out.println("Child");
}}
[2]And also if we don't have super(y) it is not compiling.And even if we provide no argument constructor the output is only Child which shows that it is not heirarchial??
Am I clear??Please help me and thanks in advance....
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, the code runs and works as expected. OK. When you don't have the line "super(y)" the JVM tries to execute "super()" except that it can't find that. I expect if you add a constructor to your "Super" class with no args, then you can do what ever you want with it. If you remove the "int x" from the "Super" constructor that you have, it will call that constructor by default.
So, if you don't specify any constructor, a default one with no args and no code is produced. if you do specify one, a default one will NOT be produced.
I am really hoping this answers you question
reply
    Bookmark Topic Watch Topic
  • New Topic