Classes — OOPs in Java
Object Oriented Programming — 1
A Class is a fundamental building block of object-oriented programming. It serves as a blueprint or template that defines the properties (also known as attributes or fields) and behaviors (also known as methods) that objects of that class can have.
A class provides a way to encapsulate related data and functionality into a single unit. It defines the structure and behavior of objects that can be created from it. Objects are instances of a class, and each object has its own state (values of its attributes) and behavior (implementation of its methods).
The different components that can be defined within a class:
- Class Declaration: The class declaration specifies the name of the class and any inheritance or interfaces it extends or implements, respectively.
- Instance Variables: These are the attributes or data members of a class. They represent the state or characteristics that each object of the class can have. Each object created from the class will have its own set of instance variables.
- Constructors: Constructors are special methods used to initialize objects of a class. They are invoked when an object is created using the
new
keyword and are responsible for setting initial values to the instance variables. - Methods: Methods define the behavior or actions that objects of the class can perform. They encapsulate reusable code that can be executed when a method is called. Methods can take parameters and can return values.
- Access Modifiers: Access modifiers control the visibility and accessibility of class members (variables and methods). The main access modifiers in Java are
public
,private
,protected
, and the default (no modifier). - Static Members: Static members, such as static variables and static methods, belong to the class itself rather than individual objects. They are shared among all objects of the class and can be accessed directly using the class name.
- Inner Classes: Java allows defining classes within other classes. These are called inner classes or nested classes. Inner classes have access to the members of the enclosing class and can provide better organization and encapsulation.
By creating and instantiating objects from a class, you can utilize the defined attributes and methods to perform specific actions and manipulate data. Classes provide a way to structure code, promote code reuse, and build complex systems through the use of objects and their interactions.
Syntax:
[access modifier] class ClassName {
// instance variables (attributes)
[access modifier] dataType variableName1;
[access modifier] dataType variableName2;
// ...
// constructors
[access modifier] ClassName([parameters]) {
// constructor body
}
// methods
[access modifier] returnType methodName1([parameters]) {
// method body
}
[access modifier] returnType methodName2([parameters]) {
// method body
}
// ...
}
[access modifier]
: An optional access modifier that determines the visibility and accessibility of the class. Common access modifiers arepublic
,private
, andprotected
, or no modifier (default).class
: The keyword used to declare a class.ClassName
: The name of the class. It follows the same naming conventions as variables (e.g., starts with a letter, no spaces, etc.).instance variables
: These are the attributes or data members of a class. They represent the state or characteristics of objects created from the class.[access modifier]
: An optional access modifier that determines the visibility and accessibility of the instance variables.dataType
: The type of data the instance variable can hold (e.g.,int
,String
, etc.).variableName
: The name that is given to the instance variable. It follows the same naming conventions as variables.constructors
: These special methods are used to initialize objects of the class. They have the same name as the class and do not have a return type.[access modifier]
: An optional access modifier that determines the visibility and accessibility of the constructor.parameters
: Optional input values that can be passed to the constructor.constructor body
: The code block inside the constructor that is executed when an object is created. It is responsible for initializing the instance variables.methods
: These are functions defined within the class that define the behavior or actions that objects of the class can perform.[access modifier]
: An optional access modifier that determines the visibility and accessibility of the method.returnType
: The data type of the value the method returns (void
if it doesn't return anything).methodName
: The name that is given to the method. It follows the same naming conventions as variables.parameters
: Optional input values that can be passed to the method.method body
: The code block inside the method that is executed when the method is called. It contains the logic and operations to be performed by the method.
Example:
Below is an example that demonstrates the concept of a class named Person
with instance variables, a constructor, a method, and getter methods.
// Class declaration
public class Person {
// Instance variables
private String name;
private int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Method
public void sayHello() {
System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
}
// Getter methods
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
In this example, we have a class named Person
. It has two instance variables (name
and age
), a constructor, a method (sayHello()
), and getter methods for accessing the instance variables.
The constructor Person(String name, int age)
initializes the name
and age
variables with the values passed as parameters. The method sayHello()
prints a greeting message using the instance variables.
To create objects and use the Person
class, you can do the following:
public class Main {
public static void main(String[] args) {
// Create an object of the Person class
Person person1 = new Person("John", 25);
// Call the sayHello() method on the object
person1.sayHello();
// Access the instance variables using the getter methods
String name = person1.getName();
int age = person1.getAge();
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
When you run this program, it will output:
Hello, my name is John and I am 25 years old.
Name: John
Age: 25
This demonstrates the basic syntax and usage of a class in Java. You can create multiple objects of the class, each with its own set of instance variables, and invoke methods on those objects to perform specific actions.
Good Luck!