Modul 6 von 15 · 📖 4 min Lesezeit · ⏱ 30 min gesamt
FI-AE 07 Objektorientierte Programmierung (EN)
Inhaltsverzeichnis (6 Abschnitte)
FI-AE 07 Object-Oriented Programming
Object-oriented programming (OOP) forms the foundation of modern software development. In this module, you will deepen your understanding of the core concepts of OOP – encapsulation, inheritance, and polymorphism – and learn to apply them to design robust and maintainable software architectures. Through a comprehensive example, you will develop practical skills in applying the SOLID principles, which form the basis for professional, extensible code design.
Concepts and Background
- Encapsulation
- Encapsulation hides the internal states and implementations of an object and makes them accessible only through defined interfaces (methods). It protects data from unauthorized access and reduces dependencies between components.
- Inheritance
- Inheritance enables the creation of new classes (derived classes) based on existing classes (base classes). The derived class inherits properties and methods from the base class and can extend or override them.
- Polymorphism
- Polymorphism ("many forms") allows objects of different classes to be treated through a common interface. Methods can be implemented differently depending on the object type, which is resolved dynamically at runtime.
- SOLID Principles
- The SOLID principles are five design guidelines for object-oriented software: Single Responsibility Principle, Open/Closed Principle, Liskov Substitution Principle, Interface Segregation Principle, and Dependency Inversion Principle. They ensure flexible, maintainable, and extensible systems.
Architecture Diagram
classDiagram
class Fahrzeug {
+geschwindigkeit: int
+position: Punkt
+beschleune(delta: int)
+bremsen(delta: int)
+getPosition(): Punkt
}
class Punkt {
+x: int
+y: int
+verschiebe(dx: int, dy: int)
}
class Auto extends Fahrzeug {
+anzahlTueren: int
+oeffneTuer(tuerNr: int)
}
class Motorrad extends Fahrzeug {
+hatBeifahrer: boolean
+setzeBeifahrer(status: boolean)
}
Fahrzeug "1" *-- "1" Punkt
Fahrzeug <|-- Auto
Fahrzeug <|-- Motorrad
Practical Steps
- Define a base class
Fahrzeugwith common attributes such asgeschwindigkeitandpositionas well as methods for speed control. This lays the foundation for later inheritance. - Implement encapsulation through access modifiers (e.g.,
privatefor internal data andpublicfor methods) to prevent direct access to object states. - Derive specific classes like
AutoandMotorradfrom the base classFahrzeugand add class-specific attributes and methods. - Override methods of the base class in the derived classes to implement polymorphic behavior – for example, different implementations of the
beschleunemethod. - Apply the Single Responsibility Principle by creating separate classes for different responsibilities (e.g.,
Punktfor position data). - Implement the Open/Closed Principle by designing the base class so that it can be extended but not modified.
- Use interfaces to satisfy the Interface Segregation Principle – for example, an
IFahrzeuginterface with only the necessary methods. - Apply Dependency Injection to decouple from concrete implementations and realize the Dependency Inversion Principle.
Common Pitfalls
Further Resources
- Microsoft documentation on object-oriented programming in C#
- SOLID Principles – Wikipedia
- Refactoring Guru – Design Patterns and SOLID Principles
- Head First Object-Oriented Programming (Book)
- Udemy Course: OOP with Python
Knowledge Check
Four questions for self-assessment. Click on each question to see the correct answer and explanation.
What is the main purpose of encapsulation in object-oriented programming?
- A) Maximum code reusability through inheritance
- B) Hiding internal implementation details and protecting data
- C) Facilitating communication between different objects
- D) Reducing the number of classes in a system
Correct Answer: B. Encapsulation hides internal states and makes them accessible only through defined interfaces, while A and C rather relate to other OOP concepts and D is not the main goal.
Which SOLID principle states that a class should have only one reason to change?
- A) Open/Closed Principle
- B) Interface Segregation Principle
- C) Single Responsibility Principle
- D) Liskov Substitution Principle
Correct Answer: C. The Single Responsibility Principle states that a class should have only a single responsibility, while A relates to extensibility, B to specific interfaces, and D to substitutability of objects.
What does polymorphism mean in the context of object-oriented programming?
- A) The ability of a class to inherit from another class
- B) The ability of objects to change during runtime
- C) The ability to have methods with the same name but different implementations
- D) The ability to bundle data and methods in a class
Correct Answer: C. Polymorphism allows objects of different classes to be treated as objects of a common superclass, with methods being implemented differently depending on the specific object type.