Welcome to The Coding College! In this article, we’ll dive into the fundamentals of classes and objects in Java, which are the building blocks of Object-Oriented Programming (OOP). Understanding these concepts will enable you to model real-world scenarios effectively in your Java programs.
What Are Classes and Objects?
Class
- A class is a blueprint for creating objects.
- It defines attributes (fields) and methods that describe the properties and behavior of an object.
Object
- An object is an instance of a class.
- It represents a specific entity with attributes and behaviors defined by its class.
Syntax for Classes and Objects
Defining a Class
class ClassName {
// Attributes (fields)
String attribute1;
int attribute2;
// Methods
void methodName() {
// Method logic
}
}
Creating an Object
ClassName objectName = new ClassName();
Example: Defining and Using Classes and Objects
Here’s a simple example to illustrate classes and objects:
// Defining a class
class Person {
// Attributes
String name;
int age;
// Method
void introduce() {
System.out.println("Hi, my name is " + name + " and I am " + age + " years old.");
}
}
// Main class
public class Main {
public static void main(String[] args) {
// Creating an object of the Person class
Person person1 = new Person();
// Setting attributes
person1.name = "Alice";
person1.age = 25;
// Calling a method
person1.introduce();
}
}
Output:
Hi, my name is Alice and I am 25 years old.
Explanation
1. Attributes (Fields)
Attributes are variables that hold the state of an object.
String name; // Attribute for storing the person's name
int age; // Attribute for storing the person's age
2. Methods
Methods define the behavior of a class.
void introduce() {
System.out.println("Hi, my name is " + name + " and I am " + age + " years old.");
}
3. Object Instantiation
An object is created using the new
keyword.
Person person1 = new Person();
4. Accessing Attributes and Methods
Attributes and methods are accessed using the .
operator.
person1.name = "Alice"; // Setting the name attribute
person1.introduce(); // Calling the introduce method
Constructors in Classes
Constructors initialize objects when they are created. They have the same name as the class and no return type.
Example: Using Constructors
class Car {
String brand;
int year;
// Constructor
public Car(String brand, int year) {
this.brand = brand;
this.year = year;
}
void displayInfo() {
System.out.println("Car: " + brand + " (" + year + ")");
}
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car("Toyota", 2020);
car1.displayInfo();
}
}
Output:
Car: Toyota (2020)
Key Features of Classes and Objects
- Encapsulation: Protects data by bundling it with methods.
- Reusability: Code written in classes can be reused across programs.
- Modularity: Classes make code more organized and modular.
- Abstraction: Hides complex implementation details.
Practice Problems
- Create a
Student
class with attributes for name, roll number, and marks, and a method to display details. - Implement a
Book
class with attributes for title, author, and price, and methods to set and get these details. - Design a
Circle
class with methods to calculate area and circumference.
For more hands-on examples and programming tips, visit The Coding College. Stay tuned for more exciting tutorials!