In Java, classes and objects are like the building blocks of your program. They help you organize your code and represent real-world things. In this tutorial, we'll break down what classes and objects are in Java.
Classes:
Imagine a class as a blueprint or a template.
It defines the structure or characteristics of an object.
Just like a blueprint for a house tells you how to build a house, a class tells you how to create an object.
Objects:
Think of an object as something real and tangible.
It's like a specific instance of a class.
For example, if the class is "Car," an object could be "MyCar," which has its own unique features like color, model, and speed.
Creating a Class:
To create a class, use the keyword class
Example
class CarBlueprint {
String make;
String model;
int year;
}
CarBlueprint myCar = new CarBlueprint();
Creating Objects from Classes:
To create an object for a class , specify the class name, followed by the object name, and use the keyword new
Each object is unique, like individual cars on the road.
Example
CarBlueprint car1 = new CarBlueprint();
CarBlueprint car2 = new CarBlueprint();
Object Properties (Attributes):
Objects have characteristics called attributes.
These attributes are like the details of a car, such as make, model, and year.
class CarBlueprint {
void start() {
System.out.println("Car is starting...");
}
}
CarBlueprint myCar = new CarBlueprint();
myCar.start(); // Output: Car is starting...
Accessing Object Attributes:
To access an object's attributes, use the dot notation (object.attribute).
It's like reading a book's title from its cover.
Example
class Book {
String title;
}
Book myBook = new Book();
myBook.title = "Java Programming";
// Accessing attribute
String bookTitle = myBook.title;
System.out.println("Title: " + bookTitle); // Output: Title: Java Programming
Modifying Object Attributes:
You can change an object's attribute by assigning a new value using the dot notation.
It's like updating a book's title on its cover.
Example
class Book {
String title;
}
Book myBook = new Book();
myBook.title = "Java Programming";
// Modifying attribute
myBook.title = "Java Mastery";
System.out.println("New Title: " + myBook.title); // Output: New Title: Java Mastery
Invoking Object Methods:
To use an object's method, call it using the dot notation (object.method()).
It's like following a recipe to cook a dish.
Example
class Calculator {
int add(int a, int b) {
return a + b;
}
}
Calculator myCalculator = new Calculator();
// Invoking method
int sum = myCalculator.add(5, 7);
System.out.println("Sum: " + sum); // Output: Sum: 12
Modifying Object Methods:
You can't directly modify object methods, but you can extend or override them in a subclass.
t's like customizing a recipe to create a unique dish.
Example
class Bird {
void sing() {
System.out.println("Bird is singing...");
}
}
class Parrot extends Bird {
void sing() {
System.out.println("Parrot is singing loudly!");
}
}
Bird myBird = new Parrot();
// Invoking method
myBird.sing(); // Output: Parrot is singing loudly!