Skip to main content

Command Palette

Search for a command to run...

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

Published
2 min read
Lab Sheet 10: Access Modifiers, Classes, and Objects in Java
A
I co-founded🫆digizen (fighting internet chaos 🧠), building an AI app to tackle misinformation, and running ideaGeek to turn “I have an idea” into real startups. I also share tech + travel on YouTube (TechNomad & Rz Omar). I touch grass too 🌱… but mostly to debug life 💻

Task 1: Understanding Access Modifiers

  1. Create a Java class called AccessDemo With 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)

  2. Create a second class AccessTester in:

    • The same package as AccessDemo

    • A different package from AccessDemo

  3. In AccessTester, try to access each variable from AccessDemo and document which accesses work and which don't, explaining why based on their access modifiers.

Task 2: Creating a Simple Class

  1. Create a class called BankAccount with:

    • Private instance variables: accountNumber (String), balance (double), accountHolderName (String)

    • Public methods: deposit(), withdraw(), checkBalance()

    • A constructor that initializes the account details

  2. 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

  1. Create a class called Student with:

    • Instance variables: studentID, name, grade

    • A class variable: totalStudents (to keep track of how many Student objects have been created)

    • Appropriate methods to get and set these values

  2. Test your class by:

    • Creating 3 Student objects

    • Printing the value of totalStudents after each creation

    • Demonstrating that each object maintains its own instance variables

Task 4: Object References

  1. Create a class called Book with:

    • Instance variables: title, author, price

    • Methods to display book information

  2. 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

  1. Create a base class Vehicle with:

    • Protected variables: make, model

    • Public method: displayInfo()

  2. Create a derived class Car that extends Vehicle with:

    • Additional private variable: numDoors

    • Method to display all information (including inherited protected variables)

  3. 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

More from this blog

T

TechNomad

46 posts

TechNomad is mostly coding exercises, not long boring explanations 😌. I share what I teach and learn through hands-on problems—mainly for my students, but anyone can jump in. If you learn by doing (and breaking things), you’ll fit right in. Less theory, more “try this and see what happens.”