OOP.Explain the main concepts of OOP: “class”, “object”, “interface”.

📚 1. Class

What is a Class?

A class is a blueprint or template for creating objects. It defines: ✅ Attributes (fields or properties — data that the object will have)
Methods (behavior — what the object can do)


Example

class Car {
    String brand;       // attribute
    int speed;          // attribute

    void accelerate() { // method
        speed += 10;
    }

    void brake() {      // method
        speed -= 10;
    }
}

Key Point

A class itself is not a real-world thing, it’s just a design.


🚗 2. Object

What is an Object?

An object is a real-world instance of a class. When you create an object from a class, it gets actual values and can perform actions using the methods of the class.

Car myCar = new Car();
myCar.brand = "Toyota";
myCar.speed = 0;

myCar.accelerate(); // myCar's speed = 10

Key Point

Objects are the real entities created using the class blueprint.


🔌 3. Interface

What is an Interface?

An interface is a special type of class that: ✅ Only defines method signatures (what should be done — not how)
✅ Has no implementation (no method body) — the actual logic is provided by classes that implement the interface


Example

interface Payment {
    void processPayment(double amount);
}

Classes like CreditCardPayment and PayPalPayment can implement this interface and provide their own versions of processPayment():





class CreditCardPayment implements Payment {
    @Override
    public void processPayment(double amount) {
        System.out.println("Processing credit card payment of $" + amount);
    }
}

class PayPalPayment implements Payment {
    @Override
    public void processPayment(double amount) {
        System.out.println("Processing PayPal payment of $" + amount);
    }
}

Why Use Interfaces?

✅ To enforce a common contract across different classes (they must implement the methods).
✅ Supports polymorphism — you can write flexible code that works with any Payment object (credit card, PayPal, etc.), without caring about the specific type.


Key Point

An interface defines what must be done, but leaves the how to the classes that implement it.


🔗 Quick Summary Table

ConceptWhat is it?Key Role
ClassBlueprint/templateDefines structure (attributes + methods)
ObjectInstance of a classReal thing, actual data + behavior
InterfaceContractDefines behavior that multiple classes must follow
This entry was posted in Без рубрики. Bookmark the permalink.