Java: Can a class inherit from two super classes a

2019-05-10 20:25发布

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?

10条回答
We Are One
2楼-- · 2019-05-10 20:53

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-05-10 20:56

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.

查看更多
放荡不羁爱自由
4楼-- · 2019-05-10 20:57

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.

查看更多
劫难
5楼-- · 2019-05-10 21:06

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.

查看更多
太酷不给撩
6楼-- · 2019-05-10 21:09

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.

查看更多
啃猪蹄的小仙女
7楼-- · 2019-05-10 21:09

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.

查看更多
登录 后发表回答