No reason to extend if all you are doing is setting properties that the parent already has.
What you are extending (aka overriding) is the parent's implemented functionality.
An example parallel to the OP's toString() example is paintComponent().
Often you will want your custom component to paint the background, border, and the contained components, and THEN do some custom painting.
Say drawing a line to the location of the mouse cursor:
Code:
protected void paintComponent(Graphics g){
super.paintComponent(g); // draws bg, and calls paintBorder() and paintChildren()
g.drawLine(startPoint, mouseLoc);
}
In java, using the super keyword is not always necessary, as it is automatically assumed if the child class does not have the method. So it is often there, implied, similar to 'this'.
It may be better, depending on the application, to make Car class extend AbstractVehicle which in turn implements some interfaces like ColorChangeable.
It can create a single point of maintenance so that you don't have to implement changeColor() in each vehicle, then forget to do it somewhere and create more headaches.
Sometimes there will even be classes that implement methods from a superclass with a no-op only because they have to in order to take advantage of the super's other qualities:
Code:
public class MouseMotionPanel extends JPanel implements MouseMotionListener{
public MouseMotionPanel(){
this.addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent me) {
Point pt = me.getPoint();
System.out.println("Point is "+ pt.x + "," + pt.y);
}
// No-op, but required to implement MouseMotionListener
public void mouseMoved(MouseEvent me) {
}
}