File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Question regarding Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question regarding Inheritance" Watch "Question regarding Inheritance" New topic
Author

Question regarding Inheritance

Johnny White
Greenhorn

Joined: Sep 12, 2004
Posts: 3
Well first of all, I'd like to comment on what a great board and site this seems to be. Everyone seems very helpful and the information is well organized. Not just asskissing, I mean it.

Now onto my question.

I am trying to extend a class called Polygon:



With a class Triangle:



But I get an error: java:5: Polygon(int,int,int,int) in Polygon cannot be applied to ()

Which makes no sense to me since I thought the constructors were not passed on during inheritance.

I also noticed that the error disappears if I remove the Polygon constructor.

Can anyone explain why this is happening? I am playing around with it, but I don't understand the reason.

Thanks in advance.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Hi John,

Welcome to JavaRanch!

Every constructor, as the very first statement in its body, calls either another constructor from the same class, or a constructor from the superclass (the sole exception to this rule is the constructor java.lang.Object.Object(), which has no superclass constructor to call.)

If you don't explicitly call another constructor using either "this(XXX)" (for one in the same class) or "super(XXX)" (for one in the superclass), then the Java compiler inserts "super()" -- a call to the superclass constructor with no arguments. Note that this means if the superclass has no such constructor, there will be a compile error: the exact one that you're seeing, because Polygon has no default constructor.

One solution: Triangle(int, int, int) can call Polygon(int, int, int, int); you could do it like this:



Another solution, as you note, is to remove polygon's constructor altogether.


[Jess in Action][AskingGoodQuestions]
Johnny White
Greenhorn

Joined: Sep 12, 2004
Posts: 3
Thanks for the reply Ernest.

What is the actual point of having to construct an object of the superclass? I have all the methods I want in the subclass since they were inherited, and the variables as well..why instantiate another object in the superclass?

Would it be wise to just make a default empty constructor in the superclass everytime, and let the implicit super() construction that entails me not explicitly using a constructor for the superclass take over?

for example:

And then when I use extends


Or is the only point of this to be able to reuse the constructor and save you from typing out the initializations again, since that's basically what I just did.

Sorry for such a specific and trivial question, but I not only want to know how it works but why it works. Could you also please expound a bit more on your previous response, because I'm still a bit confused.

Every constructor, as the very first statement in its body, calls either another constructor from the same class, or a constructor from the superclass (the sole exception to this rule is the constructor java.lang.Object.Object(), which has no superclass constructor to call.)


Do you mean that every time I instantiate an object, the constructor in that object, calls the constructor in the class (which is the same constructor) ?

When you mention superclass, does this mean that even if I make one class, it still calls a constructor from the java.lang.Object class?

Thanks for the help man, I just want to be crystal clear on this.
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
John. White:

What is the actual point of having to construct an object of the superclass? I have all the methods I want in the subclass since they were inherited, and the variables as well..why instantiate another object in the superclass?

The idea is to share implementation as much as possible. For example, if Polygon had a perimeter() method that worked, then you wouldn't have to write a new perimeter() method for Triangle. That might not seem like such a savings, but you might appreciate it more once you had Quadrilateral, Square, Pentagon, Hexagon, and Heptagon all inheriting from Polygon - writing a different perimeter() method for each would be a bit of a pain, and if there was a bug in it, fixing the same bug 7 separate times would be a significant pain. Better to keep shared methods like that in the superclass.

But that means that the subclass often has to pass some initialization data to the superclass, in order for the superclass functions to work properly. For example, Triangle would need to pass its side lengths to the Polygon constructor - again sharing implementation by keeping the data in the superclass as much as possible. In fact, in any proper inheritance hierarchy, you really want to initialize the superclass, so the language enforces that by requiring a call to some superclass constructor - just to keep people from forgetting by accident.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Well first of all, I'd like to comment on what a great board and site this seems to be. Everyone seems very helpful and the information is well organized. Not just asskissing, I mean it.

Thanks John!

Now, about that display name. Could you please change your display name to remove that period.

Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

i usually rationalize it as follows...

your triangle IS a polygon. the JVM isn't making 2 object, one a polygon and one a triangle. it's making one object, your triangle.

BUT, your triangle can in ALL WAYS be considered a polygon. so, i better set everything up in case it's used that way... hence, the call to the supercontructor.

For this example, it may seem a little silly. but imagine if your polygon constructor set a color, a size, a place on the screen, a filled property, music to play in the background, and 18 other things.

the ONLY thing different for your triangle is one side has a length of 0. do you really want to write all that code all over again to do those 20+ things? and then if you make 3 subclasses of triangle, call them Isosoles, Equilateral and Scalene (which would require 2,1 and 3 sizes), wouldn't it be nice to reuse code in the Triangle constructor?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
When you create an instance of any subclass, a subobject of the superclass is contained inside the subclass. This subobject is the same as creating a standalone instance of the superclass, except that from the outside, the subobject is wrapped within the subclass instance.
Of course the only way One can ensure that the superclass is correctly initialised is to call the constructor in the superclass. The call to the superclass constructor is automatically inserted by Java if not explicitly written by the programmer.
Construction is always performed from the superclass 'downwards'.
Johnny White
Greenhorn

Joined: Sep 12, 2004
Posts: 3
Thanks for the response Warren, and I took care of that Dirk .

I'm still a bit confused though. My book says construction is a two step process...does this mean that a superclass is always initialized as well as a subclass? If I make a regular class, without the use of extends, is there still a constructor used for a superclass from the .object class that is the class from which everything was inherited?

Which I think this is what Ernest was trying to tell me.

I have run some test programs, and indeed I noticed that the superclass is always instantiated as well as the subclass that I am constructing. However, how would I ever get in contact with this superclass?

My theory was that the superclass had to be constructed in order for the subclass to use it's variables and methods properly, but I instantiated a superclass with all of it's values at 0, and checked the subclass who I had given real numbers to, and they were different.

So to summarize my questions, is there an actual way to ever get in touch with the superclass? And is construction always a two step process in the sense that a superclass and a subclass are always constructed, even when not using extends?

Thanks guys. It's nice to have someone to ask the idiosyncrasies of the language to, because my teacher just looked at me like I had three heads and told me to not worry about it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question regarding Inheritance