-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalKeyword.java
More file actions
49 lines (41 loc) · 1.19 KB
/
FinalKeyword.java
File metadata and controls
49 lines (41 loc) · 1.19 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
// final variables -> can't change value
// final methods -> can't override; if inherited, child class will have access
// final classes -> can't extend
//can't finalize any abstract class/method
class Final {
final String name = "Abdur Rahman"; // to use 'final' : either initialize it
final String anotherName; //or access it only using constructor
Final(String anotherName) {
this.anotherName = anotherName;
}
void showName() {
//name = "Another One"; //not allowed
System.out.println(name);
System.out.println(anotherName);
}
final void finalMethod() { //can't override finalized methods
System.out.println("inside final method");
}
}
class Final2 extends Final {
Final2(String anotherName) {
super(anotherName);
}
//can't override finalized methods
//final void finalMethod() { System.out.println("overriding"); }
}
//final class can't be extended
final class FinalClass {
//
}
//class FinalClass2 extends FinalClass
//{
// //cannot extend final class.
//}
public class FinalKeyword {
public static void main(String[] args)
{
Final f = new Final("L Lawliet");
f.showName();
}
}