Sobes.tech
Junior — Middle

Can you explain the main principles of object-oriented programming implemented in Java?

sobes.tech AI

Answer from AI

The main principles of object-oriented programming (OOP) in Java include:

  • Encapsulation — combining data and methods that operate on that data into a single object, as well as hiding internal implementation details from the outside world.
  • Inheritance — the ability to create new classes based on existing ones, inheriting their properties and methods, which promotes code reuse.
  • Polymorphism — the ability of objects of different classes to respond to the same messages (method calls) in different ways, allowing a single interface to be used for different object types.
  • Abstraction — highlighting the essential characteristics of an object while hiding implementation details, simplifying interaction with objects.

Example:

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" — example of polymorphism
    }
}
Can you explain the main principles of… - sobes.tech