-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalKeyword.java
More file actions
60 lines (47 loc) · 1.73 KB
/
FinalKeyword.java
File metadata and controls
60 lines (47 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
We can implement final keyword in :
1. Class= If we use final keyword before class name than that class cannot be inherited by any class
2. Method=If we use final keyword with method than that method cannot be Override by any other method
3. Variable= If we use final keyword before variable name than we cannot make changes in that variable means if
ex:- final int a=10;
than we cannot change the value of a.. it is final(We cannot make empty final variable we must declare it with value)
*/
// Final Variable
public class FinalKeyword {
final int a = 10;
a = 20;
// We cannot change the value of final variable
public static void main(String[] args) {
FinalKeyword f = new FinalKeyword();
//f.a=20;
}
}
// Final Class
final class Parent {
String name;
int age;
public void show() {
System.out.println("Hello");
}
}
public class FinalKeyword extends Parent {
// we cannot inherit the final class
public static void main(String[] args) {
}
}
// Final Method
class Parent {
public final void show() {
System.out.println("Parent");
}
}
public class FinalKeyword extends Parent {
public void show() {
// we cannot override the final method
System.out.println("FinalKeyword");
}
public static void main(String[] args) {
FinalKeyword f = new FinalKeyword();
f.show();
}
}