Sobes.tech
Senior

Tell about OOP — what is it, what is it for?

sobes.tech AI

Answer from AI

Object-oriented programming (OOP) is a programming paradigm based on the concept of objects, which combine data and methods to operate on that data.

Main principles of OOP:

  • Encapsulation — combining data and methods within a single object, hiding internal implementation.
  • Inheritance — the ability to create new classes based on existing ones, inheriting their properties and methods.
  • Polymorphism — the ability of objects of different classes to respond to the same messages (methods) in different ways.
  • Abstraction — highlighting essential characteristics of an object, hiding implementation details.

OOP helps model real entities, improves code structure and reusability, and facilitates maintenance and extension of programs.

Example in Java:

class Animal {
    void sound() {
        System.out.println("Some sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Bark");
    }
}

// Usage
Animal myDog = new Dog();
myDog.sound(); // Outputs "Bark"