JVM - 子类方法的执行顺序和@override的使用

Und*_*Dog 4 java oop

这是一个新手问题.我读到JVM的执行是从层次结构中从最低级别搜索methodname开始的,如果该方法在该类中不可用,则它将遍历到查找该方法的父类.

如果是这种情况那么为什么我们需要使用"@override"将自定义逻辑添加到继承的类中?

下面的例子说明了我的问题

class superclassA
{

method()
{
}

}

class subclassB extends superclassA
{

@Override 
//While executing if JVM starts looking for the method name from the lowest hierarchy
//then why do we have to use "override" as the methodname will be matched from the lowest level itself?
method() 
{
--custom subclass specific code...
} 

}
Run Code Online (Sandbox Code Playgroud)

Mic*_*rdt 5

如果是这种情况那么为什么我们需要使用"@override"将自定义逻辑添加到继承的类中?

我们没有.该@Override注解没有技术含义-它存在于文件的事实,此方法将覆盖一个超类,它具有一定的优势:

  • 如果查看代码,它会告诉您有一个超类方法可能对了解此方法的作用很重要
  • 如果超类方法的签名以子类方法实际上不再覆盖它的方式更改,则会出现编译器错误.
  • 如果您在不使用注释的情况下覆盖方法,则可以获得编译器警告,以防您无意中执行此操作.