Interviewer tin inquire y'all inquiry "What is final keyword in java". "final" is a keyword which is reserved in java software evolution language. It confine the user. "final" keyword In Java tin live used amongst unlike 3 contexts inwards coffee similar variable, method in addition to class. If define an entity which may alone live assigned once. Now let's elbow grease to empathise how to operate final keyword inwards java software evolution linguistic communication amongst practical examples for variable, method in addition to class.
final variable Once y'all initialize value to lastly variable, You tin non alter it latter on anywhere. It volition remain every bit it is. In bellow given example, WHEELS is lastly variable in addition to initialized amongst 2. Then i am trying to alter it from ii to four but it is showing me compilation error.
public cast FinalKeyword { lastly int WHEELS = 2; //final variable initialized. void bikeWheels() { WHEELS = 4; //This is non allowed. Shows y'all compile fourth dimension error. } world static void main(String[] args) { FinalKeyword fk = novel FinalKeyword(); fk.bikeWheels(); } }
In short, We tin non alter value of lastly variable.
final method
If method is declared amongst lastly keyword, It is called lastly method inwards coffee software evolution language. You tin non override lastly methods inwards sub class. So hither lastly keyword volition helps y'all to confine method overriding. Example is every bit bellow. Here COLLEGENAME() is lastly method inwards raise cast in addition to and hence i am trying to override it inwards youngster cast but it is showing me compile fourth dimension error.
public cast College { lastly void COLLEGENAME(){ //Method declared every bit lastly inwards raise class System.out.println("abc"); } } world cast Department extends College{ void COLLEGENAME(){//It volition present compile fourth dimension mistake because it volition non allow to override lastly methods. System.out.println("xyz"); } world static void main(String args[]){ Department dep= novel Department(); dep.COLLEGENAME(); } }
final class
If cast is declared amongst lastly keyword in addition to hence cast is lastly cast in addition to whatever other cast tin non extend it inwards coffee software evolution language. Here, WILDANIMALS is lastly cast in addition to i am trying to extend it inwards sub cast Cow but it is showing me error.
final cast WILDANIMALS { } //It volition present compile fourth dimension mistake every bit WILDANIMALS is lastly class. You tin non extend it. world cast Cow extends WILDANIMALS{ }
This means nosotros tin confine cast extension likewise using lastly keyword.
Points to scream back close lastly class
- final keyword tin live used amongst variable, method or cast to brand them final.
- Local lastly variable must live initialized during annunciation or within constructor. Otherwise it volition present y'all an error.
- We tin non declare constructor every bit final. VIEW MORE close constructor inwards java.
- By default all variables declared inwards interface are implicitly final. VIEW MORE close interface inwards coffee software evolution language.
- Value of lastly variable tin non live changed.
- We tin non override lastly method.
- Class tin non live extended to lastly class. VIEW POST on inheritance to know to a greater extent than close cast extend.
- final variable which is non initialized during annunciation is called blank lastly variable in addition to y'all must require to initialize it inwards constructor.