5

This is a question mostly about Java inheritance. I am developing a program which has 2 windows, both of which will be developed in separate classes which will extend JPanel. The first class is "FileSub1" and the second one is "FileSub2".

There are a lot of methods that are common to these two classes, so I would like to create a class called "Files" and make "FileSub1" and "FileSub2" its subclasses. But Java doesn't support multiple inheritance! What can I do here?

Shaun the Sheep
  • 22,353
  • 1
  • 72
  • 100
PeakGen
  • 21,894
  • 86
  • 261
  • 463

3 Answers3

16

Prefer Composition over Inheritance

Include a FileThing in your JPanel subclass, instead of making it a FileThing and a JPanel.

Community
  • 1
  • 1
Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
  • 1
    Exactly. You shouldn't mix the GUI code with the file I/O code directly. 1+ – Hovercraft Full Of Eels Jun 01 '12 at 17:28
  • I beg your apologies, I didn't understand. More explanations please? – PeakGen Jun 01 '12 at 17:33
  • Please read the question I linked to and its wonderful answers. You should understand what the phrase "Prefer composition over inheritance" means; when you do my answer requires no elaboration. – Carl Manaster Jun 01 '12 at 17:34
  • @Sepala: your GUI classes should hold only GUI code. Look into the MVC pattern (Google this) if you don't want your code to be a debugging nightmare. – Hovercraft Full Of Eels Jun 01 '12 at 18:31
  • @HovercraftFullOfEels: Appreciate your reply. If you don't mind, could you please give me a small code example? – PeakGen Jun 02 '12 at 02:55
  • @HovercraftFullOfEels: Hold on hold on! I guess I am getting it now. This is what you are telling: Create 2 classes extending JPanel. Create another 2 classes extending "Files" class. Let those 2 GUI classes to do their work by communicating with those two subclasses of "Files". This is it right? Awesome! – PeakGen Jun 02 '12 at 07:00
  • @Sepala: if the file routines are the same for both, then you may only need one FileManipulation (or whatever name) class that your GUI's controllers would hold a reference to and call. But no one can give you any more than the most general advice unless you give us more specific information about your problem. – Hovercraft Full Of Eels Jun 02 '12 at 12:43
13

I don't see why you need multiple inheritance. As far as I can tell you should be fine with a an abstract base class that implements the common methods:

public abstract class AbstractFilePanel extends JPanel
{
    public void commonMethod1() {}
}

public class FileSub1 extends AbstractFilePanel 
{
    public void sub1Method() {}
}

public class FileSub2 extends AbstractFilePanel 
{
    public void sub2Method() {}
}
Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
  • HMMM...This seems nice too. Why did u make the class abstract? – PeakGen Jun 01 '12 at 17:47
  • @Sepala: my understanding is that the base class should not be used, therefor I made it abstract. If it can used "standalone" then apparently there is no reason to do so. –  Jun 01 '12 at 17:51
  • Yes. Base class will not be used.. I guess I am getting your point. We can use NON ABSTRACT methods and FIELDS in this abstract class, so the methods can have a body and I can use them without any overriding. Isn't it? – PeakGen Jun 01 '12 at 18:00
  • I am lucky today to make this a nice answer :-) – nIcE cOw Jun 02 '12 at 05:26
  • Thanks a lot for the reply and I am selecting this as the answer. However, considering the answer of "Carl", I changed my class diagram. Now, all the GUI stuff extends from the "AbstractFilePanel" as in your code and all the IO stuff extends from the "FileBase". Thanks again for the help :) – PeakGen Jun 08 '12 at 17:47
3

You can do as below

public class Files extends JPanel{
}

public class FileSub1 extends Files{
}

public class FileSub2 extends Files{
}
raddykrish
  • 1,866
  • 13
  • 15