aspose file tools*
The moose likes Beginning Java and the fly likes Help on Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help on Inheritance" Watch "Help on Inheritance" New topic
Author

Help on Inheritance

uday ramakrishna
Greenhorn

Joined: Feb 24, 2003
Posts: 3
Dear All,

I have been learning Java for a while now and I have run a few successful snippets on inheritance using extends keyword. But suddenly now I get the following error on running the subclass. This is a sample picked up from the java site directly. Could some one tell me what I am doing wronog? I am using textpad and j2sdk1.4.2_04

D:\RND\java\Cat.java:1: cannot resolve symbol
symbol : class Animal
location: class Cat
public class Cat extends Animal {
^
D:\RND\java\Cat.java:11: cannot resolve symbol
symbol : class Animal
location: class Cat
Animal myAnimal = myCat;
^
D:\RND\java\Cat.java:13: cannot resolve symbol
symbol : variable Animal
location: class Cat
Animal.hide(); //Better!
^
3 errors

Tool completed with exit code 1


//------------Base class
public class Animal {
public static void hide() {
System.out.println("The hide method in Animal.%n");
}
public void override() {
System.out.println("The override method in Animal.%n");
}
}

//------------Sub Class
public class Cat extends Animal {
public static void hide() {
System.out.println("The hide method in Cat.%n");
}
public void override() {
System.out.println("The override method in Cat.%n");
}

public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
myAnimal.hide(); //BAD STYLE
Animal.hide(); //Better!
myAnimal.override();
}
}

Please tell me what is wrong and why I am getting an error.

Regards
Uday
Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105
You have to first compile Animal.java before compiling Cat.java

Regards,
Raja


SCJP 1.4
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Are both source files (Cat.java and Animal.java) in the same directory? Are you using packages?

With both source files in the same directory without packages, this compiles fine for me (regardless of the order).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
> javac -classpath . Cat.java
> java -classpath . Cat


There is no emoticon for what I am feeling!
uday ramakrishna
Greenhorn

Joined: Feb 24, 2003
Posts: 3
Thanks very much for all your responses. I am not using packages, but I am wondering if there is some porblem in the way JDK got installed and hence the problem. I ran several other examples and the same problems. I am not having an error when I compile files which inherit some custom classes from packages like AWT, SWING, etc.

Best Regards
Uday
uday ramakrishna
Greenhorn

Joined: Feb 24, 2003
Posts: 3
I am really sorry friends but I just tried what Jeff Albertson had posted and it compiled from the command prompt. I am not sure what is wrong when I compile it without the class path i.e when I directly compile it as javac Cat.java it does not compile. Could someone please tell me what settings I need to refine.

Thanks a ton for the soloution Jeff Albertson.

Best Regards
Uday
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
When you write you are not "using packages", you in fact are using the current directory as an unnamed package. My advice is to always use named packages. Once you have step things up correctly, there is no down-side to this. But in any case, commands javac and java work work with class paths. You have a couple of options:

1. You can change your CLASSPATH environment variable to include "." for example, if you want to write classes not explicitly in any package.

2. You can continue to use command line flags like -cp or -classpath

3. You can automate your build process by using Ant.

4. Use an IDE like Eclipse or NetBeans.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help on Inheritance