Junior — Middle
Kas saaksite selgitada Java-s rakendatud objektorienteeritud programmeerimise põhiprintsiipe?
sobes.tech AI
Vastus AI-lt
The main principles of object-oriented programming (OOP) in Java include:
- Encapsulation — combining data and methods that work with this data into a single object, as well as hiding the internal implementation 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 differently to the same messages (method calls), allowing a single interface to be used for different object types.
- Abstraction — highlighting the essential characteristics of an object, hiding implementation details, which simplifies 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(); // Will print "Bark" — example of polymorphism
}
}