This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Constuctors and super Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constuctors and super" Watch "Constuctors and super" New topic
Author

Constuctors and super

Anant Agarwalla
Greenhorn

Joined: Feb 02, 2013
Posts: 5
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

Joined: Oct 13, 2005
Posts: 38519
    
  23
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

Joined: Feb 02, 2013
Posts: 5
Thanks for pointing that out. This one gave me quite a scare! I know i should indent. From now, i will.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constuctors and super