Lab Sheet 10: Access Modifiers, Classes, and Objects in Java

Task 1: Understanding Access Modifiers
Create a Java class called
AccessDemoWith the following:One default access variable
One public variable
One protected variable
One private variable
Methods to demonstrate access to each variable (some accessible, some not)
Create a second class
AccessTesterin:The same package as
AccessDemoA different package from
AccessDemo
In
AccessTester, try to access each variable fromAccessDemoand document which accesses work and which don't, explaining why based on their access modifiers.
Task 2: Creating a Simple Class
Create a class called
BankAccountwith:Private instance variables:
accountNumber(String),balance(double),accountHolderName(String)Public methods:
deposit(),withdraw(),checkBalance()A constructor that initializes the account details
Test your class by:
Creating two BankAccount objects with different initial values
Performing deposit and withdrawal operations
Checking balances after each operation
Task 3: Instance vs Class Variables
Create a class called
Studentwith:Instance variables:
studentID,name,gradeA class variable:
totalStudents(to keep track of how many Student objects have been created)Appropriate methods to get and set these values
Test your class by:
Creating 3 Student objects
Printing the value of
totalStudentsafter each creationDemonstrating that each object maintains its own instance variables
Task 4: Object References
Create a class called
Bookwith:Instance variables:
title,author,priceMethods to display book information
Demonstrate object references by:
Creating one Book object
Creating two references to the same Book object
Modifying the book through one reference and showing the change is visible through the other reference
Then creating a new Book object and assigning one reference to it
Task 5: Protected Access Modifier
Create a base class
Vehiclewith:Protected variables:
make,modelPublic method:
displayInfo()
Create a derived class
Carthat extendsVehiclewith:Additional private variable:
numDoorsMethod to display all information (including inherited protected variables)
Test your classes by:
Creating a Vehicle object and trying to access its protected members directly
Creating a Car object and showing it can access the protected members of Vehicle




