Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added concept of final keyword #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions java/more/final-keyword.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
## Final Keyword in JAVA

Final keyword in java is a non-access modifier. It is used to restrict user from making any change in variable, method and classes.

### 1. Final Variable
If any variable is declared with keyword final, then the value of final variable can't be change, i.e. it becomes constant.

### Example

```java
class Student{
//final variable
final int id=50;
void get_id(){
id=100;
}
public static void main(String[] args){
Student obj=new Student();
obj.get_id();
}
}
```

### 2. Final Method
If any method is declared with keyword final, then it can't be overridden.

### Example

```java
class Fruit{
//final method
final void run(){
System.out.println("Inside Fruit Class");
}
}
class Apple extends Fruit{
void run(){
System.out.println("Inside Apple Class");
}
public static void main(String[] args){
Apple obj=new Apple();
obj.run();
}
}
```

### 3. Final Class
If any class is declared with keyword final, then it can't be inherited.

### Example

```java
//final class
final class Fruit{
}
class Apple extends Fruit{
void run(){
System.out.println("Inside Apple Class");
}
public static void main(String[] args){
Apple obj=new Apple();
obj.run();
}
}
```