|
| 1 | +package JavaEntry; |
| 2 | + |
| 3 | +// public: The class is accessible by any other class. |
| 4 | +// default: The class is only accessible by classes in the same package. |
| 5 | +// private: The code is only accessible within the declared class. |
| 6 | +// protected: The code is accessible in the same package and subclasses. |
| 7 | +// final: The class cannot be inherited by other classes |
| 8 | +// abstract: The class cannot be used to create objects (To access an abstract class, it must be inherited from another class). |
| 9 | + |
| 10 | + |
| 11 | +abstract class AbstractClass { |
| 12 | + String absAtr = "Abastract Attribute"; |
| 13 | + short year = 2025; |
| 14 | + abstract void absmethod(); |
| 15 | +} |
| 16 | + |
| 17 | +class ForAbstractUse extends AbstractClass { |
| 18 | + short day = 5; |
| 19 | + void absmethod(){ |
| 20 | + System.out.println("This is an abstract method"); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +public class ClassModifiersEx12 { |
| 25 | + final int x = 15; |
| 26 | + private String fullName = "Test"; // We can access the private attribute in the same class object |
| 27 | + |
| 28 | + private String getName(){ // We can access the private Getter Method in the same class object |
| 29 | + return fullName; |
| 30 | + } |
| 31 | + |
| 32 | + public void setName(String newName){ // Setter Method |
| 33 | + this.fullName = newName; |
| 34 | + } |
| 35 | + |
| 36 | + static String staticMethod(){ |
| 37 | + return "I am static method, and will call without creating object."; |
| 38 | + } |
| 39 | + |
| 40 | + public String publicMethod(){ |
| 41 | + return "I am public method, will call with object only"; |
| 42 | + } |
| 43 | + |
| 44 | + public static void main(String[] args){ |
| 45 | + // System.out.println(x); // we can't access the class attribute directly, can access with creating object. |
| 46 | + |
| 47 | + ClassModifiersEx12 modifierObj = new ClassModifiersEx12(); |
| 48 | + // modifierObj.x = 40; // we can oberride value to a final varaiable. |
| 49 | + System.out.println(modifierObj.x); // we can access the class attribute with creating object. |
| 50 | + System.out.println(modifierObj.fullName); |
| 51 | + modifierObj.setName("Banti Shaw"); |
| 52 | + System.out.println(modifierObj.getName()); |
| 53 | + |
| 54 | + ClassModifiersEncapEx12 encapObj = new ClassModifiersEncapEx12(); |
| 55 | + // System.out.println(encapObj.fullName); // we can't access the private attribute directly |
| 56 | + encapObj.setName("Java Excapsulation"); |
| 57 | + System.out.println(encapObj.getName()); |
| 58 | + |
| 59 | + |
| 60 | + System.out.println(staticMethod()); |
| 61 | + System.out.println(modifierObj.publicMethod()); |
| 62 | + |
| 63 | + //Using the abstract class |
| 64 | + ForAbstractUse absObj = new ForAbstractUse(); |
| 65 | + System.out.println(absObj.absAtr); |
| 66 | + System.out.println(absObj.day); |
| 67 | + absObj.absmethod(); |
| 68 | + |
| 69 | + } |
| 70 | +} |
0 commit comments