- 1

What does thiz keyword means @override should i use this when i override

15th Aug 2017, 10:24 AM
corejava
6 Réponses
+ 9
It works as a safety measure. If you use this annotation, the compiler will get to know that you're going to write an overriden method next. So it'll be able to show error if you make any mistake while writing the prototype. public double calculateGrade(){ // parent method // implementation } public double calculategrades(){ // overriden method in child class // no error in spite of different spelling } @Override public double calculategrades(){ /* will show error, because there's no matching prototype in parent class */ } It's best practice to use this annotation to avoid mistake.
15th Aug 2017, 10:34 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 6
Yes, it'll work
15th Aug 2017, 10:37 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 6
Exactly, @Override notation helps you by showing error in this type of mistakes. While overriding, we have to follow same spelling. Otherwise it will be treated as different. If I don't use @Override, when I'll write wrong spelling, the compiler will allow me to do that. If I use that notation, the compiler will show error and make me notified about my mistake. This is the significance of this notation.
15th Aug 2017, 10:47 AM
Shamima Yasmin
Shamima Yasmin - avatar
0
if I don't use this @override keywrd .will the code works
15th Aug 2017, 10:36 AM
corejava
0
in parent class u used calculateGrade() and in child class u used calculategrades() where did u overide a method ....spelling differs then it's not same method right
15th Aug 2017, 10:39 AM
corejava
0
if you override a method, complier will use the overriden method instead of the one define in the parent class. it's useful when you are inheriting from a class. if you make Android app, you will notice that Android Activity class has bunch of methods for you to override like onCreate, onPause ect
15th Aug 2017, 12:18 PM
Gao Xiangshuai
Gao Xiangshuai - avatar