Java is an object-oriented programming language that offers robust support for classes and objects. A class in Java is a blueprint for creating objects that bundle data (attributes) and behaviors (methods). Now, let's explore the essentials of classes, objects, and access modifiers in Java.
Defining a Class in Java:
In Java, a class is defined using the keyword "class", followed by the class name. Inside the class, we can declare attributes (fields) and methods that operate on these attributes.
class Person
{
// Attributes
String name;
int age;
// Method to introduce the person
void introduce()
{
System.out.println("Hi, my name is " + name + " and I am " + age + " years old.");
}
}
Creating Objects and Accessing Members:
class Main
{
public static void main(String[] args)
{
// Creating objects of the Person class
Person person1 = new Person();
Person person2 = new Person();
// Setting the attributes of person1
person1.name = "Alice";
person1.age = 25;
// Setting the attributes of person2
person2.name = "Bob";
person2.age = 30;
// Using methods to introduce the persons
person1.introduce();
person2.introduce();
}
}
Access Specifiers:
1. `public`:
2. `private`:
3. `protected`:
3. `default (no modifier)`:
class MyClass
{
public int publicVar; // Public attribute
private int privateVar; // Private attribute
protected int protectedVar; // Protected attribute
int defaultVar; // Default attribute
// Methods can have any access modifier
public void publicFunction()
{
System.out.println("PublicFunction: I can access all attributes.");
}
private void privateFunction()
{
System.out.println("PrivateFunction: I can only access the privateVar.");
}
protected void protectedFunction()
{
System.out.println("ProtectedFunction: I can access privateVar and protectedVar.");
}
void defaultFunction()
{
System.out.println("DefaultFunction: I can access all attributes within the same package.");
}
}
NOTE: In a class, the default access specifier for its members is "default" (no modifier). This means that if you don't specify an access specifier for a member, it will be considered default by default. (alert-success)
Class Methods:
Class methods, also known as member functions, are functions defined within a class that operate on the class's attributes.
Classification of Member Methods:
In Java, methods can be classified based on their purpose and behavior. Three common types of methods are accessor methods (getter methods), mutator methods (setter methods), and utility methods.
1. Accessor or Getter Methods:
- Accessor methods are used to retrieve the values of private attributes of a class.
- Accessor methods provide read-only access to the private attributes, allowing other parts of the program to obtain the values.
- Typically, accessor methods have a return type that matches the attributes they retrieve.
Example:
class Rectangle
{
private double length;
private double width;
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
}
In the above example, `getLength()` and `getWidth()` are accessor methods that allow retrieving the values of the private attributes `length` and `width`, respectively.
2. Mutator or Setter Methods:
- Mutator methods are used to modify the values of private attributes of a class.
- They provide a way to update the internal state of an object by setting new values for the private attributes.
- Mutator methods do not return a value (or return `void`), but they typically take parameters to update the attributes.
Example:
class Rectangle
{
private double length;
private double width;
public void setLength(double Length)
{
length = Length;
}
public void setWidth(double Width)
{
width = Width;
}
}
In the above example, `setLength()` and `setWidth()` are mutator methods that allow setting new values for the private attributes `length` and `width`, respectively.
3. Utility Methods:
- Utility methods perform specific operations related to the class but may not directly manipulate the class's attributes.
- They can be used for calculations, conversions, or any other functionality associated with the class.
Example:
class MathOperations
{
public int Square(int number)
{
return number * number;
}
}
In the above example, `Square()` is a utility method defined as a static member method. It calculates the square of a given number, but it does not require any access to the object's attributes.