0% found this document useful (0 votes)
17 views4 pages

Lab 3

Object Oriented Programming Lab Sheets

Uploaded by

f20220584
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Lab 3

Object Oriented Programming Lab Sheets

Uploaded by

f20220584
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 2:Static Keyword and Access Specifiers

Static keyword:
There will be times when you will want to define a class member that will be used independently
of any object of that class. Normally, a class member must be accessed only in conjunction with
an object of its class. However, it is possible to create a member that can be used by itself
without reference to a specific instance. To create such a member, precede its declaration with
the keyword static. When a member is declared static, it can be accessed before any objects of its
class are created and without reference to any object.

We can apply static keywords with variables, methods, blocks and nested classes. The static
keyword belongs to the class rather than an instance of the class.

Static variable: If you declare any variable as static, it is known as a static variable. All the
objects will share the variable.

Eg1: Create a Java class to demonstrate the use of a static


variable.

class Counter {
static int count = 0;

Counter() {
count++;
System.out.println("Count: " + count);
}

public static void main(String[] args) {


Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
}
}

Static Methods: With static methods, we do not need an instance of a class to call them. We can
call them directly or using the class name.
Eg: public class MathOperations {
static int add(int a, int b) {
return a + b;
}

public static void main(String[] args) {


int result = MathOperations.add(5, 10);

// Int result = add(5,10)


System.out.println("Sum: " + result);
}
}

Static Block: we can initialize static fields or execute necessary operations at class loading time,
ensuring efficiency and avoiding redundant initialization during the program's runtime.

class StaticBlockDemo {
static int configValue;

static {
configValue = 100;
System.out.println("Static block executed. Config Value:
" + configValue);
}

public static void main(String[] args) {


System.out.println("Main method executed.");
}
}
Access specifiers: Access modifiers help to restrict the scope of a class, constructor, variable,
method, or data member. It provides security, accessibility, etc, to the user depending upon the
access modifier used with the element.

Package

A package in Java is used to group related classes. Think of it as a folder in a file directory.

We use packages to avoid name conflicts, and to write a better maintainable code.

Example: import java.util.Scanner;

In the example above, java.util is a package,

while Scanner is a class of the java.util package

Types of access specifiers:

1. Default: When no access modifier is specified for a class, method, or data member – It is
said to have the default access modifier by default. The data members, classes, or
methods that are not declared using any access modifiers i.e. having default access
modifiers, are accessible only within the same package.

2. Private: The private access modifier is specified using the keyword private. The methods
or data members declared as private are accessible only within the class in which they are
declared. Any other class of the same package will not be able to access these members.

3. Protected: The protected access modifier is specified using the keyword protected. The
methods or data members declared as protected are accessible within the same package or
subclasses in different packages.

4. Public : The public access modifier is specified using the keyword public. The public
access modifier has the widest scope among all other access modifiers. Classes, methods,
or data members that are declared as public are accessible from everywhere in the
program. There is no restriction on the scope of public data members.
Exercise 1: Implementing static variables
a. Create a class Student with a static variable totalStudents to keep track of the total
number of students.
b. Create a constructor that increments totalStudents whenever a new Student object
is created.
c. Create another class TestStatic with a main method to create three Student objects
and display the total number of students.

Exercise 2: Create a class, Calculator using static methods


a. Create a class Calculator with static methods add, subtract, multiply, and divide.
b. Each method should take two integers as parameters and return the result.
c. In the main method, call these static methods directly from the class without
creating an instance.

Exercise 3: Create a class `BankAccount` with private variables `accountNumber` and `balance`.
Provide public methods to get the balance and deposit money. Demonstrate the use of
encapsulation by accessing these methods from another class.

Exercise 4: Design a Product class with private attributes: productName, price, and quantity.
Provide public methods to:
a. Get the product details.
b. Update the quantity (ensure the quantity is non-negative).
c. Calculate the total cost for the product (price * quantity).
Implement a ShoppingCart class that holds multiple Product objects and provides methods to add
products, remove products, and calculate the total cost of the cart.

You might also like