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 2 of 2

Thread: Suggestions needed for returning subclass objects to superclass main

  1. #1
    Junior Member
    Join Date
    Aug 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Suggestions needed for returning subclass objects to superclass main

    I am designing a bunch of JPanels, so to make it a bit easier for myself I've made a CustomPanel class that extends JPanel. This class contains a main method that sets up a JFrame with the JPanel inside. All the JPanels I design then extend CustomPanel, this way I only need the main method of each Panel to call the main of the CustomPanel class, rather than having to build a JFrame in each new Panel class. Let's say that I'd have a class TestPanel that extends CustomPanel.

    The problem arises when I need to actually add a Panel to the JFrame when I call the main method in the corresponding Panel class.
    I want to add a way to the CustomPanel class of getting an object of the Panel class from which I'm calling the main method. So for example, adding a TestPanel object. But because the main method is static, any method used for this would have to be static too. This means I can't override that method in each Panel class to return an object of that Panel. And Method hiding doesn't work either, as I would call the static method on the CustomPanel class which has to return null, or a Custom Panel, thus adding my CustomPanel or null to my JFrame.

    I'm struggling to see any way of overcoming this obstacle, which is why I'm hoping someone will have some suggestions/implementations for how to do this. I have deliberately chosen not to post any code, as it would just be empty classes, and I'm not even sure if a static method is the way to go, or if there are other ways of achieving this.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Suggestions needed for returning subclass objects to superclass main

    Can you post a small, simple code that shows the problem?

    The way methods in one class get references to object instances that exist in another class is to use getter methods:
      SomeClass  someClassRef = classRef.getReferenceSToSomeClassObject();

    Note programs should not do much work in main methods. Move the logic to the constructor.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: November 12th, 2012, 03:24 PM
  2. Problem with subclass and superclass methods
    By Farmer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 15th, 2011, 08:43 PM
  3. [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
  4. [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
  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