3

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?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Manish Basdeo
  • 6,139
  • 22
  • 68
  • 102
  • 3
    no. there is no multiple inheritance in java. Check http://stackoverflow.com/questions/3621290/java-multiple-inheritance, http://stackoverflow.com/questions/1038314/alternative-of-multiple-inheritance-in-java, http://stackoverflow.com/questions/3448864/how-does-multiple-inheritance-in-java-work – Bozho Jan 04 '11 at 13:17
  • 6
    Subclassing from Swing components is rarely good idea. These classes are already very large. It's better to use composition (your class will have reference to frame). – Peter Štibraný Jan 04 '11 at 13:19
  • quite a number of answers in a few seconds which you could have avoided by looking a little bit more in the already existing questions (and answers !) :) – LudoMC Jan 04 '11 at 13:21
  • 1
    Extending JFrame is *almost always* wrong. – Qwerky Jan 04 '11 at 13:34

10 Answers10

4

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.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
3

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

LudoMC
  • 750
  • 8
  • 17
  • 1
    It's always possible, it's not always the best solution :) – Philip Potter Jan 04 '11 at 13:20
  • Yes, it's what I meant, as sometimes, even if possible, it's better to think twice about the design and change it instead of implementing something upside-down. – LudoMC Jan 04 '11 at 13:24
2

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.

David Tang
  • 92,262
  • 30
  • 167
  • 149
2

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.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
2

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.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
  • I didn't vote this up because the last paragraph, which is the sensible advice, is contradicted by the first part. "Favor composition over inheritence". – DJClayworth Jan 04 '11 at 16:56
  • The first part was answering the question asked, "Can a class inherit from two super classes at the same time?" I feel it's important to actually answer the question. The second part is the common sense advice about the specific example being cited. I also don't subscribe to blanket statements like "favor composition over inheritance." I will always favor what makes sense over that which does not. – Erick Robertson Jan 06 '11 at 12:24
1

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.

Philip Potter
  • 8,975
  • 2
  • 37
  • 47
1

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.

Norm
  • 583
  • 5
  • 15
1

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.

darioo
  • 46,442
  • 10
  • 75
  • 103
1

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.

DJClayworth
  • 26,349
  • 9
  • 53
  • 79
0

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.