Junior — Middle
Can you tell me about the key characteristics of objects in object-oriented programming?
sobes.tech AI
Answer from AI
Key characteristics of objects in object-oriented programming (OOP):
-
Encapsulation — combining data (state) and methods (behavior) within a single object, as well as hiding internal implementation from the outside world.
-
Inheritance — a mechanism that allows creating 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, ensuring flexibility and extensibility.
-
Abstraction — highlighting the essential characteristics of an object while hiding implementation details, simplifying interaction with the object.
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" — example of polymorphism
}
}