Search code examples
javaswinginheritancejframeextend

Inheritance in Java: Extending another class when I've already extended one


I have to extend class A's variables into Class B. For that I have to write:

public class B extends class A

But in my case, the place is already taken up by "extends javax.swing.JFrame". It looks like:

public class B extends javax.swing.JFrame

Please suggest any method to inherit variables from class A to class B. I am very new to this field. So please explain.


Solution

  • Either use composition or create an inner class in class B which extends class A.

     class B extends JFrame {
         A a = ... // this is one option
    
         class C extends A {
             // this is another option 
         }  
    
     }