• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Linking multiple classes

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have posted about PetDogDriver on your other thread.
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I said, you are nowhere using PetDog at all.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bryan Peach wrote: probably a big mistake..

I hope you aren't paying a lot for this course
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So does this code look better?

[/code]
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bryan Peach wrote:So does this code look better?



Yes.... does it work now?

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hahaha no... this line:

is giving me a bunch of ')' expected and illegal start of expression errors... i cannot figure out why.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On closer examination, I think Henry is correct. Not super (String name, etc); but super(name, etc);
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just wanted ot let you guys know i got it working... thank you so much for your help....
reply
    Bookmark Topic Watch Topic
  • New Topic