-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx12ClassModifiers.java
More file actions
70 lines (54 loc) · 2.51 KB
/
Ex12ClassModifiers.java
File metadata and controls
70 lines (54 loc) · 2.51 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
61
62
63
64
65
66
67
68
69
70
package JavaEntry;
// public: The class is accessible by any other class.
// default: The class is only accessible by classes in the same package.
// private: The code is only accessible within the declared class.
// protected: The code is accessible in the same package and subclasses.
// final: The class cannot be inherited by other classes
// abstract: The class cannot be used to create objects (To access an abstract class, it must be inherited from another class).
abstract class AbstractClass {
String absAtr = "Abastract Attribute";
short year = 2025;
abstract void absmethod();
}
class ForAbstractUse extends AbstractClass { // Inherit the abstract class with the extends keyword
short day = 5;
void absmethod(){
System.out.println("This is an abstract method");
}
}
public class Ex12ClassModifiers {
final int x = 15;
private String fullName = "Test"; // We can access the private attribute in the same class object
private String getName(){ // We can access the private Getter Method in the same class object
return fullName;
}
public void setName(String newName){ // Setter Method
this.fullName = newName;
}
static String staticMethod(){
return "I am static method, and will call without creating object.";
}
public String publicMethod(){
return "I am public method, will call with object only";
}
public static void main(String[] args){
// System.out.println(x); // we can't access the class attribute directly, can access with creating object.
Ex12ClassModifiers modifierObj = new Ex12ClassModifiers();
// modifierObj.x = 40; // we can oberride value to a final varaiable.
System.out.println(modifierObj.x); // we can access the class attribute with creating object.
System.out.println(modifierObj.fullName);
modifierObj.setName("Banti Shaw");
System.out.println(modifierObj.getName());
Ex12ClassModifiersEncap encapObj = new Ex12ClassModifiersEncap();
// System.out.println(encapObj.fullName); // we can't access the private attribute directly
encapObj.setName("Java Excapsulation");
System.out.println(encapObj.getName());
System.out.println(staticMethod());
System.out.println(modifierObj.publicMethod());
//Using the abstract class
ForAbstractUse absObj = new ForAbstractUse();
System.out.println(absObj.absAtr);
System.out.println(absObj.day);
absObj.absmethod();
}
}