Reputation: 2159
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.
Upvotes: 1
Views: 6537
Reputation: 10667
Multiple Inheritance is NOT supported in Java. But still Java provides other ways to achieve multiple inheritance.
Upvotes: 1
Reputation: 621
Make class A an interface and B implementing it like this :
public interface A{
public ....[Variables]
}
public class B extends javax.swing.JFrame implements A{
...
}
Upvotes: 1
Reputation: 10543
Use composition or aggregation. Learn more about Has-A
relationship.
Upvotes: 1
Reputation: 500167
Java does not support multiple inheritance, so you can't extend both A
and JFrame
.
You could either turn A
into an interface, or embed an instance of A
into B
.
Upvotes: 3
Reputation: 15644
You can do like this :
public class A extends javax.swing.JFrame{
...
}
and then
public class B extends A{
...
}
Upvotes: 2
Reputation: 4202
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
}
}
Upvotes: 4