This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help on Inheritance

 
uday ramakrishna
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to first compile Animal.java before compiling Cat.java

Regards,
Raja
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> javac -classpath . Cat.java
> java -classpath . Cat
 
uday ramakrishna
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic