Sunday, July 12, 2009

Please help regarding inheritance with packages in Java?

I have 2 classes in a package. One class inherits from the other. Ihad no problems compiling the classes when the classes weren't in apackage, but as soon as I put the classes in a package and appropriate directories, I get the error :





Base.java





package p1;


public class Base {


public static void main(String[] args)


{


System.out.println("This is base class");


}


}





Sub.java





package p1;


public class Sub extends Base {


public static void main(String[] args)


{


System.out.println("This is sub class");


}


}





Now i m executing in this way from the console..





C:\programs\p1\javac Base.java





c:\programs\p1\cd..





c:\programs\java p1.Base


This is Base class


c:\programs\cd p1





c:\programs\p1\javac Sub.java (here when i compile this class, then the error comes like this..)





Sub.java:2 : cannot resolve symbol


symbol : class Base





location : class p1.Sub





public class Sub extends Base Class {





So, can any one tell me where it went wrong ? plzzzzzzzzz

Please help regarding inheritance with packages in Java?
It's not a problem at all - it's intended behaviour.





From the directory containing Base.java and Sub.java create a subdirectory called 'p1'.





javac -d p1 Base.java


Means 'make Base.class in the directory called 'p1').





javac -classpath p1 -d p1 Sub.java


Means 'make Sub.class in the directory called 'p1', and if you need any classfiles - e.g. Base.class, try looking in a directory called 'p1'.





Don't forget if you're making a jar of this to start from the base directory.
Reply:this is common problem while working with packages. i have also encountered this problem. they say the file in the same package has to placed in the same folder with package name but it doesnt compiles.


the solution i have found is to use IDE like netbeans or eclipse n compile thru that. i takes care of necessary parameters to be passed to the system. so it is good to use it.


No comments:

Post a Comment