Sobes.tech
Junior — Middle

Kas saaksite selgitada objektorienteeritud programmeerimise kontseptsiooni ja selle põhiprintsiipe?

sobes.tech AI

Vastus AI-lt

Object-oriented programming (OOP) is a programming paradigm based on the use of objects, which combine data and methods to work with that data.

Key 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 of functionality.
  • Polymorphism — the ability of objects of different classes to respond differently to the same messages (methods).
  • Abstraction — highlighting the 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.