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?
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?
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.
From the Inheritance page of the Sun Java Tutorial
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.
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.
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 (aJFrame
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:Now you can create an instance of PlannedJourney and add it to the applications
JFrame
at the appropriate position.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.