Classes and objects are fundamental concepts in object-oriented programming (OOP). Java is an object-oriented language, and understanding classes and objects is essential for writing Java programs. In this Core Java tutorial, we'll explore classes and objects in detail, along with comprehensive explanations and examples.
1. Classes:
A class is a blueprint or template for creating objects. It defines the structure and behavior of objects. In Java, a class is declared using the 'class' keyword.
'public class ClassName': Declares a class with the name 'ClassName'. The 'public' keyword makes the class accessible from other classes.
Fields: These are attributes or properties that define the state of objects created from the class.
Constructors: Special methods used to initialize objects of the class.
Methods: Functions that define the behavior of objects created from the class.
Example:
Let's create a simple class called 'Person' :
public class Person {
// Fields
String name;
int age;
// Constructor
public Person(String n, int a) {
name = n;
age = a;
}
// Method to display information
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
2. Objects:
An object is an instance of a class. It represents a real-world entity and has its own state and behavior. To create an object, you use the 'new' keyword followed by a constructor of the class.
Syntax:
ClassName objectName = new ClassName();
'ClassName': The name of the class.
'objectName': The name you give to the object.
'new': The keyword used to create an instance of the class.
'ClassName()': The constructor of the class used to initialize the object.
Example:
Let's create two 'Person' objects:
Person person1 = new Person("Alice", 25);
Person person2 = new Person("Bob", 30);
// Call methods on objects
person1.displayInfo();
person2.displayInfo();
In this example, we created two 'Person' objects, 'person1' and 'person2', and called the 'displayInfo' method on each object to display their information.
3. Constructors:
Constructors are special methods used to initialize objects when they are created. They have the same name as the class and do not have a return type.
Example:
public class Student {
String name;
int age;
// Constructor
public Student(String n, int a) {
name = n;
age = a;
}
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
In this example, the 'Student' class has a constructor that takes two parameters, 'name'and 'age', to initialize the object's state when it's created.
4. Methods:
Methods define the behavior of objects created from a class. They are functions that can perform actions and return values.
Example:
public class Circle {
double radius;
// Constructor
public Circle(double r) {
radius = r;
}
// Method to calculate area
public double calculateArea() {
return Math.PI * radius * radius;
}
}
In this example, the 'Circle'class has a method 'calculateArea'that calculates and returns the area of the circle.
Conclusion:
Classes and objects are fundamental concepts in Java and object-oriented programming. Classes define the structure and behavior of objects, while objects represent real-world entities with their own state and behavior. Understanding how to create classes, objects, constructors, and methods is essential for building Java applications. As you progress in your Java journey, you'll use classes and objects extensively to model and solve real-world problems.