🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Java, a class is a blueprint or template that defines the structure and behavior (attributes and methods) that the objects created from the class can have. A class encapsulates data for the object and methods to manipulate that data. It serves as the fundamental building block in object-oriented programming (OOP) in Java.
What is a Class?
A class in Java is a user-defined data type that serves as a blueprint for creating objects. It defines the attributes (data fields) and behaviors (methods) that the objects created from the class can possess.
Key Points:
- A class is a blueprint for objects.
- It defines attributes and methods.
- Objects are instances of a class.
Syntax of a Class
The basic syntax to define a class in Java is as follows:
class ClassName {
// Attributes (data fields)
dataType attributeName;
// Constructor
public ClassName(parameters) {
// Initialize attributes
}
// Methods
returnType methodName(parameters) {
// Method body
}
}
Example: Defining and Using a Class
Let's consider a real-world example: a Car class. We will define a Car class with attributes such as color, model, and speed, and methods such as start(), accelerate(), and brake(). Then, we will create an instance of the Car class and use it.
Step 1: Define the Car Class
public class Car {
// Attributes (state)
private String color;
private String model;
private int speed;
// Constructor
public Car(String color, String model) {
this.color = color;
this.model = model;
this.speed = 0; // Initially, the car is stationary
}
// Methods (behavior)
public void start() {
System.out.println(model + " is starting.");
speed = 10; // Starting speed
}
public void accelerate(int increment) {
speed += increment;
System.out.println(model + " is accelerating. Speed: " + speed + " km/h");
}
public void brake() {
speed = 0;
System.out.println(model + " has stopped.");
}
// Getters
public String getColor() {
return color;
}
public String getModel() {
return model;
}
public int getSpeed() {
return speed;
}
}
Step 2: Create and Use an Instance of the Car Class
public class Main {
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car("Red", "Toyota Corolla");
// Using the object
myCar.start();
myCar.accelerate(20);
myCar.brake();
// Accessing the object's attributes
System.out.println("Car Model: " + myCar.getModel());
System.out.println("Car Color: " + myCar.getColor());
System.out.println("Car Speed: " + myCar.getSpeed() + " km/h");
}
}
Explanation:
-
Defining the Car Class:
- The
Carclass has three attributes:color,model, andspeed. - It has a constructor to initialize the
colorandmodelattributes, and thespeedis initially set to 0. - The class has three methods:
start(),accelerate(int increment), andbrake(), representing the behavior of the car. - The class also includes getter methods to access the attributes of the car.
- The
-
Creating and Using an Object:
- In the
Mainclass, we create an instance of theCarclass using thenewkeyword. - We then call the methods
start(),accelerate(int increment), andbrake()on themyCarobject to simulate the car's behavior. - We access the attributes of the
myCarobject using the getter methods and print their values.
- In the
Text-based Diagram
Class: Car
+---------------------------+
| Car |
+---------------------------+
| - color: String |
| - model: String |
| - speed: int |
+---------------------------+
| + Car(color, model) |
| + start(): void |
| + accelerate(int): void |
| + brake(): void |
| + getColor(): String |
| + getModel(): String |
| + getSpeed(): int |
+---------------------------+
Object: myCar
+---------------------------+
| myCar |
+---------------------------+
| - color: "Red" |
| - model: "Toyota Corolla" |
| - speed: 0 |
+---------------------------+
| + start() |
| + accelerate(int) |
| + brake() |
| + getColor() |
| + getModel() |
| + getSpeed() |
+---------------------------+
Conclusion
In Java, a class is a blueprint for creating objects. It defines the attributes and methods that the objects created from the class can possess. By using classes and objects, we can model real-world entities and their interactions, making our code more modular, reusable, and maintainable.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment