Sobes.tech
Junior — Middle

Can you explain the main principles and features of object-oriented programming?

sobes.tech AI

Answer from AI

Object-oriented programming (OOP) is based on several key principles:

  • Encapsulation — combining data and methods that operate on that data into a single object. This allows hiding internal implementation details and protecting data from direct external access.

  • Inheritance — a mechanism that allows creating new classes based on existing ones, inheriting their properties and methods. This promotes code reuse and hierarchical organization.

  • Polymorphism — the ability of objects of different classes to respond differently to the same messages (method calls). This is achieved through method overriding and interfaces.

  • Abstraction — highlighting essential characteristics of an object while hiding implementation details. It enables working with objects through abstract interfaces.

Example in Java:

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

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

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        myDog.sound(); // Outputs "Bark" — an example of polymorphism
    }
}
Can you explain the main principles and features of… - sobes.tech