Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java inheritance: How to achieve something similar to "multiple inheritance" when it is not allowed in Java?

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?

like image 792
PeakGen Avatar asked Jun 01 '12 17:06

PeakGen


1 Answers

Prefer Composition over Inheritance

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

like image 87
Carl Manaster Avatar answered Oct 13 '22 11:10

Carl Manaster