Junior — Middle
Can you explain the concept of object-oriented programming and its main principles?
sobes.tech AI
Answer from AI
Object-oriented programming (OOP) is a programming paradigm based on the use of objects, which combine data and methods for working with that data.
Main principles of OOP:
- Encapsulation — combining data and methods into a single object, hiding internal implementation and providing access only through a public interface.
- Inheritance — creating new classes based on existing ones, allowing code reuse and extension.
- Polymorphism — objects of different classes respond to the same messages (methods) differently.
- Abstraction — highlighting essential characteristics of an object and hiding non-essential details.
Example in Swift (iOS):
class Animal {
func makeSound() {
print("Some sound")
}
}
class Dog: Animal {
override func makeSound() {
print("Bark")
}
}
let myDog = Dog()
myDog.makeSound() // Prints: Bark
Here, the Dog class inherits from Animal and overrides the makeSound method, demonstrating inheritance and polymorphism.