Results 1 to 18 of 18
  1. #1
    makbar24 is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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?

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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 11:50 AM.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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:
    public class testInher {

    public static void main(String [] args)
    { B e = new B();
    e.displayFromB();
    }
    }
    properly formatted (but not changed) as
    Java 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.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ah, I see it in different way. Sorry about that. I've not test in a peace of paper or something else.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,877
    Rep Power
    25

    Default

    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.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  9. #9
    makbar24 is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    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 03:26 AM. Reason: add one more question

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,877
    Rep Power
    25

    Default

    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 *

  12. #12
    makbar24 is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    hi Norm, thank you for ur reply. how/when/where to use "*".

    thank you

    Ovi

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,877
    Rep Power
    25

    Default

    javac *.java

  14. #14
    khajalid is offline Member
    Join Date
    Sep 2008
    Posts
    31
    Rep Power
    0

    Default

    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

  15. #15
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  16. #16
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    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

  17. #17
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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).

  18. #18
    sagarbirari is offline Member
    Join Date
    Sep 2008
    Posts
    17
    Rep Power
    0

    Default

    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

Similar Threads

  1. Organize class source code
    By Alejandro Valdez in forum Eclipse
    Replies: 0
    Last Post: 05-16-2008, 03:58 PM
  2. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:12 PM
  3. Binaries and Source File
    By sumation in forum NetBeans
    Replies: 0
    Last Post: 04-03-2008, 08:14 PM
  4. subclass vs inner class
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:31 PM
  5. which class is superclass and subclass?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 09:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •