Results 1 to 18 of 18
- 09-08-2008, 05:51 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 3
- Rep Power
- 0
Super class and Subclass in same source file
hi forum,
I have a problem to compile the following code. i have declered a super class and its sub class in same file - called A.java. then i created a class file (wich conatin mail method) called - testInher.java. but when i try to compile by cmd, then it giving me following error:
file name : A.java
class A {
public void displayFromA()
{
System.out.println("I am a display message from class A- Parent");
}
}
class B extends A{
public void displayFromB()
{
System.out.println("I am a display message from class B- Child");
}
}
File name : testinher.java
public class testInher {
public static void main(String [] args)
{ B e = new B();
e.displayFromB();
}
}
cmd error msg:
"
C:\Test\Inher1>javac testInher.java
testInher.java:6: cannot find symbol
symbol : class B
location: class testInher
{ B e = new B();
^
testInher.java:6: cannot find symbol
symbol : class B
location: class testInher
{ B e = new B();
^
2 errors
"
here notice that: all are file in same directory and my classpath has been set up with".". i had tryed above code with IDE RAD and it compile and run over there.
i am wonder wht was i did wrong in that code??
Any help will be higly appriciated . looking forward ur reply . plz help me to learn java
thank you
Ovi
- 09-08-2008, 10:12 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Seems to me, this should working fine. What happened when you compile the class A. Did you get 2 class files as A and B?
- 09-08-2008, 10:46 AM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Give your classes packages, for one thing, and you may find that the problem goes away.
Edit: BTW, How did you "setup" your classpath? You'd be better off using the "-cp ." option with javac.Last edited by masijade; 09-08-2008 at 10:50 AM.
- 09-08-2008, 11:23 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ah, he forget the } sign at the end of testInher class. That's the reason I think. Because in NetBeans it notify that EOF reaches at the before last braces.
- 09-08-2008, 11:36 AM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Really? Here is what he posted:
properly formatted (but not changed) aspublic class testInher {
public static void main(String [] args)
{ B e = new B();
e.displayFromB();
}
}
Besides, that would result in a different error message.Java Code:public class testInher { public static void main(String [] args) { B e = new B(); e.displayFromB(); } }
- 09-08-2008, 01:07 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ah, I see it in different way. Sorry about that. I've not test in a peace of paper or something else.
- 09-08-2008, 02:01 PM #7
Eranga's point is did you compile A.java first to get the B.class? Since the file name of the file containing B is A.java, the compiler won't look in it for B.class.
- 09-08-2008, 05:21 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I hope that's the issue defiantly there, since that missing } is not valid here in the code. I was take that in wrong way actually.
- 09-09-2008, 02:18 AM #9
Member
- Join Date
- Sep 2008
- Posts
- 3
- Rep Power
- 0
Eranga and all,
Thank u for ur help. its work if i 1st compile A.java then testInher.java(which contain main method). Now, my question is: what is the rule for declaring superclass and subclass in a same file??? I was looking this information in some book but none of them did not mention the rule/regulation to declar superclass and subclass in same file. PLZ some one explain the proper rule to declar superclass and subclass declaration- i would really appriciate that. and in my above case, why does java compiler did not find the class "B". why should i compile them separtly? what need to be change/add to above code so i need to compile only testInher class(which contain main method). Please help me to understand the concept.
Thank a lot
OviLast edited by makbar24; 09-09-2008 at 02:26 AM. Reason: add one more question
-
There can only be one public class in a file and its name must match the file name.
Having said that, at this stage in the game, there is no reason to put more than one class in a file. In other words, put A in its file and B in a separate file, and both of them should have proper packages. HTH and good luck!
- 09-09-2008, 02:30 AM #11
How does the compiler know which file to look in to find the B class? If the file is named A.java.why does java compiler did not find the class "B".
If you want to compile all the .java files in a folder use the *
- 09-09-2008, 02:35 AM #12
Member
- Join Date
- Sep 2008
- Posts
- 3
- Rep Power
- 0
hi Norm, thank you for ur reply. how/when/where to use "*".
thank you
Ovi
- 09-09-2008, 03:01 AM #13
javac *.java
- 09-09-2008, 05:15 PM #14
Member
- Join Date
- Sep 2008
- Posts
- 31
- Rep Power
- 0
I think you have compiled javac A.java. With this you can't get class file for class B so you compile all three java classes. You can do this in one go as javac *.java
- 09-09-2008, 06:28 PM #15
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
It is perfectly valid to declare and define multiple classes in one file, and each class will "get it's own class file" once compiled. Hell, even anonymous inner classes get a separate class file once compiled.
- 09-10-2008, 06:38 AM #16
There is only one public class per file as Fubarable said above. The only reason for defining more classes in a file is to keep them from being used by code outside that file. Not your case! Define B in its own file called B.java
Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought
- 09-10-2008, 08:23 AM #17
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Yes you can use mulitple classes declared inside a file out side of that "file". You cannot use them outside of the declared package. They are, by default, package protected (same as any class that is not specifically assigned an access modifier). Case in point, try these three files.
Java Code:// Filename BogusA.java package test; class BogusA { BogusA() { System.out.println("A"); } } class BogusB { BogusB() { System.out.println("B"); } }Java Code:// Filename BogusC.java package test; public class BogusC { public static void main(String[] args) { new BogusA(); new BogusB(); } }Java Code:// Filename BogusD.java package test.subtest; import test.BogusA; import test.BogusB; public class BogusD { public static void main(String[] args) { new BogusA(); new BogusB(); } }
C works since it is in the same package as A and B (don't even need imports as is normal for classes in the same package), but D does not, since it is in another package (which also helps to illustrate that "subpackages" have no extra permissions to their "parents" as to any other package, and vice-versa).
Declaring multiple classes in the same file is not, necessarily, a "good programming practice", but it is perfectly valid. The only restriction, is that there can only be one public class (but not that there must be).
- 09-10-2008, 01:24 PM #18
Member
- Join Date
- Sep 2008
- Posts
- 17
- Rep Power
- 0
hello makbar24 & other members those replied to this post
Everything is fine in terms of code.
Only u need to compile ur java file in specific manner.
In testinher.java file object of class B created, hence u should compile a file which has B class, means A.java file
and then compile testinher.java
RegardsSagar Birari
Development & Technical Blog
Similar Threads
-
Organize class source code
By Alejandro Valdez in forum EclipseReplies: 0Last Post: 05-16-2008, 02:58 PM -
Class Reflection: Finding super class names
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:12 PM -
Binaries and Source File
By sumation in forum NetBeansReplies: 0Last Post: 04-03-2008, 07:14 PM -
subclass vs inner class
By bugger in forum New To JavaReplies: 1Last Post: 01-13-2008, 07:31 PM -
which class is superclass and subclass?
By java_fun2007 in forum New To JavaReplies: 0Last Post: 12-11-2007, 08:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks