wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Inheritance Trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance Trouble" Watch "Inheritance Trouble" New topic
Author

Inheritance Trouble

Brad Edwards
Greenhorn

Joined: Mar 19, 2012
Posts: 18
I can't seem to get my constructors on the right page....



Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: lab6.Name1.<init>
at lab6.Infant.<init>(Infant.java:10)
at lab6.Lab6.main(Lab6.java:17)
Java Result: 1







Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Hi,

Unless explicit invocation, a constructor always calls a default constructor of super class.

Super class of Infant (i.e. Name1) does not have a default constructor, and hence compilation would fail.

Are you sure this is the exact code you got exception with?


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

As the previous poster mentions, you need to specifically call a constructor of the super-class, or have the super-class have a default, no-argument constructor.

Your real problem is stemming from the inheritance tree. When you say class Infant extends Name1, what you are saying is that Infant IS-A Name, that is, it is everything a Name1 is plus some specific differences. Then your usage indicates that Infant HAS-A Name1, and I think this is closer to the truth.

Similarly, you have class Name1 extends Item1, which says that every Name1 IS-A Item1, by by extension because in your code an Infant IS-A Name1, then Infant IS-A Item1. Are your Infants for sale? Name1 should not extend Item1, and Infant should not extend Name1. Rather, Infant should have instance variable which hold the Name1 and Item1 which you want to associate with the Infant.



Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Anayonkar Shivalkar wrote: . . . a constructor always calls a default constructor of super class. . . .
No, it doesn’t. It calls a no-arguments constructor. A default constructor is a no-arguments constructor, but a no-arguments constructor is not necessarily a default constructor.

You should follow the guideline given in this link about javadoc, and always write a constructor in every class.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Oops. I meant to say a no-arg constructor.
Brad Edwards
Greenhorn

Joined: Mar 19, 2012
Posts: 18
Thanks, for the info. I put in the no-arg constructors that I needed and it works great.

Similarly, you have class Name1 extends Item1, which says that every Name1 IS-A Item1, by by extension because in your code an Infant IS-A Name1, then Infant IS-A Item1. Are your Infants for sale? Name1 should not extend Item1, and Infant should not extend Name1. Rather, Infant should have instance variable which hold the Name1 and Item1 which you want to associate with the Infant.


The reason why I couldn't do this though was because the instructor wanted us to make a class an inheritance of two other classes and be able to call the parent classes from the subclass.

Thanks for the help once again, though.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

You are welcome.

However, I'm still wondering, how did you get runtime exception? I got compile time error itself.

@Campbell Ritchie, thanks for the 'no-arg constructor' correction.
Brad Edwards
Greenhorn

Joined: Mar 19, 2012
Posts: 18
In the Item1 class


In Name1 class



In Infant Class



I also Changed the driver class because I didn't like the order the program received the input...



Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Anayonkar Shivalkar wrote:You are welcome.

However, I'm still wondering, how did you get runtime exception? I got compile time error itself.

@Campbell Ritchie, thanks for the 'no-arg constructor' correction.


It is run-time cause he probably compiled Infant when Name1 had a no-arg constructor. He then modified Name1 and recompiled it. Then when he runs Infant is broken.

Brad Edwards wrote:The reason why I couldn't do this though was because the instructor wanted us to make a class an inheritance of two other classes and be able to call the parent classes from the subclass.

Ack. I hope he didn't give these classes as an example... If I were your professor (and I am not, nor do I know him) I would probably be looking for an inheritance tree which makes sense and might consider docking points for ones that don't. It is precisely this sort of inheritance tree which cause problems and grow into headaches later on. Best to correct the behavior now before it becomes a habit.
Brad Edwards
Greenhorn

Joined: Mar 19, 2012
Posts: 18
The classes were actually another lab that we had to make instances of each into a driver program and run it from there, so in other words no inheritance. This lab he wanted us to use the same classes just instead of instances, we were to make one the subclass of two and run the subclass in a main driver program. Yes, I agree it is an ugly way to do it, nor does it make much sense.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Brad Edwards wrote: . . . I put in the no-arg constructors that I needed and it works great. . . .
No, you are simply hiding other potential errors from yourself. A constructor should initialise the class by establishing its invariant. By writing a no-arguments constructor you are saying you permit different Items, all with the same description price and number, possibly . What you ought to do is delete that no-args constructor and always call the existing constructor from the sub-class constructor. Assuming the Item constructor is the same as in your first post, your Name1 constructor should look like this: . . . which shows how to call a superclass’ constructor to establish its invariant correctly.

Unfortunately, it also shows how inappropriate your inheritance structure is, as somebody has already told you. The awkward positioning of the () in the constructor signature suggests it might have too many parameters for its own good, I am afraid. Another style thing: you should use spaces, not tabs for indenting.
Another design thing: you are risking inaccuracies by using floating-point arithmetic for money; it is even worse if you use floats than doubles. This post shows what you ought to do, but you will have to make some changes to get the right number of decimal places.
Brad Edwards
Greenhorn

Joined: Mar 19, 2012
Posts: 18
ok, I understand... Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Brad Edwards wrote:ok, I understand... Thanks
Well done . . . and “you’re welcome”.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance Trouble