• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating an Abstract Superclass and Concrete Subclass  RSS feed

 
Adithi Palli
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have created a superclass as follows:

package food;
public abstract class fruit { }

I have created a subclass in a seperate file as follows :

import food.fruit;
class apple exends fruit{ }

I have created a directory called food in the directory of my class path setting.

When I am trying to compile these two files on the command prompt as below, they are getting compiled without any error

C:\jp>javac Food\fruit.java

C:\jp>javac Food\apple.java

But when Iam trying to compile them as below, Iam getting an error with the second file (apple.java) as

javac: invalid flag: .
Usage: javac <options> <source files>
use -help for a list of possible options

C:\jp\Food>javac fruit.java

C:\jp\Food>javac . apple.java

Request help on this.

Thanks,
Adithi
 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Adithi,

I think you should be placing apple.java file outside Food directory,
because apple.java is not in Food package.
 
camilo lopes
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you go to have that to have .class of superclass before compiling subclass..
 
Adithi Palli
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Asha and Camilo.

So, should I alwaays comile apple.java by coming out of Food Directory.
Is there any way in which I can compile this file by being within the Food Directory?

Thanks,
Adithi
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The directory structure in which you have your source files must match the package structure.

So if your class fruit is in package food, then it must be in a directory named "food".

Your class apple is in package food.fruit, so it must be in a directory "food\fruit" (a subdirectory "fruit" of the directory "food").
 
Adithi Palli
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper,

Thanks for the response.But Iam still confused.
I have kept the fruit.java file in "Food" Directory and I have kept the apple.java file in "Fruit" directory under "Food" Directory.
Now, Iam trying to compile these two files by being in the "Food" Directory on the command prompt. fruit.java is getting compiled but apple.java does not compile. I tried going into the "fruit" directory to compile apple.java, but again it fails. Also I tried compiling by being outside the "Food" and "Fruit" directory. Again, fruit.java compiles and apple.java does not compile.

My query is :

I can compile these two files by coming out of the "food directory " , but by having ony "food" directory and no "fruit" directory . I wanted to know whether Iam doing correct or not?

Thanks,
Adithi
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using packages and putting files in different directories you should use the compiler -classpath option when you compile.

assuming this is your setup
../Food/fruit.java
../Food/Fruit/apple.java

to compile apple.java you need to have the -classpath option specify where to find package/directory Food.

It has to point to the directory ABOVE Food. You can fully qualify it or if you are in directory Food you can do:

javac -classpath .. Fruit/apple.java

".." represents the directory above Food and will allow the compiler to find where the Food directory is when it encounters

import Food.fruit;

in the apple.java file.

If you were in the Fruit directory you could type

javac -classpath ../.. apple.java

But it is simpler to fully qualify the directories in your -classpath:

javac -classpath c:\somedir c:\somedir\Food\Fruit\apple.java

That should work from any directory on your computer.
[ August 13, 2007: Message edited by: Red Smith ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!