Sobes.tech
Junior — Middle

Can you name the main patterns of object-oriented programming?

sobes.tech AI

Answer from AI

The main patterns of object-oriented programming (OOP) include:

  • Inheritance: allows creating new classes based on existing ones, inheriting their properties and methods.
  • Encapsulation: hides the internal details of an object, providing access only through public methods.
  • Polymorphism: the ability of objects of different classes to respond to the same messages (methods) in different ways.
  • Abstraction: highlighting the essential characteristics of an object, hiding non-essential details.

These patterns help create flexible, extensible, and maintainable programs.

Example in Go (using interfaces for polymorphism):

package main
import "fmt"

type Animal interface {
    Speak() string
}

type Dog struct{}
func (d Dog) Speak() string {
    return "Woof!"
}

type Cat struct{}
func (c Cat) Speak() string {
    return "Meow!"
}

func main() {
    animals := []Animal{Dog{}, Cat{}}
    for _, animal := range animals {
        fmt.Println(animal.Speak())
    }
}

In this example, different types implement a single interface, demonstrating polymorphism.

Can you name the main patterns of object-oriented… - sobes.tech