Abstract class

from Wikipedia, the free encyclopedia

In object-oriented programming, an abstract class denotes a special class which, by definition, cannot be instantiated, i.e. This means that no objects can be created from it, and thus only serves as a structural element within a class hierarchy. Within abstract classes it is possible to declare abstract methods, i.e. methods without implementation only with the signature .

Interfaces are purely abstract classes that only declare method signatures. A class, on the other hand, is already considered abstract as soon as a method is available that has to be implemented by an inheriting class. Variables can also be defined and methods implemented in an abstract class .

As base classes in a class hierarchy, abstract classes can define basic properties of their derived classes without actually implementing them. If a class derives from an abstract class, all inherited abstract methods must be overwritten and implemented so that the inheriting class itself is not abstract.

Abstract classes cannot be instantiated themselves, only specializations of them. However, parts of the source code can be kept general and only implemented using the properties of the abstract base type. The special implementations of the non-abstract derived classes are carried out through polymorphism .

Examples

geometry

Two-dimensional geometric figures have in common that they have an area . Depending on the figure, this area is calculated differently.

If an abstract base class name is Geometrischcreated, a method body for a function berechneFlaecheninhaltFlaechecan be used to specify that all derived classes must implement a corresponding method . For example, a class will Rechteck multiply the length by the width, in a class RechtwinkligesDreieckthe length will be multiplied by the height and the product will be halved.

The class hierarchy with an abstract base class made it possible to create functions elsewhere that Geometrischreceive an object of the type and berechneFlaecheninhaltcall the function on it , regardless of whether it is a triangle , rectangle or circle . The functions can thus be used for all geometric figures and have a high degree of flexibility and reusability.

Sample program

The following examples are written in the Java programming language. As data type one is floating point double used.

A. The definition of the interface :

public interface Geometrisch
{
    public abstract double berechneFlaecheninhalt();
}

B. Create a base class for height and length specification:

public abstract class BasisFigur implements Geometrisch
{
    double laenge;
    double hoehe;
	
	// Konstruktor
    public BasisFigur(final double LAENGE, final double HOEHE)
    {
        laenge = LAENGE;
        hoehe = HOEHE;
    }

    public double getLaenge()
    {
        return laenge;
    }

    public void setLaenge(final double LAENGE)
    {
        laenge = LAENGE;
    }

    public double getHoehe()
    {
        return hoehe;
    }

    public void setHoehe(final double HOEHE)
    {
        hoehe = HOEHE;
    }
}

C. Exemplary implementation of a concrete figure rectangle :

public class Rechteck extends BasisFigur
{
    // Konstruktor
    public Rechteck(final double LAENGE, final double HOEHE)
    {
        super(LAENGE, HOEHE);
    }

    @Override
    public double berechneFlaecheninhalt()
    {
        return getLaenge() * getHoehe();
    }
}

D. Exemplary implementation of another concrete figure right angled triangle :

public class RechtwinkligesDreieck extends BasisFigur
{
    // Konstruktor
    public RechtwinkligesDreieck(final double LAENGE, final double HOEHE)
    {
        super(LAENGE, HOEHE);
    }

    @Override
    public double berechneFlaecheninhalt()
    {
        return (getLaenge() * getHoehe()) / 2;
    }
}

E. An example class for extended use which performs operations based on the interface :

public class GeometrischerRechner
{
    public static double berechneGesamtFlaecheninhalt(final Geometrisch[] figuren)
    {
        double ergebnis = 0;
        for (Geometrisch figur : figuren)
        {
            ergebnis += figur.berechneFlaecheninhalt();
        }
        return ergebnis;
    }
}

F. An example of an implementation provides the following class :

public class GeometrischesFigurenBeispiel
{
    public static void main(String[] args)
    {
        // 1.1. Erstellung einzelner Objekte
        Geometrisch rechteck = new Rechteck(10, 20);
        Geometrisch dreieck = new RechtwinkligesDreieck(10, 20);
		
        // 1.2. Separate Berechnung der Flächeninhalte
        System.out.println("Rechtecksfläche: " + rechteck.berechneFlaecheninhalt());
        System.out.println("Dreiecksfläche: " + dreieck.berechneFlaecheninhalt());
		
        // 2.1. Parameter für die Übergabe an GeometrischerRechner
        Geometrisch[] figuren = new Geometrisch[]{rechteck, dreieck};
		
        // 2.2. Berechnung und Ausgabe der Gesamtfläche
        System.out.println("Gesamtfläche: " + GeometrischerRechner.berechneGesamtFlaecheninhalt(figuren));
    }
}

G. If this program is compiled and executed, the following output appears in the console:

  Rechtecksfläche: 200.0
  Dreiecksfläche: 100.0
  Gesamtfläche: 300.0

Example of database access

A classic example of the use of abstract classes comes from the area of database applications. All methods for accessing the database are defined in an abstract class. A specific class can be programmed for each database type , which implements all inherited access methods. The specifically implemented class must be known at a single point in the application - this is where the object is created. It is then treated as an object of the type of the abstract class throughout the rest of the program code .

The concrete class can be specified as a property, a so-called property , in a file outside of the program code . This means that no new compilation is necessary after the database has been replaced, and the application is highly reusable.

Web links