There are serveral ways to do that. But first, you have to notice that in your code Class A and Class B are not related at all.
In this case, I mean, if your classes are not related, you may want to use composition instead of inheritance. That's to say, you can create a wrapper (or Adpater).
Sample code
Code:
class Adaptee{
public void adaptedOperation(String text){
System.out.println(text);
}
}
class Adapter{
private Adaptee adaptee;
public Adapter(){
adaptee = new Adaptee();
}
public void doOperation(){
adaptee.adaptedOperation("Called from adapter");
}
}
public class Application {
public static void main(String[] args) {
Adapter adapter = new Adapter();
adapter.doOperation();
}
}
But this approach may have disadventages if you have to make a class hierachy out of Adapter.
Another perpective is inheritance (which seems to be the one that you sugest)
There are two ways to do it.
Sample code
Code:
class A extends JFrame{
public void myOperation(){
System.out.println();
}
}
class B extends A{
public void anotherOperation(){
//Calls class A method
this.myOperation();
//Calls JFrame method
this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
}
}
But to do this you have to inherit twice. That´s to say, A must first inherits from JFrame, so that you can add a new method and then you make B inherit from A to call that method, besides the methods of JFrame. All this because Java does not have multiple inheritance and you cannot make class B inherits from JFrame and class A at the same time.
So, there is another way to do it. You can build an interface which is a protocol that tells the implementor classes which methods they shall implement.
Code:
//Interface just declares methods, but do not implement them
interface A{
public void doSomething();
}
//Inherits from JFrame and implements the interface methods
class B extends JFrame implements A{
public void doSomething() {
System.out.println("I implemented doSomething");
getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
}
}
Now, evidentrly, when you use inheritance any subclass can call the methods of its parent class, as you can see in the samples above, I call the getContentPane() methods of the JFrame class.
I hope it helps.
edalorzo@hotmail.com