-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFinalExample.java
56 lines (46 loc) · 1.19 KB
/
FinalExample.java
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
package company;
/*
* If we write class Animal as final, it cannot be extended
* final class Animal
*/
class Animal {
private String name;
private String colour;
/* If we write eat() method as final, it cannot be overridden.
* public final void eat()
*/
public void eat() {
System.out.println("munch munch munch");
}
public void setClour(String colour) {
this.colour = colour;
}
public String getClour() {
return colour;
}
}
class Dog extends Animal {
@Override
public void eat() {
System.out.println("nom nom nom " +FinalExample.PI);
}
public void bark() {
System.out.println("woof woof woof");
}
}
public class FinalExample {
public static final double PI = 3.14159;
public static void main(String[] args) {
/* If we write the object below as final, it cannot be created multiple times.
* final Dog myDog;
* myDog = new Dog();
*/
final Dog myDog;
myDog = new Dog();
// myDog = new Dog(); --> not allowed, as it has been declared final.
myDog.eat();
// If a constant is initiated final, it cannot be changed.
final int a = 9;
// a = 4; --> not allowed, as it has been initiated final.
}
}