GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Linking multiple classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Linking multiple classes" Watch "Linking multiple classes" New topic
Author

Linking multiple classes

Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
Hello.. So i have this assignment that I have to create A class called Dog with two sub classes called PetDog and WorkingDog. I have created and tested the Dog class and now am working on the PetDog class. I have created a driver to test it but I get a bunch of errors including one that says: cannot find symbol constructor Dog().. and the others are cannot access any of my Pet Dog methods from a static context... Here is my code for the two classes and a driver... any ideas?

Main Class:


PetDog Class(sub-class)


Driver:
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Make a directory Pet, put all the files in. From the directory above Pet, type things like "javac Pet/Dog.java". When it's time to run your class, type things like "java Pet.MainProgram". Do not try to compile or run anything from within the Pet directory. Everything will then work out.


[Jess in Action][AskingGoodQuestions]
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
Everything is in the same directory. Should I have some kind of import statement to inport the Dog class?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Is everything in a directory named "Pet", after the package, and then when you type are you in the parent of that directory? That's how Java wants you to do it. If you're doing anything else, you're going to have problems.

Remember that you have no class named "Dog". You have a class named "Pet.Dog", and at some point the compiler is going to be looking for Pet/Dog.class .
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
I really have no idea what you are telling me to do. I have a file called Dog that JCreator is putting all the .java files and the .class files. The system will not allow me to move any of the files or rename the folder Pet. I'm completly lost
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Your PetDog class does not specify the super constructor to call -- so Java will, by default, call the no-arg constructor of the super class. And in this case, the Dog class does not have a no-arg constructor.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
The Files are all in a folder named Pet... when I go to compile i get a cannot find symbol constructor dog() error on this line...

if its not obvious I am pretty new to this and have absolutly no idea what is happening... can somebody please explain it in terms i might understand?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
I have posted about PetDogDriver on your other thread.
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
Ok so I moved my Dog constructor to the petDog class and removed the extends clause form the petDog driver. and i still don't get the right output. It prints out the Dog information from the superclass but nothing from the PetDog sub-class... here is the revised code.

Superclass:


SubClass:


Driver:


but now with the Dog constructor gone from my driver i get this error:
illegal start of expression

at mt PetDog constructor.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

You do know that this...



is the same thing as this...



Is this really what you want? Don't you want to initialize the Dog portion of PetDog too?

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

It prints out the Dog information from the superclass but nothing from the PetDog sub-class... here is the revised code.


You need to explain what you mean by this? Are no information being printed at all? Are the information wrong? etc.

Henry
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
As i have said before i am very new to this and really have no idea what i am doing.. so no i didn't know that so what do you suggest i do with this code?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Bryan Peach wrote:As i have said before i am very new to this and really have no idea what i am doing.. so no i didn't know that so what do you suggest i do with this code?


I am *not* suggesting anything. I am trying to figure out what you are trying to accomplish -- and somewhat what you are trying to ask about.

You need to help us understand what you are trying to do first.

Henry
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
I am trying to construct an object from the PetDog class(which is a subclass of Dog) So what I need to do is pass the information needed for a Petdog to the constructor and have it print out along with the data from Dog. Seeing as how a PetDog is aloso part of the Dog class. I would give you what i am getting for output but I have been following suggestions from here and now it will not compile for some reason... So i have to try to get it back to the point where I had it. I am completely lost right now.
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
When I run My PetDogDriver this is the output I am getting:

Breed is: Rotweiller
Weight is: 125 lbs.
Sex is: Male
Age is: 3


But nothing about the fields that the PetDog subclass is supposed to take care of.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
As I said, you are nowhere using PetDog at all.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Bryan Peach wrote:When I run My PetDogDriver this is the output I am getting:

Breed is: Rotweiller
Weight is: 125 lbs.
Sex is: Male
Age is: 3


But nothing about the fields that the PetDog subclass is supposed to take care of.


A method doesn't magically know how to do stuff just because you extended it. You actually have to implement what is necessary to extended it. Your output is cause by the printResults() method. If you want it to behave different in your PetDog class, you will need to override it.

Henry
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
Well how do i? My textbook doesn't say to do anything other than what i have done... I am looking everywhere online and all i can seem to find info. for is using the default constructor... nothing about the super constructor with parameters. and why am i not using the PetClass at all?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
How did you move the Dog constructor to PetDog? That will only make things worse?I told you to get rid of the no-arguments constructor, since you presumably don't want a Dog weight 0. Now you have two no-arguments constructors, so you can have a dog with no owner and address too.
You need your PetDog constructor to set up the Dog's weight as well as its address.

Haven't you been taught how create a constructor for a subclass with super(x, y, z);? There is an example in the Java™ Tutorials?
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
No sorry.. I haven't been taught that... I am doing this course through correspondance... probably a big mistake.. but i'll take a look at that tutorial
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Bryan Peach wrote: probably a big mistake..
I hope you aren't paying a lot for this course
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Bryan Peach wrote:No sorry.. I haven't been taught that... I am doing this course through correspondance... probably a big mistake.. but i'll take a look at that tutorial


Yea, I recommend taking a step back.

Creating a subclass isn't just using the extends keywork. You need to understand how the subclass is initialized, along with the superclass. You need to understand what you need to override. etc. etc. etc.

You seem to be expecting a lot to magically occur with just the extends keyword.

Henry
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
So does this code look better?

[/code]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Bryan Peach wrote:So does this code look better?


Yes.... does it work now?

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40


BTW, you can use the super keyword to help with overridden methods too. So, instead of this...



It may be better to do something like this...



Henry
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
hahaha no... this line:

is giving me a bunch of ')' expected and illegal start of expression errors... i cannot figure out why.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
A Dog has to have breed, age, sex and weight (not necessarily in that order). We know that because you said so in the Dog class. A PetDog IS-A Dog, so everything a Dog does, a PetDog does too. So a PetDog has age, sex, weight and breed. In fact a PetDog object has a part which is set up by the Dog class. [And the Dog class has a part which is set up by the Object class.] So your constructor has to set up the Dog part first before setting up the Pet part. So the information required for a Dog (sex, weight, breed, age) must be passed to the Dog part before the Pet part is set up. If you get your hands on a copy of Head First Java (2nd edition) by Kathy Sierra and Bert Bates, have a look at pages 214ff, 250ff, etc, where you will find diagrams to illustrate this, and an explanation of how the constructors are called. Or you can look in the Java™ Language Specification which is more definitive . . . and more difficult to understand.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Bryan Peach wrote:hahaha no... this line:

is giving me a bunch of ')' expected and illegal start of expression errors... i cannot figure out why.


I think that it is caused by the next line. When you call the super() constructor, there is no need to define the parameter types.

Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Bryan Peach wrote:hahaha no... this line:
. . .
is giving me a bunch of ')' expected and illegal start of expression errors... i cannot figure out why.
Illegal start of expression errors are usually caused by too many {s or too few }. Get a text editor which supports bracket matching (eg gedit, Kate [for Linux], NotePad++ Notepad2 or jEdit [for Windows]). When you put your mouse onto a ([{ or )]} the paired symbol changes colour. Use that to count your brackets and braces and things. You are only allowed to use the keyword public one indent level away from the left margin, if your indentation correctly reflects the {} structure.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
On closer examination, I think Henry is correct. Not super (String name, etc); but super(name, etc);
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
So even after all those changes I still get this output:


Breed is: Rotweiller
Weight is: 125 lbs.
Sex is: Male
Age is: 3


I'm going crazy here. Thanks for putting up with me guys...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

The obvious thing to check first -- is to make sure everything compiles correctly. And you are not running your previous version of the class files.

Henry
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
its not calling the right files, but I'm using JCreator and don't know how to get it to change where it looks, or what files its calling... does that make sense?
Bryan Peach
Ranch Hand

Joined: Sep 08, 2009
Posts: 76
just wanted ot let you guys know i got it working... thank you so much for your help....
 
GeeCON Prague 2014
 
subject: Linking multiple classes