I have the following code and when I try to write the method rellenarTipo
it gives me an error, I have created a main class called main
and another class called subclase
that inherits from main
where @Override
when I put it, it indicates an error
public class main {
public static void main(String[] args) {
rellenarTipo();
}
public static void rellenarTipo() {
String dni, nombre;
Scanner scan = new Scanner(System.in);
System.out.println("introduza nombre");
nombre = scan.next();
System.out.println("introduzca dni");
dni = scan.next();
}
}
And this is the subclass that inherits from the class main
and where I want to write fill type, can someone guide me a bit to know what I'm doing wrong?
public class SubClase extends main {
int edad;
@Override
public static void rellenarTipo(){
System.out.println("introduzca edad");
}
}
In Java, static methods
static
cannot be overridden since static members belong to the class, not to instances of the class. You must declare non-static methods for it.Your class should look like this:
Also, if you want to store data for your class, it should not be variables local to the methods, but should be fields of the class. So your class layout should look like this: