简体   繁体   中英

Netbeans Override Annotation

i'm trying to create a new class that inherits from an abstract superclass (contains three abstract methods). The issue is that netbeans gives me a warning : add @override annotation. why should i do this (add this annotation) if i'm not overriding any method. What's the problem ?

Superclass is

abstract class Vehicul {

   String denumireaVehiculului;
   float lungimeaMinimaVehicul;
   int numarulMinimDeLocuri;

   public abstract void mediulDeDeplasareAVehiculului();
   public abstract void nivelulDeSiguranta();

   // Constructor implicit
   Vehicul() {

       denumireaVehiculului = "Eu sint vehicul";
       System.out.println(denumireaVehiculului);

       lungimeaMinimaVehicul = 3.50f;
       System.out.println("Lungimea minima este "+lungimeaMinimaVehicul);

       numarulMinimDeLocuri = 2;
       System.out.println("Numarul minim de locure este "+numarulMinimDeLocuri);
   }
}

Subclass is

 public class Avion extends Vehicul {

 public void mediulDeDeplasareAVehiculului() {

}

 public  void  nivelulDeSiguranta() {

}

public String getDenumireaVehiculului() {

     return "Avion";

public void   afiseazaCineEsti() {

        System.out.println("Eu sunt un avion");

   }

}

Because you are overriding (sort of - see below).

public class Avion extends Vehicul {

     public void mediulDeDeplasareAVehiculului() {
     ...
}

One reason for adding the annotation is to protect yourself from yourself. Without the annotation if the abstract class were modified and the abstract method removed, the method in the subclass would become "normal" without you knowing it.

With the annotation, the compiler will say "Hey - you're telling me you're overriding something, but you're not"

Here is what the documentation for @Override says:

Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.

I know it is a bit confusing, because you are not actually overriding anything (you are implementing it!), but that is just how it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM