I have a school assignment where I have to write class files to correspond with my instructor's already written class, which includes the main() method.
He has the main class importing the child class of a ParentClass, each of which is in a different package. In the main method, he goes on to create an instance of the ParentClass, but when I try to compile I get errors: "ParentClass cannot be resolved to a type." This makes sense to me since the ParentClass is never imported. I just don't really know what I would have to do to make this instantiation legal, or whether it's even possible to do or not(since I can't import the ParentClass). I would only be able to fix it in my ParentClass or child class.

Here is part of the main method, which I am unable to alter:

package edu.louisville.cecs.hw2;

import hw2.pkg2.ChildClassInAnotherPackage;

public class Example {
    public static void main( String [] args ) {
        ParentClass parent = new ParentClass( "privateProperty", "publicProperty", "protectedProperty", "packageProperty" );

Any help you guys could give me on this would be greatly appreciated.

8 Years
Discussion Span
Last Post by ~s.o.s~

I really don't. Since my main class is only importing the child class, which obviously extends the parent class, and this doesn't give me access to the parent class I don't see how I can instantiate it otherwise. Is there a resource you could point me to that would tell me how to make parent classes accessible when only the child class is imported? I've gone through the Sun Java tutorials and I didn't really find them all that helpful.


the error that you are getting usually comes if a function has not been called properly or with the correct number of parameters or the proper type of parameters to what had been declared earlier.

check the constructor of the ParentClass in the ParentClass class. the calling function should match the declared function.


This is the constructor in my ParentClass:

public ParentClass(String selectPrivateProperty, String selectPublicProperty, String selectProtectedProperty, String selectPackageProperty){
		this.privateProperty = selectPrivateProperty;
		this.publicProperty = selectPublicProperty;
		this.protectedProperty = selectProtectedProperty;
		this.packageProperty = selectPackageProperty;

It appears to match the calling function. If I import the ParentClass into the class I listed in my OP, I do not get the error, and it compiles and runs just fine. That's the only way I've been able to get it to work, but alas my assignment specifies that I am unable to do that.


from what i can understand your assignment is to import the ChildClass and use that to create the ParentClass. however you need the ParentClass if you want to create an object of it, but since ChildClass is extending ParentClass ChildClass should import ParentClass. like that you will not be explicitly telling the compiler to import ParentClass but the ChildClass will be telling the compiler to import ParentClass.

i hope this makes sense.


> I just don't really know what I would have to do to make this instantiation legal

Ensure that the Main class and the Parent class are placed in the same package.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.