1

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.

1
  • You can try multilevel inheritance. Mar 29, 2013 at 6:45

6 Answers 6

4

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 
     }  

 }
4
  • still it is saying "package a doesnot exists"
    – Ojas Kale
    Mar 29, 2013 at 7:29
  • all classes and variables are public.
    – Ojas Kale
    Mar 29, 2013 at 7:30
  • still it is saying "package a doesnot exists"
    – Ojas Kale
    Mar 29, 2013 at 7:46
  • theres this "rs" variable in main class . i want to acess it in a class name entryform . so as u said i created an object like "main abc = new main() ;" n fr acessinig rs also write rs.abc.moveToInsertRow(); but its showing error
    – Ojas Kale
    Mar 29, 2013 at 7:48
3

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.

1

You can do like this :

public class A extends javax.swing.JFrame{

...

}

and then

public class B extends A{

...

}
1

Use composition or aggregation. Learn more about Has-A relationship.

Read this and this

1

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{
   ...
}
1

Multiple Inheritance is NOT supported in Java. But still Java provides other ways to achieve multiple inheritance.

  1. Check if you can solve it by using interface, as you can implement multiple interfaces.
  2. Use composition or aggregation. Fundamentally, we achieve code re-usability using inheritance. The same can be achieved using composition/aggregation as well.
  3. Use Inner classes (you can extend multiple classes using inner classes)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.