Click to See Complete Forum and Search --> : Urgent Java Problem


Cyrus_Hunter
November 13th, 2003, 11:57 PM
I'm trying to create a child class called Insurance who's parent is called Shipment.

The first line of Insurance is:

public class Insurance extends Shipment



And everytime I try to compile the child, I keep getting the message:

Insurance.java:1: cannot resolve symbol
symbol : constructor Shipment ()
location: class Shipment
public class Insurance extends Shipment
^ (points at the 'c' in class)

Does anybody have any thoughts as to what can be done to fix this?

stephen_wee
November 14th, 2003, 12:14 AM
Hi,

you may need to import the lib where the Shipment class reside.

For example

Your Shipment Class may go something like this
package YourPackageName

public class Shipment
{
}


for your Insurance Class
import YourPackageName

public class Insurance extends Shipment

cjard
November 14th, 2003, 05:16 AM
consider, though, your use in inheritance here..

why must insurance inherit from shipment? it wont solve the java problem, but there is a logical disconnection between the 2 entities..

if you had a Super class called Shipment, that held, maybe the address of the parcel

and then inherited classes called:

InsuredShipment
NextDayShipment
InsuredNextDayShipment

i could understand, but at the moment, i see no reason for insurance to extend shipment, because you would then be saying Insurance.deliveryAddress, for example.. and that, is not logical

you could, perhaps, implement an interface:

public class Shipment implements Insurance

but better:

public class Shipment implements Insurable


once an object is insurable, it can be involved in a "new Claim(Insurable item)" - objects without insurance cannot be added to a claim..


-

note, like i said, changing your class to support these notions (if any) wont change the behaviour, as java doesnt know one from the other.. i was merely questioning your logic choice to make an Insurance object extend a Shipment..

a Car, UPS Van, Bus, Bicycle are 'extend'sions of a Vehicle
but a Parcel, Envelope, Box, Crate etc are not extensions of a UPS Van..

do you see?

dlorde
November 14th, 2003, 04:17 PM
Ignoring for a moment whether Insurance should extend Shipment (really doesn't make sense to me either cjard!), I don't think it's an import problem.

The problem is probably due to you declaring a non-default constructor in Shipment (i.e. a constructor with one or more arguments), and not calling it from the Insurance constructor (or maybe not even declaring a constructor for Insurance). Can't be sure without seeing the code.

When a subclass is instantiated, the superclass part must also be constructed. If the subclass doesn't explicitly call a superclass constructor, the compiler assumes a call to default superclass constructor and inserts it automatically. One way or another, a superclass constructor must be called. However, if the superclass doesn't have a default constructor, the compiler will complain that it can't find one. This is the error you are getting.

The compiler can't find the superclass default constructor because when you declare a non-default constructor, the compiler will no longer automatically provide a default one for you. If you want one, you have to declare it explicitly.

The solution is either to declare a default constructor for Shipment, or to call the Shipment constructor from the Insurance constructor. If Insurance doesn't have a constructor, it should have.

However, as cjard points out, if Insurance extends Shipment, either your design is seriously flawed, or you have seriously misleading names for your classes. Inheritance provides an is a relationship - the subclass is a kind of the base class. I don't see how you can ever say that an Insurance is a Shipment...

A Shipment may have an Insurance, in which case it should be modelled as a containment or an association - e.g. an Insurance field within Shipment, or alternatively, as cjard says, a Shipment may be Insurable (a Shipment is an Insurable thing).

The first step towards wisdom is calling things by their right names...
Chinese proverb

sammy8
November 15th, 2003, 01:10 AM
The solution is either to declare a default constructor for Shipment, or to call the Shipment constructor from the Insurance constructor.
In C++, this would not work - the constructor for the parent class is called automatically *before* the constructor for the child runs. In C++, you would have to use an initializer list, it is the only way around this problem that I know of.

As I understand it, Java does not have initializer lists, but it still calls the default base class constructor automatically *before* the derived class constructor runs.

But when you want to call a constructor with arguments in Java, you have to do what you describe - call the super class constructor from inside the child's constructor. I know that the call to the super class constructor must be the first thing in the derived class's constructor, but it is still a different syntax from C++.

So my question is: Does that super call still run BEFORE the instantiation of the child, or after? For example, does the heap get allocated for the memory of the child before the parent if you call the super class from inside the child's constructor?

dlorde
November 15th, 2003, 08:49 AM
Yes, C++ is a different language, but there are many similarities...

In Java, the superclass parts are also constructed before the subclass parts, but only the default superclass constructor is called automatically, and then only if it is either explicitly declared, or if there are no other superclass constructors.

When an explicit call to a superclass constructor is made from a subclass constructor, it must be the first statement in the subclass constructor, thus enforcing the correct construction order.

I expect the memory layout of subclasses/derived classes is much the same in Java and C++.

If you don't think carefully, you might believe that programming is just typing statements in a programming language...
W. Cunningham