• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance and packages  RSS feed

 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two questions,
1)
I have an assignment in which I am supposed to extend a class I created in a previous assignment. But when I create the class and have it extend the old class, I get two errors;
cannot resolve symbol: constructor Person ()in class Person, and
Object ()in Java.lang.object is not defined in a public class or interface, cannot be accessed from outside package
The code I use is

The two classes are defined in different files, and I'm using Borland JBuilder 9.
2)
In the first of the assignments, I created a bunch of methods which were defined in the main class. Now I need to use these methods again, and the second main class really shouldn't inherit the first main right? So I've been thinking of creating a package with these methods, is that a good thing to do?
Not sure this was very easy to understand, should I clarify something?
 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Make sure you have "." in your CLASSPATH environment variable as well - if you're not sure how this is done, do a search on "CLASSPATH" in Java in General (Beginner) - there have been many posts on the issue.

2)
In the first of the assignments, I created a bunch of methods which were defined in the main class. Now I need to use these methods again, and the second main class really shouldn't inherit the first main right? So I've been thinking of creating a package with these methods, is that a good thing to do?

The whole idea behind OO is reusable code that can provide behaviour to other classes through inheritance ( I hope my kids don't act like me!! ). So, I am a little confused about "main class" do you mean the class with the main( String[] ) in it? Every application must have one class that contains a main() method, else there would be no way to begin the application as the JVM would not know where to start.
However, if you have a body of methods that would be used and reused over and over again, you might would consider putting those in abstract, concrete or template classes in a package somewhere to be extended, or instanced or implemented in many other classes. These would not need to have a main( String[] ) method.
There are some really good articles at java.sun.com/docs/books/tutorial
Packages Search Results
Inheritance Search Results
HTH

(I give up on editing these urls. The UBB scrambles them everytime I re-enter them. What's up with that?)
[ October 26, 2003: Message edited by: Donald R. Cossitt ]
 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O, sure now they work Not taking a chance on editing out my complaint in fear they will be scrambled again. Miles Davis would have loved that process.
[ October 26, 2003: Message edited by: Donald R. Cossitt ]
 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My errors came from the fact that I had forgotten that the constructor of Person took arguments... So now I've solved that. With the methods I would reuse, I created a class PersonUtils which I extend with new methods as I need them.
But now I have a new problem When I create a JavaStudent, it is saved in the ArrayList that contains them, but as a Person. When I then try to edit the Array that is added in the JavaStudent class, I get a ClassCastException. How do I get around this problem?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!