Day 4 - 27-05-2025 - Solving Assignment Questions - Encapsulation Example, AccessModifier example,

// Give the real time example of encapsulation with code.

//Create a class Student with 4 variables called as rollNo, admissionNo, age,

//courseId.Each of the variables should have one of the access modifier -

//public, protected, no-access-modifier and private.Add 4 methods in the

//class – public, method doPublic, no access modifier method doDefault,

//protected method doProtected, private method doPrivate.


//a. In main method outside the class but in same package – create object of

//type Student.

//b. Try to access rollno outside the class.

//c. Try to access age outside the package.

//d. Also try to access private methods outside the class and protected

//methods outside the package.






Learnings :


Modifier Same Class Same Package Subclass (same pkg) Subclass (other pkg) Other Class (other pkg)
public
protected (via subclass)
(default)
private





Comments

Popular posts from this blog

17- 6-25 | Fibonacci Series | While(true) is infinite loop untill we break. | InputMismatchException in scanner object | sc.next in catch block.