In Java, the concept of classes and objects forms the foundation of Object-Oriented Programming (OOP). These concepts help in modeling real-world entities and organizing code in a logical, maintainable, and reusable way.
Class
A class in Java can be thought of as a blueprint or template for creating objects. It defines the properties and behaviors that the objects created from the class will have. In simpler terms, a class is like a blueprint for a house, while the objects are the actual houses built from that blueprint.
A class can have:
- Fields (Variables): These are the attributes or properties that represent the state of the object.
- Methods: These are the actions or behaviors that an object can perform.
- Constructors: Special methods used for initializing objects.
- Access Modifiers: These define the visibility or scope of classes, methods, and variables.
Syntax
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class Car { // Fields (Variables) String make; String model; int year; // Constructor public Car(String make, String model, int year) { this.make = make; this.model = model; this.year = year; } // Method (Behavior) public void displayCarInfo() { System.out.println("Car: " + make + " " + model + " (" + year + ")"); } } |
In the above example:
- The class Car defines three properties:
make
,model
, andyear
. - It has a constructor that initializes these properties when an object is created.
- It also defines a method called
displayCarInfo
that prints out the car’s details.
Object
An object in Java is an instance of a class. When a class is defined, no memory is allocated until an object is created. Objects are created using the new
keyword, and they hold specific values for the fields defined in the class.
In the above example, an object of the Car
class would be an actual car, such as a “Toyota Corolla 2020”.
Syntax
To create an object from a class, you use the new
keyword followed by the constructor of the class:
1 2 3 4 5 6 7 8 9 |
public class Main { public static void main(String[] args) { // Create an object of the Car class Car myCar = new Car("Toyota", "Corolla", 2020); // Calling the method using the object myCar.displayCarInfo(); } } |
In this case, myCar
is an object of the Car
class, and we call the displayCarInfo()
method to print out its details.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// Class definition public class Book { // Fields (Attributes) String title; String author; int yearPublished; // Constructor to initialize the Book object public Book(String title, String author, int yearPublished) { this.title = title; this.author = author; this.yearPublished = yearPublished; } // Method to display the book information public void displayBookInfo() { System.out.println("Book: " + title + " by " + author + " (" + yearPublished + ")"); } } // Main class to create and use Book objects public class Main { public static void main(String[] args) { // Creating Book objects (instances of the Book class) Book book1 = new Book("The Catcher in the Rye", "J.D. Salinger", 1951); Book book2 = new Book("To Kill a Mockingbird", "Harper Lee", 1960); Book book3 = new Book("1984", "George Orwell", 1949); // Calling methods on the Book objects book1.displayBookInfo(); // Outputs: Book: The Catcher in the Rye by J.D. Salinger (1951) book2.displayBookInfo(); // Outputs: Book: To Kill a Mockingbird by Harper Lee (1960) book3.displayBookInfo(); // Outputs: Book: 1984 by George Orwell (1949) } } |
Output
1 2 3 |
Book: The Catcher in the Rye by J.D. Salinger (1951) Book: To Kill a Mockingbird by Harper Lee (1960) Book: 1984 by George Orwell (1949) |
In this example:
- The class (
Book
) is the template that defines the properties and behaviors of books. - The objects (
book1
,book2
,book3
) are instances of theBook
class, each representing a specific book with its unique data. - Methods like
displayBookInfo()
act on these objects, performing actions based on their data.
Differences Between Classes and Objects
Aspect | Class | Object |
---|---|---|
Definition | Blueprint or template for creating objects | Instance or concrete representation of a class |
Memory | No memory is allocated | Memory is allocated when created |
Creation | Defined using the class keyword |
Created using the new keyword |
Purpose | Defines properties and behaviors | Represents real-world entities with specific values |