Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-08-2008, 07:51 AM
Member
 
Join Date: Sep 2008
Posts: 3
makbar24 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-08-2008, 12:12 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-08-2008, 12:46 PM
Senior Member
 
Join Date: Jun 2008
Posts: 469
masijade is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-08-2008, 01:23 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to 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.
__________________
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-08-2008, 01:36 PM
Senior Member
 
Join Date: Jun 2008
Posts: 469
masijade is on a distinguished road
Quote:
Originally Posted by Eranga View Post
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:
Quote:
public class testInher {

public static void main(String [] args)
{ B e = new B();
e.displayFromB();
}
}
properly formatted (but not changed) as
Code:
public class testInher { public static void main(String [] args) { B e = new B(); e.displayFromB(); } }
Besides, that would result in a different error message.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 09-08-2008, 03:07 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 09-08-2008, 04:01 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 09-08-2008, 07:21 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,545
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 09-09-2008, 04:18 AM
Member
 
Join Date: Sep 2008
Posts: 3
makbar24 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 09-09-2008, 04:21 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 876
Fubarable is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 09-09-2008, 04:30 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
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 *
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 09-09-2008, 04:35 AM
Member
 
Join Date: Sep 2008
Posts: 3
makbar24 is on a distinguished road
hi Norm, thank you for ur reply. how/when/where to use "*".

thank you

Ovi
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 09-09-2008, 05:01 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
javac *.java
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 09-09-2008, 07:15 PM
Member
 
Join Date: Sep 2008
Posts: 31
khajalid is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 09-09-2008, 08:28 PM
Senior Member
 
Join Date: Jun 2008
Posts: 469
masijade is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 09-10-2008, 08:38 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 191
danielstoner is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 09-10-2008, 10:23 AM
Senior Member
 
Join Date: Jun 2008
Posts: 469
masijade is on a distinguished road
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.

Code:
// Filename BogusA.java package test; class BogusA { BogusA() { System.out.println("A"); } } class BogusB { BogusB() { System.out.println("B"); } }
Code:
// Filename BogusC.java package test; public class BogusC { public static void main(String[] args) { new BogusA(); new BogusB(); } }
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).
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 09-10-2008, 03:24 PM
Member
 
Join Date: Sep 2008
Posts: 17
sagarbirari is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Organize class source code Alejandro Valdez Eclipse 0 05-16-2008 04:58 PM
Class Reflection: Finding super class names Java Tip java.lang 0 04-23-2008 10:12 PM
Binaries and Source File sumation NetBeans 0 04-03-2008 09:14 PM
subclass vs inner class bugger New To Java 1 01-13-2008 09:31 PM
which class is superclass and subclass? java_fun2007 New To Java 0 12-11-2007 10:55 PM


All times are GMT +3. The time now is 01:11 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org