+ 1

void methods declared with the final keyword does what?

sample code: ______________________________ public final void setName(String name) { this.name = name; } ______________________________ since void doesn't return any value, then why can the final keyword be used with it? I don't really get it.

22nd May 2018, 2:21 PM
Moses Katsina
Moses Katsina - avatar
2 Réponses
+ 5
the final keyword ensures a method cannot be overridden whether void or not
22nd May 2018, 2:23 PM
᠌᠌Code X
᠌᠌Code X - avatar
+ 3
Brains is right, if you would try to override the method in the subclass that would give an error, for example... class A { public final void a() {} } class B extends A { public final void a() {} //Error }
22nd May 2018, 6:21 PM
Elmer Martens
Elmer Martens - avatar