• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Constuctors and super

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Tester
{
public static void main(String[] args)
{
Sub ob = new Sub();
}
}

class Super
{
Super(int x, int y){}
}

class Sub
{
Sub(){}
}


In the above piece of code, i have made Sub as a subclass of Super class. When i create on object of Sub, its constructor should make an implicit super() call to the Super class' constructor but Super will not generate a no-arg constructor with me having written one. But this code compiles in my liunx termial. I wanted to know the reason why this checks out because I was expecting an error.
 
Marshal
Posts: 80653
476
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anant Agarwalla wrote: . . . In the above piece of code, i have made Sub as a subclass of Super class. . . .

No, you haven’t. It doesn’t say anything about extends Super. Those two classes both extend java.lang.Object directly, so you are using Object’s no‑args constructor as the target of super().

And welcome to the Ranch
I would have added code tags to your post, which would have made it look so much better, but you hadn’t indented your code.
 
Anant Agarwalla
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for pointing that out. This one gave me quite a scare! I know i should indent. From now, i will.
 
reply
    Bookmark Topic Watch Topic
  • New Topic