Hi!

I have two classes:
1) Class A extends JPanel
2) Class B extends JFrame

I would like to use some methods from Class A in Class B. How could I implement it?

Thanks!

Well, If I understand your question right, then this code would probably do what you are saying.

class A extends JPanel{
   public static void methodcalledfromB(){
      //some code
   }
   public static void methodcalledfromA(){
      B.methodcalledfromA();
   }
}
class B extends JFrame {
   public static void methodcalledfromB(){
      A.methodcalledfromB();
   }
   public static void methodcalledfromA(){
      //some code
   }
}

If you could give some specifics then I could probably come up with a better solution. becouase this is just the quick and dirty method of doing it.

Edited 6 Years Ago by Gerbiler: n/a

If the two methods are not exactly the same, you could use an interface.

If they are exactly the same, the easiest way would probably be to copy and paste the method, seeing as Java doesn't support multiple inheritance and that might not make sense anyway (you could use an interface here too, but it wouldn't be a great design decision).

Thank you! This is exactly what I wanted to know. I knew that a static method can be used, however I didn't want to rewrite it. So, B.methodcalledfromA(); is a good idea.

Well, If I understand your question right, then this code would probably do what you are saying.

class A extends JPanel{
   public static void methodcalledfromB(){
      //some code
   }
   public static void methodcalledfromA(){
      B.methodcalledfromA();
   }
}
class B extends JFrame {
   public static void methodcalledfromB(){
      A.methodcalledfromB();
   }
   public static void methodcalledfromA(){
      //some code
   }
}

If you could give some specifics then I could probably come up with a better solution. becouase this is just the quick and dirty method of doing it.

If I can be forgiven a simple question - what's wrong with simply calling the methods?

someVariableInA=B.doSomeWork(x);

Why would you need the methods to be static?


An interface is not a means of inheritance at all, of course, and shouldn't be confused with one. Implementing an interface would have nothing to do with this problem, it would only serve to tell the compiler that you've solved it by brute force.

I smell some design confusion in the original question, but it's possible I'm mistaken, so I won't pursue that line any further. I'll only observe that the point of object oriented programming is to let each object tend to its own knitting, and if you have classes which have the same methods in them, that's often a sign that your classes are not correctly divided.

Well, the compiler asks the methods called from class B to be static...
Probably, you are right here "...that's often a sign that your classes are not correctly divided". I'll check it out once again.

If I can be forgiven a simple question - what's wrong with simply calling the methods?

someVariableInA=B.doSomeWork(x);

Why would you need the methods to be static?


An interface is not a means of inheritance at all, of course, and shouldn't be confused with one. Implementing an interface would have nothing to do with this problem, it would only serve to tell the compiler that you've solved it by brute force.

I smell some design confusion in the original question, but it's possible I'm mistaken, so I won't pursue that line any further. I'll only observe that the point of object oriented programming is to let each object tend to its own knitting, and if you have classes which have the same methods in them, that's often a sign that your classes are not correctly divided.

This question has already been answered. Start a new discussion instead.