# Programmering 2 - Rehearsal OOP ###### tags: `python` `oop` Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs. It is based on four main principles: abstraction, encapsulation, inheritance, and polymorphism. ## Abstraction Abstraction is the process of hiding the implementation details and showing only the necessary information to the users. It helps in reducing the complexity of the program and makes it more manageable. An example of abstraction in OOP is using a class to represent a bank account, with methods like deposit and withdraw, without showing the internal details of how the account balance is stored and updated. ## Encapsulation Encapsulation is the process of wrapping the data (variables) and functions (methods) inside a class, making it inaccessible to the outside world. By encapsulating the data, we can ensure that it is not accidentally modified or accessed by external entities. Encapsulation provides a level of security and allows for greater control over the inner workings of an object. ## Inheritance Inheritance is the ability of a class to inherit properties and methods from its parent class. It allows code reuse and promotes the DRY (Don't Repeat Yourself) principle. In OOP, inheritance creates a hierarchical relationship between classes, where the derived class inherits the properties and methods of the base class. ## Polymorphism Polymorphism is the ability of an object to take on many forms. In OOP, polymorphism allows for objects of different classes to be treated as objects of a common base class. This allows for a more generic and flexible code, where a single method can work with different types of objects, depending on their specific implementation. ## Example code (Shape) ```python= class Shape: def __init__(self, x, y): self.x = x self.y = y def area(self): pass class Rectangle(Shape): def __init__(self, x, y, width, height): super().__init__(x, y) self.width = width self.height = height def area(self): return self.width * self.height class Circle(Shape): def __init__(self, x, y, radius): super().__init__(x, y) self.radius = radius def area(self): return 3.14 * (self.radius ** 2) shape_1 = Rectangle(0, 0, 4, 5) shape_2 = Circle(0, 0, 3) print(shape_1.area()) # 20 print(shape_2.area()) # 28.26 ``` In the above example, Shape is the base class and Rectangle and Circle are derived classes. The area() method is a polymorphic method that is implemented differently by the derived classes, but can be called on objects of both classes using the same syntax --- # Repetition Object-oriented programming (OOP) är en programmeringsparadigm som använder objekt och deras interaktioner för att designa program och datorprogram. Det bygger på fyra huvudprinciper: abstraktion, inkapsling, arv och polymorfism. ## Abstraktion Abstraktion är processen att dölja implementationdetaljerna och visa endast den nödvändiga informationen för användarna. Det hjälper till att minska komplexiteten i programmet och gör det mer hanterbart. Ett exempel på abstraktion i OOP är att använda en klass för att representera ett bankkonto, med metoder som insättning och uttag, utan att visa de interna detaljerna om hur saldot hålls och uppdateras. ## Inkapsling Inkapsling är processen att innesluta data (variabler) och funktioner (metoder) inuti en klass, vilket gör det otillgängligt för omvärlden. Genom att inkapsla data kan vi säkerställa att det inte oavsiktligt ändras eller kommer åt av externa entiter. Inkapsling ger en nivå av säkerhet och ger större kontroll över objektets inre arbete. ## Arv Arv är förmågan hos en klass att ärva egenskaper och metoder från sin förälderklass. Det tillåter kodåteranvändning och främjar DRY (Don't Repeat Yourself) principen. I OOP skapar arv en hierarkisk relation mellan klasser, där den derived klassen ärver egenskaper och metoder från base klassen. ## Polymorfism Polymorfism är förmågan hos ett objekt att ta många former. I OOP tillåter polymorfism att objekt av olika klasser behandlas som objekt av en gemensam base-klass. Det ger mer generisk och flexibel kod, där en metod kan arbeta med olika typer av objekt, beroende på deras specifika implementation.