Oops in java
In Java, a class is a blueprint or template that defines the properties (attributes) and behavior (methods) of objects. It's like a cookie cutter that you use to create similar cookies. The class itself doesn't hold any data or perform any actions, but it serves as a specification for creating objects that do.
Key aspects of class in Java
- Blueprint: A class defines the characteristics of its objects. These characteristics include variables (to store data) and methods (to define actions).
- Objects: An object is an instance of a class. It's like a single cookie created from the cookie cutter. Objects hold their own data and can execute the methods defined in the class.
- Properties: These are also known as attributes or member variables. They represent the data or state of an object. For example, a Car class might have properties like color, weight, and speed.
- Behavior: These are defined by methods, which are functions associated with the class. Methods define the actions that objects can perform. Following the car example, a Car class might have methods like accelerate(), brake(), and turn().
Imagine a class named Dog. This class would define the general characteristics of dogs, like breed, color, and age (properties). It might also have methods like bark(), wagTail(), and playFetch() (to define the behavior of dogs). Individual dogs (like your pet Fido) would be objects created from the Dog class. Each dog would have its own specific fur color, breed, and age (data stored in the object's properties).
By using classes, you can create multiple objects with similar characteristics and behaviors, promoting code reusability and organization in your Java programs.
Getting Started with OOP in Java:
- Define a Class: Use the
class
keyword followed by the class name to define a class. Inside the class, you declare its properties (using variables) and methods (using functions). - Create Objects: Use the
new
keyword followed by the class name to create an object of that class. You can then access the object's properties and methods. - Inheritance: Use the
extends
keyword to create a subclass that inherits properties and methods from a superclass.
Example: Let's Build a Car!
public class Car {
// Properties (data)
private String color;
private int speed;
// Methods (actions)
public void accelerate() {
speed++;
}
public void brake() {
speed--;
}
// Getter and setter methods (controlled access)
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Create an object of the Car class
myCar.setColor("Red"); // Set the color using a setter method
myCar.accelerate();
System.out.println("Car color: " + myCar.getColor() + ", Speed: " + myCar.speed);
}
}
In this example, the Car
class defines the blueprint for car objects with properties like color and speed, and methods like accelerate and brake. We create a myCar
object and interact with its properties and methods.