Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: how to extend a subclass?!

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default how to extend a subclass?!

    hi,
    i want to write a class that extends from a subclass of another class,something like this:

    public class A{
    //some codes here
    public class B{
    //some codes here
    }
    }

    public class C extends A.B ( or public class C extends B ){
    //some codes here
    }

    can anyone help me with that?!
    Last edited by migongotar; August 2nd, 2011 at 11:48 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: how to extend a subclass?!

    What happened when you tried? Did you get an error message?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to extend a subclass?!

    when i try this code (public class C extends A.B) i get this error message :
    No enclosing instance of type DataStructures is available due to some
    intermediate constructor invocation

    i dont know how to resolve this error message...
    Last edited by migongotar; August 2nd, 2011 at 12:23 PM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: how to extend a subclass?!

    That's because your B class is not a subclass, it's an inner class. And since your B class is not static, it belongs to a specific instance of A. So when you try to make C extend B, the compiler says "I can't reach B without an instance of A".

    Is there a reason you're keeping B as an inner class? Is there a reason it's not static?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    migongotar (August 2nd, 2011)

  6. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to extend a subclass?!

    in fact, there's no reason! i just had no idea that static class would solve my problem and i forgot to try it! thanks anyway!

Similar Threads

  1. [SOLVED] Help regarding Superclass variable can reference subclass object
    By rohan22 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 12th, 2011, 01:30 AM
  2. [SOLVED] Invoking a superclass constructor in my subclass
    By kari4848 in forum Object Oriented Programming
    Replies: 5
    Last Post: April 29th, 2011, 01:12 PM
  3. subclass that implements
    By speedycerv in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2011, 07:35 AM
  4. is there a better way to extend an array?
    By kurt-hardy in forum Collections and Generics
    Replies: 5
    Last Post: January 12th, 2011, 06:33 PM
  5. Instantiating a SuperClass Map from a SubClass
    By drexasaurus in forum Collections and Generics
    Replies: 1
    Last Post: September 9th, 2010, 10:51 PM

Tags for this Thread