|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

09-08-2008, 07:51 AM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 3
|
|
|
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, 12:12 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
|
|
|
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?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

09-08-2008, 12:46 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 469
|
|
|
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 12:50 PM.
|
|

09-08-2008, 01:23 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

09-08-2008, 01:36 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 469
|
|
Originally Posted by Eranga
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.
Really? Here is what he posted:
public class testInher {
public static void main(String [] args)
{ B e = new B();
e.displayFromB();
}
}
properly formatted (but not changed) as
public class testInher {
public static void main(String [] args)
{ B e = new B();
e.displayFromB();
}
}
Besides, that would result in a different error message.
|
|

09-08-2008, 03:07 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
|
|
|
Ah, I see it in different way. Sorry about that. I've not test in a peace of paper or something else.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

09-08-2008, 04:01 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
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, 07:21 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

09-09-2008, 04:18 AM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 3
|
|
|
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
Ovi
Last edited by makbar24 : 09-09-2008 at 04:26 AM.
Reason: add one more question
|
|

09-09-2008, 04:21 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 876
|
|
|
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, 04:30 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
why does java compiler did not find the class "B".
How does the compiler know which file to look in to find the B class? If the file is named A.java.
If you want to compile all the .java files in a folder use the *
|
|

09-09-2008, 04:35 AM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 3
|
|
|
hi Norm, thank you for ur reply. how/when/where to use "*".
thank you
Ovi
|
|

09-09-2008, 05:01 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
javac *.java
|
|

09-09-2008, 07:15 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 31
|
|
|
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, 08:28 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 469
|
|
|
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, 08:38 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Canada
Posts: 191
|
|
|
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 @ [ To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. ]
Language is froth on the surface of thought
|
|

09-10-2008, 10:23 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 469
|
|
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.
// Filename BogusA.java
package test;
class BogusA {
BogusA() {
System.out.println("A");
}
}
class BogusB {
BogusB() {
System.out.println("B");
}
}
// Filename BogusC.java
package test;
public class BogusC {
public static void main(String[] args) {
new BogusA();
new BogusB();
}
}
// 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, 03:24 PM
|
|
Member
|
|
Join Date: Sep 2008
Posts: 17
|
|
|
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
Regards
__________________
Sagar Birari
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|