Sobes.tech
Junior — Middle

What are the main principles and capabilities of object-oriented programming implemented in Java?

sobes.tech AI

Answer from AI

Java implements the main principles of object-oriented programming (OOP):

  • Encapsulation — combining data and methods that work with them into classes. Access to data is controlled through access modifiers (private, protected, public).

  • Inheritance — the ability to create a new class based on an existing one, inheriting its properties and methods, which allows for code reuse.

  • Polymorphism — the ability of objects of different classes to respond to the same messages (method calls) differently. In Java, this is implemented through method overriding and interfaces.

  • Abstraction — highlighting common characteristics of objects and hiding implementation details. In Java, this is achieved through abstract classes and interfaces.

Example:

public abstract class Animal {
    public abstract void sound();
}

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

public class Cat extends Animal {
    @Override
    public void sound() {
        System.out.println("Meow");
    }
}

This demonstrates inheritance (Dog and Cat inherit from Animal), abstraction (Animal is an abstract class), and polymorphism (different implementations of the sound method).