Java Inheritance: How to achieve something like "multiple inheritance" when it is forbidden in Java?

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

There are many methods that are common to these two classes, so I would like to create a class called “Files” and make it “FileSub1” and “FileSub2” its subclasses. But Java does not support multiple inheritance! What can i do here?

+5
source share
3 answers

, . , , :

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() {}
}
+12

FileThing JPanel, , FileThing JPanel.

+16

You can do as below

public class Files extends JPanel{
}

public class FileSub1 extends Files{
}

public class FileSub2 extends Files{
}
+3
source

All Articles