Skip to content
IRC-Coding IRC-Coding
OOP Fundamentals Classes Objects Attributes Methods Instance Constructor

OOP Fundamentals: Classes, Objects, Attributes & Methods

Learn OOP basics: classes as blueprints, objects as instances. Master attributes, methods, constructors, and abstract design patterns.

S

schutzgeist

2 min read
OOP Fundamentals: Classes, Objects, Attributes & Methods

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 Auto defines properties such as color and methods like brake()

Object/Instance (concrete thing):

  • Concrete manifestation of a class
  • Created at runtime (new operator)
  • Has specific attribute values
  • Example: Object myGolf with 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

  1. Class: Definition with attributes and methods
  2. Object: Concrete instance of a class
  3. Attributes: Properties/data of an object
  4. Methods: Behavior/functions of an object
  5. Constructor: Initialization method for new objects
  6. Instance Variables: Object-specific data
  7. Class Methods: Methods valid for the class
  8. 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

  1. What is the difference between a class and an object? A class is the blueprint, an object is the concrete instance at runtime.

  2. Explain the terms attribute and method! Attributes are properties/data, methods are behavior/functions of an object.

  3. What is a constructor? Special method for initializing new objects, called when new is invoked.

  4. Why is encapsulation important? Protects data from uncontrolled access and enables validation.

Most Important Sources

  1. https://de.wikipedia.org/wiki/Objektorientierte_Programmierung
  2. https://docs.oracle.com/javase/tutorial/java/concepts/
  3. https://docs.python.org/3/tutorial/classes.html

Keine Bücher für Kategorie "objektorientierte-programmierung" gefunden.

Back to Blog
Share:

Related Posts