• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constuctors and super

 
Anant Agarwalla
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic