📜  Java的@Override注解

📅  最后修改于: 2022-05-13 01:55:24.103000             🧑  作者: Mango

Java的@Override注解

@Override 注解是Java 1.5 中首次引入的标准Java注解。 @Override 注解表示子类方法覆盖了基类方法。

出于两个原因,@Override 注释很有用。

  1. 如果带注释的方法实际上没有覆盖任何内容,则编译器会发出警告。
  2. 它可以帮助使源代码更具可读性。

为什么我们使用@Override 注解:

由于以下两个优点,在覆盖方法时使用 @Override 注释被认为是在Java编码的最佳实践:

1) 如果程序员在覆盖时犯了错误,例如使用错误的方法名称或参数类型,则会出现编译时错误。因为您正在通过使用此注释通知编译器您正在覆盖此方法。如果不使用注解,子类方法将被视为子类中的新方法(而不是覆盖方法)。



2) 提高了代码的可读性。如果你改变一个被覆盖方法的签名,所有覆盖它的子类都会抛出一个编译错误,这最终会导致你改变子类中的签名。如果您的应用程序中有大量的类,这个注解将极大地帮助您识别当一个方法的签名改变时需要改变的类。

句法:

public @interface Override

示例 1:不使用抽象类

Java
// Java Program Illustrating Override Annotation
 
// Importing input output classes
import java.io.*;
 
// Class 1
// Parent class
class ParentClass {
 
    // Method inside parent class
    public void display()
    {
 
        // Print statement whenever
        // method of parent class is called
        System.out.println("We are in base class method");
    }
}
 
// Class 2
// Child class
class ChildClass extends ParentClass {
 
    // @Override
    // Method inside child class
    public void display()
    {
 
        // Print statement whenever
        // method of child class is called
        System.out.println("We are in child class method");
    }
}
 
// Clas 3
// OverrideAnnotationTest
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Display message only
        System.out.println(
            "Example of @Override annotation");
 
        // Creating an object of parent class
        // with reference t ochild class
        ParentClass obj = new ChildClass();
 
        // Calling the method to execute inside classes
        obj.display();
    }
}


Java
// Java Program illustrating Override Annotation
// Using Abstract class
 
// Importing input output classes
import java.io.*;
 
// Class 1
// Helper abstract class
abstract class Vehical {
 
    // Calling this method
    public abstract void method();
}
 
// Class 2
// Helper class
class Car extends Vehical {
 
    // @Override
    // Method of Car class
    public void method()
    {
 
        // Print statement whenever this method is called
        System.out.println("This is Car");
    }
}
 
// Class 3
// Helper class
class Bike extends Vehical {
 
    // @Override
    // Method of bike class
    public void method()
    {
 
        // Print statement whenever this method is called
        System.out.println("This is Bike");
    }
}
 
// Class 4
// OverrideAnnotationExample
public class GFG {
    // Main drive method
    public static void main(String[] args)
    {
        // Creating object of both the classes
        // namely Car and Bike
        Car Carobj = new Car();
 
        // Calling method over car object
        Carobj.method();
 
        Bike Bikeobj = new Bike();
 
        // Similarly calling method over bike object
        Bikeobj.method();
    }
}


输出:

示例 2:使用抽象类

Java

// Java Program illustrating Override Annotation
// Using Abstract class
 
// Importing input output classes
import java.io.*;
 
// Class 1
// Helper abstract class
abstract class Vehical {
 
    // Calling this method
    public abstract void method();
}
 
// Class 2
// Helper class
class Car extends Vehical {
 
    // @Override
    // Method of Car class
    public void method()
    {
 
        // Print statement whenever this method is called
        System.out.println("This is Car");
    }
}
 
// Class 3
// Helper class
class Bike extends Vehical {
 
    // @Override
    // Method of bike class
    public void method()
    {
 
        // Print statement whenever this method is called
        System.out.println("This is Bike");
    }
}
 
// Class 4
// OverrideAnnotationExample
public class GFG {
    // Main drive method
    public static void main(String[] args)
    {
        // Creating object of both the classes
        // namely Car and Bike
        Car Carobj = new Car();
 
        // Calling method over car object
        Carobj.method();
 
        Bike Bikeobj = new Bike();
 
        // Similarly calling method over bike object
        Bikeobj.method();
    }
}
输出
This is Car
This is Bike