-->

Java: Can a class inherit from two super classes a

2019-05-10 20:12发布

问题:

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?

回答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.



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



回答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.



回答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.



回答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.



回答6:

No. There is no multiple inheritance in Java. This was a deliberate design decision.

If you can find a way to express one of the superclasses as an interface instead, then this might solve your problem.



回答7:

I think a better way might be to have PlannedJourney extend Journey and then have PlannedJourney have an instance of JFrame. It looks like a way to kill two birds with one stone depending on how you view it.



回答8:

Multiple inheritance in Java isn't possible.

Multiple implementations of interfaces is possible, this is the closest thing to multiple inheritance in Java you've got.

If possible, favor composition over inheritance. Quote taken from Effective Java.



回答9:

If you haven't read "Effective Java" by Joshua Bloch now is the time to do it. Particularly the section "favor composition over inheritance". Since you can't do multiple inheritence in Java, he explains why in almost all cases the right approach is to have your JFrame extension CONTAIN a reference to the PlannedJourney it is the UI for.



回答10:

Not exactly sure what you want but multiple inheritance is a very possible and common thing in Java. Firstly when a class inherits from another class, if that class is also a subclass of another class it will pass on it's inherited methods to it's subclasses. Secondly, within Java there is an string of coding otherwise known as 'the Interface', this allows for a class to inherit from multiple classes without extending a class.