0

I'm studying again some OOP concepts so I made this simple code just to see how overriding methods work and surprise, it's not working. Also, just ignore the fact that the class name is HelloWorld, it's just a file for studying purposes. It's already changed to Filha.java btw

class HelloWorld {

    public static void imprimir() {
        System.out.println("Oie");
    }//

    public static void imprime(int a) {
        System.out.println("27");
    }//

}//

public class Filha extends HelloWorld {

    public static void main(String[] args) {
        imprimir();
        imprime(55);
    }//

    @Override
    public static void imprimir() {
        System.out.println("Ola");
    }//

    @Override
    public static void imprime(int num) {
        System.out.println(num);
    }//

}// 

Yesterday, the error was Exception in thread "main" java.lang.Error: Unresolved compilation problem in the public static void main(String[] args) { line, but I turned off my computer, turned it on today and it's not there anymore.

Now the error is The method imprimir() of type Filha must override or implement a supertype method for the public static void imprimir() { line. There's also the error in the main method where it's calling imprimir(), as expected.

I don't really know what other information to give since it's a simple code with a simple problem (I believe), it's also my first question so if I'm leaving anything out please ask and I'll be more throughout.

xhmdb
  • 1
  • https://stackoverflow.com/questions/2475259/can-i-override-and-overload-static-methods-in-java/5436790. – Suraj Jun 16 '20 at 16:36
  • Static methods cannot be overridden. They can only be hidden. Which static method is invoked is determined at compile-time—they're not runtime-polymorphic. – Slaw Jun 16 '20 at 16:36
  • https://javarevisited.blogspot.com/2013/03/can-we-overload-and-override-static-method-java.html#:~:text=Answer%20is%2C%20No%2C%20you%20can,that%20is%20called%20method%20hiding. – Suraj Jun 16 '20 at 16:37
  • You cannot override a static method. – Patrick Magee Jun 16 '20 at 16:37

2 Answers2

0

with your code

class HelloWorld {

    public static void imprimir() {
        System.out.println("Oie");
    }//

    public static void imprime(int a) {
        System.out.println("27");
    }//

}//

public class Filha extends HelloWorld {

    public static void main(String[] args) {
        imprimir();
        imprime(55);
    }//

    @Override
    public static void imprimir() {
        System.out.println("Ola");
    }//

    @Override
    public static void imprime(int num) {
        System.out.println(num);
    }//

}// 

you are not overriding anything. If you remove the @Override the code will run normally but only shows what you have declared within your main method, as static methods can't be overridden as below:

class HelloWorld {

    public static void imprimir() {
        System.out.println("Oie");
    }//

    public static void imprime(int a) {
        System.out.println("27");
    }//

}//

public class Filha extends HelloWorld {

    public static void main(String[] args) {
        imprimir();
        imprime(55);
    }//

    public static void imprimir() {
        System.out.println("Ola");
    }//

    public static void imprime(int num) {
        System.out.println(num);
    }//

}// 
0

You need to use abstract class instead of normal class and in HelloWorld class you need to replace the regular methods with abstract methods without implementation and in the Filha class replace extends with implements and you will successfuly be able to @Override the functions

Note: static methods cannot be overriden