Java: Can a class inherit from two super classes at the same time?

25,598

Solution 1

Don't mix models and views. If you keep both domains clearly separated, then you won't be in need of multiple inheritance (this time).

You have one model class for journeys and a viewer for such journeys. The viewer should subclass Component and be displayed (somewhere) in your window (a JFrame instance).

This viewer takes one journey object and presents the journey's properties. Journey alone has all information about a journey, only the viewer knows how to present a journey to the user:

 public class PlannedJourney extends JPanel {
   private Journey journey;
   public JourneyViewer(Journey journey) {
     this.journey = journey;

     // build this panel based on the journey object
     // (add labels, subpanels, widgets, ...)
   }
 }

Now you can create an instance of PlannedJourney and add it to the applications JFrame at the appropriate position.

Solution 2

No multiple inheritance in Java. It's not allowed by the language. Check if you can change your design to use Interface instead. (it's almost always possible).

Solution 3

No this is not possible with Java. Usually, a similar effect can be achieved using interfaces (I'm thinking Journey may be an interface here), though if you wish to inherit method implementations, then you'll have to rethink your design.

My best guess from your description is that you can separate your classes into one hierarchy that represents the data model, and another that extends JFrame to display the data.

Solution 4

From the Inheritance page of the Sun Java Tutorial

Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.

Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.

Solution 5

Either make Journey extend JFrame or make Journey an interface.

Choose the option that makes the most sense for your object structure.

If it makes sense for Journey to extend JFrame then do that. This way, when PlannedJourney extends Journey, it also inherits everything that Journey does from JFrame.

If this doesn't make sense, then make Journey an interface, and have PlannedJourney implement Journey. PlannedJourney will not be able to inherit any code from Journey, but you will be able to inherit method signatures and pass the object around as a Journey in addition to as a JFrame.


Be sure when you're working with Swing that you are separating out the Model from the View. Your data should be stored in one set of objects (Model), and the Swing components to display that data should be a separate set of objects (View). This helps to encapsulate data functions, separate them from GUI objects, and enforce a one-way reliance of the View on the Model. Consider that a Journey object should perhaps require a JourneyFrame to display. This way, the JourneyFrame can take care of just displaying it and can extend JFrame and carry only a reference to Journey, which can have its own hierarchy.

Share:
25,598
Manish Basdeo
Author by

Manish Basdeo

Updated on July 09, 2022

Comments

  • Manish Basdeo
    Manish Basdeo almost 2 years

    I have a class Journey which I want to make a superclass and another class plannedjourney. The plannedjourney class extends JFrame since it contains forms..However I also want this class to extends Journey..

    Is there a possible way to do this?