Search code examples
javasuperclass

Java method does not override a method from a supertype


I am just learning how inheritance works in java and I came across a problem.

I have a base class which defines the method KlickEvent

import java.awt.*;

import java.awt.event.*;
public class Handlerclass implements MouseListener, MouseMotionListener
{
    public Handlerclass()
{

}
public void mouseClicked(MouseEvent event)
{

}
public void mousePressed(MouseEvent event)
{
    KlickEvent(event.getX(),event.getY());
}
public void mouseReleased(MouseEvent event)
{
    System.out.println("Eine Maustaste wurde losgelassen");     
}
public void mouseEntered(MouseEvent event)
{
    System.out.println("Eine Maus hat den Bereich betreten");
}
public void mouseExited(MouseEvent event)
{
    System.out.println("Eine Maus hat den Bereich verlassen");
}
public void mouseDragged(MouseEvent event)
{
    //System.out.println("Eine Maus wird herumgezogen");
}
public void mouseMoved(MouseEvent event)
{
    //System.out.println("Eine Maus bewegt sich");
}

@SuppressWarnings("unused")
public void KlickEvent(int x, int y)
{
    System.out.println("klick");
}
}

Another class is inheriting from that class and there I am trying to override the method KlickEvent but I get the Error message "method does not override or implement a method from a supertype".

import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;

public class GUI extends Handlerclass
{
    JFrame Frame;
    int Sizex, Sizey;
    public GUI()
    {
        this(800,600,"Unbenannt");
    }
    public GUI(int x, int y, String Fensternahme)
    {
        Sizex = x;
        Sizey = y;
        Frame = new JFrame(Fensternahme);
        Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Frame.setSize(Sizex,Sizey);
        Handlerclass handler = new Handlerclass();
        Frame.addMouseListener(handler);
        Frame.addMouseMotionListener(handler);
        Frame.setVisible(true);
    }
    @Override
    public void KlickEvent()
    {
        System.out.println("test");
    }
}

How can I fix this? And I'm sorry that this question has been asked alot but I still couldn't figure it out.


Solution

  • The methods aren't the same, the one on Handlerclass has 2 parameters while the one on GUI has any, so it's not overriding the method in Handlerclass but rather defining its own KlickEvent method.

    I bet your IDE should be notifying you about that with a yellow / red mark (or any other symbol) to the left of the method in GUI class (maybe over the line number).

    So, as @ElliottFrisch mentioned in his answer, add the parameter types to match the signature of the parent class so it's actually overriding it and not just defining its own method.


    BTW a side note is to follow Java naming conventions:

    • firstWordLowerCaseVariable
    • firstWordLowerCaseMethod()
    • FirstWordUpperCaseClass
    • ALL_WORDS_UPPER_CASE_CONSTANT

    This will make your code easier to read to you and us, for example:

    JFrame Frame; -> JFrame frame;
    int Sizex, Sizey; -> int sizeX, sizeY;