Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

subclassing...

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Can I make a class extend 2 different classes?
I need a class to inherite the methods of another one so I'll just subclass it, but I'd also like to make this class extend JInternalFrame class.
Is this possible?

something like

public class my_subclass extends my_class, JInternalFrame {

}

like this it won't work, I tryed, but is there a way to do it?

any other suggestions are welcome.

thank you.
 
multiple inheritance can only be done in Java by implementing interfaces. its more restrictive than in C++.
 
thanks for the reply.

So what would be the way of doing this?

Can anyone give me some help please? I'm all in dark here :(
 
you just extend a class as you would normally

class extends otherClass implements ActionListener, MouseListener etc ect

I am not sure if there is a limit to the number of interfaces you can implement as I have only used 3 as a max myself. You should also be able to implement adapter classes that are basically cut down versions of interfaces.

For an explanation of interfaces and adapters you will need to consult a book. Or wait for some one else to fill you in. I have seena couple of threads here recently wexpalining them, so maybe you can utilise the search facility of this website, or even better go to java docs at suns website:


best of luck.
 
An adapter class is basically used for convenience. An adapter usually extends an interface with many methods that are not always used. Most adapter classes give empty definitions for these methods, though some give basic functionality. So instead of litering your class with a bunch of empty methods it could be easier to use an adapter class and override the methods that you need.

For an easy example of this you need to look no further than J2SE. Take the
Code:
MouseAdapter
as an example. It implements
Code:
MouseListener
, here is the source for
Code:
MouseListener
:
Code:
public interface MouseListener extends EventListener {

    /**
     * Invoked when the mouse has been clicked on a component.
     */
    public void mouseClicked(MouseEvent e);

    /**
     * Invoked when a mouse button has been pressed on a component.
     */
    public void mousePressed(MouseEvent e);

    /**
     * Invoked when a mouse button has been released on a component.
     */
    public void mouseReleased(MouseEvent e);

    /**
     * Invoked when the mouse enters a component.
     */
    public void mouseEntered(MouseEvent e);

    /**
     * Invoked when the mouse exits a component.
     */
    public void mouseExited(MouseEvent e);
}
Very simple but in most cases you will not need to implement the functionality for everyone of these events when using
Code:
MouseListener
. So in comes
Code:
MouseAdapter
, here is the source:
Code:
public abstract class MouseAdapter implements MouseListener {
    /**
     * Invoked when the mouse has been clicked on a component.
     */
    public void mouseClicked(MouseEvent e) {}

    /**
     * Invoked when a mouse button has been pressed on a component.
     */
    public void mousePressed(MouseEvent e) {}

    /**
     * Invoked when a mouse button has been released on a component.
     */
    public void mouseReleased(MouseEvent e) {}

    /**
     * Invoked when the mouse enters a component.
     */
    public void mouseEntered(MouseEvent e) {}

    /**
     * Invoked when the mouse exits a component.
     */
    public void mouseExited(MouseEvent e) {}
}
That's it. Essentially nothing. Each method is an empty definition but now by extending
Code:
MouseAdapter
instead of implementing
Code:
MouseListener
you have effectively streamlined your code.

Sorry for the long post. I was bored. Wushutwist
 
ok, but the problem is I just can't turn a class into an interface.
If I do something like public class extends class1 implements class2 it won't work since class2 isn't an interface.

So isn't it possible for my class to extend JInternalFrame and also inherite the methods of other class?

 
No, only through interfaces or redesign the structure of your classes. Typically in this situation you would probably want to change your other class (myclass?) to inherit from JInternalFrame and then have my_subclass inherit from myclass.

Lack of true multiple inheritance is definitely a drawback of Java but it definitely simplifies things. MI tends to make things pretty messy. In most cases, good design will solve the need of MI. Wushutwist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top