OOP Fundamentals: Class, Object, Instance, Attributes & Methods
This post is a definition of terms covering the fundamentals of object-oriented programming – including classes, objects, attributes, and methods.
In a Nutshell
OOP is a programming paradigm that composes software from reusable, clearly structured “building blocks” – the objects. A class is the blueprint, an object is the concrete manifestation.
Concise Technical Description
Object-Oriented Programming (OOP) is a paradigm that models real-world objects through software objects. The central distinction between class and object is fundamental:
Class (Blueprint):
- Abstract template or schema
- Defines attributes (properties) and methods (capabilities)
- Exists only once in code
- Example: Class
Autodefines properties such as color and methods likebrake()
Object/Instance (concrete thing):
- Concrete manifestation of a class
- Created at runtime (
newoperator) - Has specific attribute values
- Example: Object
myGolfwith color=“blue”, power=150HP
Core Concepts:
- Encapsulation: Data and methods bundled into a unit
- Reusability: Classes can be instantiated multiple times
- Structuring: Clear separation of responsibilities
- Abstraction: Complex reality reduced to relevant properties
Exam-Relevant Key Points
- Class vs Object: Abstract blueprint vs concrete instance
- Attributes: Properties/data of an object
- Methods: Behavior/capabilities of an object
- Instantiation: Creation of objects from classes
- Constructor: Special method for object creation
- Encapsulation: Bundling data and methods together
- IHK-relevant: Fundamental understanding for software development
- Practice: Code reuse and maintainability
Core Components
- Class: Definition with attributes and methods
- Object: Concrete instance of a class
- Attributes: Properties/data of an object
- Methods: Behavior/functions of an object
- Constructor: Initialization method for new objects
- Instance Variables: Object-specific data
- Class Methods: Methods valid for the class
- Getter/Setter: Access methods for attributes
Practical Examples
1. Simple Class in Java
// Class as blueprint
public class Auto {
// Attributes (properties)
private String farbe;
private int anzahlTueren;
private int leistungInPS;
private int aktuelleGeschwindigkeit;
// Constructor for object creation
public Auto(String farbe, int anzahlTueren, int leistungInPS) {
this.farbe = farbe;
this.anzahlTueren = anzahlTueren;
this.leistungInPS = leistungInPS;
this.aktuelleGeschwindigkeit = 0;
}
// Methods (behavior)
public void beschleunigen(int kmh) {
this.aktuelleGeschwindigkeit += kmh;
System.out.println("Auto beschleunigt auf " + this.aktuelleGeschwindigkeit + " km/h");
}
public void bremsen(int kmh) {
if (this.aktuelleGeschwindigkeit >= kmh) {
this.aktuelleGeschwindigkeit -= kmh;
System.out.println("Auto bremst auf " + this.aktuelleGeschwindigkeit + " km/h");
} else {
System.out.println("Kann nicht unter 0 km/h bremsen");
}
}
// Getter methods for attribute access
public String getFarbe() {
return farbe;
}
public int getAktuelleGeschwindigkeit() {
return aktuelleGeschwindigkeit;
}
public void anzeigen() {
System.out.println("Auto - Farbe: " + farbe +
", Türen: " + anzahlTueren +
", Leistung: " + leistungInPS + " PS" +
", Geschwindigkeit: " + aktuelleGeschwindigkeit + " km/h");
}
}
// Usage of the class
public class Garage {
public static void main(String[] args) {
// Create objects (instances)
Auto meinGolf = new Auto("blau", 5, 150);
Auto deinA3 = new Auto("schwarz", 3, 120);
Auto companyAuto = new Auto("silber", 4, 200);
// Use objects
meinGolf.anzeigen();
meinGolf.beschleunigen(50);
meinGolf.beschleunigen(30);
meinGolf.bremsen(20);
System.out.println("---");
deinA3.anzeigen();
deinA3.beschleunigen(80);
System.out.println("---");
companyAuto.anzeigen();
companyAuto.beschleunigen(100);
}
}
2. Class in Python
# Class as blueprint
class Auto:
# Class attribute (valid for all cars)
anzahl_autos = 0
# Constructor
def __init__(self, farbe, anzahl_tueren, leistung_in_ps):
# Instance attributes (object-specific)
self.farbe = farbe
self.anzahl_tueren = anzahl_tueren
self.leistung_in_ps = leistung_in_ps
self.aktuelle_geschwindigkeit = 0
# Increment class attribute
Auto.anzahl_autos += 1
# Methods
def beschleunigen(self, kmh):
self.aktuelle_geschwindigkeit += kmh
print(f"Auto beschleunigt auf {self.aktuelle_geschwindigkeit} km/h")
def bremsen(self, kmh):
if self.aktuelle_geschwindigkeit >= kmh:
self.aktuelle_geschwindigkeit -= kmh
print(f"Auto bremst auf {self.aktuelle_geschwindigkeit} km/h")
else:
print("Kann nicht unter 0 km/h bremsen")
def anzeigen(self):
print(f"Auto - Farbe: {self.farbe}, Türen: {self.anzahl_tueren}, " +
f"Leistung: {self.leistung_in_ps} PS, " +
f"Geschwindigkeit: {self.aktuelle_geschwindigkeit} km/h")
# Class method
@classmethod
def get_anzahl_autos(cls):
return cls.anzahl_autos
# Usage of the class
def garage_demo():
# Create objects (instances)
mein_golf = Auto("blau", 5, 150)
dein_a3 = Auto("schwarz", 3, 120)
company_auto = Auto("silber", 4, 200)
# Use objects
mein_golf.anzeigen()
mein_golf.beschleunigen(50)
mein_golf.beschleunigen(30)
mein_golf.bremsen(20)
print("---")
dein_a3.anzeigen()
dein_a3.beschleunigen(80)
print("---")
company_auto.anzeigen()
company_auto.beschleunigen(100)
print(f"Anzahl erstellter Autos: {Auto.get_anzahl_autos()}")
if __name__ == "__main__":
garage_demo()
3. Class in C#
using System;
// Class as blueprint
public class Auto
{
// Attributes (properties)
private string farbe;
private int anzahlTueren;
private int leistungInPS;
private int aktuelleGeschwindigkeit;
// Static property for all cars
public static int AnzahlAutos { get; private set; }
// Constructor
public Auto(string farbe, int anzahlTueren, int leistungInPS)
{
this.farbe = farbe;
this.anzahlTueren = anzahlTueren;
this.leistungInPS = leistungInPS;
this.aktuelleGeschwindigkeit = 0;
AnzahlAutos++;
}
// Methods
public void Beschleunigen(int kmh)
{
this.aktuelleGeschwindigkeit += kmh;
Console.WriteLine($"Auto beschleunigt auf {this.aktuelleGeschwindigkeit} km/h");
}
public void Bremsen(int kmh)
{
if (this.aktuelleGeschwindigkeit >= kmh)
{
this.aktuelleGeschwindigkeit -= kmh;
Console.WriteLine($"Auto bremst auf {this.aktuelleGeschwindigkeit} km/h");
}
else
{
Console.WriteLine("Kann nicht unter 0 km/h bremsen");
}
}
// Properties for attribute access
public string Farbe => farbe;
public int AktuelleGeschwindigkeit => aktuelleGeschwindigkeit;
public void Anzeigen()
{
Console.WriteLine($"Auto - Farbe: {farbe}, Türen: {anzahlTueren}, " +
$"Leistung: {leistungInPS} PS, " +
$"Geschwindigkeit: {aktuelleGeschwindigkeit} km/h");
}
}
// Usage of the class
public class Garage
{
public static void Main(string[] args)
{
// Create objects (instances)
Auto meinGolf = new Auto("blau", 5, 150);
Auto deinA3 = new Auto("schwarz", 3, 120);
Auto companyAuto = new Auto("silber", 4, 200);
// Use objects
meinGolf.Anzeigen();
meinGolf.Beschleunigen(50);
meinGolf.Beschleunigen(30);
meinGolf.Bremsen(20);
Console.WriteLine("---");
deinA3.Anzeigen();
deinA3.Beschleunigen(80);
Console.WriteLine("---");
companyAuto.Anzeigen();
companyAuto.Beschleunigen(100);
Console.WriteLine($"Anzahl erstellter Autos: {Auto.AnzahlAutos}");
}
}
Concepts in Detail
Constructors
// Overloaded constructors in Java
public class Auto {
private String farbe;
private int leistung;
// Default constructor
public Auto() {
this.farbe = "schwarz";
this.leistung = 100;
}
// Constructor with parameters
public Auto(String farbe, int leistung) {
this.farbe = farbe;
this.leistung = leistung;
}
// Copy constructor
public Auto(Auto other) {
this.farbe = other.farbe;
this.leistung = other.leistung;
}
}
Static vs Instance Members
public class MathUtil {
// Static method - belongs to the class
public static int addiere(int a, int b) {
return a + b;
}
// Instance method - belongs to the object
private int wert;
public MathUtil(int startWert) {
this.wert = startWert;
}
public int addiereZuWert(int a) {
this.wert += a;
return this.wert;
}
}
// Usage
int ergebnis1 = MathUtil.addiere(5, 3); // Static
MathUtil rechner = new MathUtil(10);
int ergebnis2 = rechner.addiereZuWert(5); // Instance
Getters and Setters
public class Person {
private String name;
private int alter;
// Getter
public String getName() {
return name;
}
// Setter with validation
public void setAlter(int alter) {
if (alter >= 0 && alter <= 150) {
this.alter = alter;
} else {
throw new IllegalArgumentException("Ungültiges Alter");
}
}
// Getter for calculated value
public boolean istVolljaehrig() {
return alter >= 18;
}
}
Advantages and Disadvantages
Advantages of OOP
- Reusability: Classes can be used multiple times
- Maintainability: Clear structure makes changes easier
- Understandability: Models the real world
- Encapsulation: Data is protected and controlled access
- Scalability: Large systems can be structured
Disadvantages
- Overhead: More code for simple tasks
- Learning curve: Object-oriented thinking requires practice
- Performance: Object creation can be expensive
- Complexity: Becomes confusing with too many classes
Common Exam Questions
-
What is the difference between a class and an object? A class is the blueprint, an object is the concrete instance at runtime.
-
Explain the terms attribute and method! Attributes are properties/data, methods are behavior/functions of an object.
-
What is a constructor? Special method for initializing new objects, called when
newis invoked. -
Why is encapsulation important? Protects data from uncontrolled access and enables validation.
Most Important Sources
- https://de.wikipedia.org/wiki/Objektorientierte_Programmierung
- https://docs.oracle.com/javase/tutorial/java/concepts/
- https://docs.python.org/3/tutorial/classes.html
Recommended Literature: Object-Oriented Programming
Keine Bücher für Kategorie "objektorientierte-programmierung" gefunden.