Abstract type

An abstract class refers to the object-oriented programming, a special class, at least one, but also more abstract methods included (ie methods without " hull " ( implementation) only with the signature). By definition, abstract classes can not be instantiated, ie no objects are created from them.

Interfaces are pure abstract classes, declare the only method signatures. One class, however, is already considered as abstract as soon as a method is available, which must be implemented by an inheriting class. In an abstract class, variables can be defined and methods are implemented.

As base classes in a class hierarchy, abstract classes can specify basic properties of its subclasses without implementing this already concretely. Introduces a class from an abstract class that all inherited abstract methods must be overridden and implemented so that the inheriting class itself is not abstract.

Abstract classes can not be instantiated itself, only specializations of these. Nevertheless, parts of code that can be generally maintained and implemented using only the properties of the abstract base type. Through polymorphism while the specific implementations of the non-abstract subclasses are to be executed.



Two-dimensional geometric figures have in common is that they have a surface. Depending on the figure, these will, however, calculated differently.

If an abstract base class called Geometric created, can be determined with a method body for a function berechneFlaeche that all subclasses must implement an appropriate method. For example, a class of rectangle with the width multiplied by the length, in a class RechtwinkligesDreieck the length is multiplied by the height, and the product is cut in half.

Through the class hierarchy with an abstract base class has opened up the possibility elsewhere to create functions that accept an object of type Geometry and calling on him the berechneFlaeche function, regardless of whether they are triangles, rectangles or circles. The functions can therefore be used for all the geometric shapes and have a high degree of flexibility and reusability.

Program Example

The following examples are written in the Java programming language. When a floating-point data type double is being used.

A. The definition of the interface:

Public interface Geometric {        public abstract double berechneFlaeche ();   } B. Creation of a base class for height and length specification:

Public abstract class Base { Geometric figure implements        double length;      height- double;        public base figure ( final double LENGTH, final double HEIGHT ) {          length = LENGTH;          height = HEIGHT;      }        public double getLaenge () {          return len;      }        public void setLaenge ( final double LENGTH ) {          length = LENGTH;      }        public double getHoehe () {          height- return;      }        public void setHoehe ( final double HEIGHT ) {          height = HEIGHT;      } } C. Exemplary implementation of a concrete figure rectangle:

Public class Rectangle extends Figure { base        public Rectangle ( final double LENGTH, final double HEIGHT ) {          super ( LENGTH, HEIGHT );      }        @ Override      public double berechneFlaeche () {          return getLaenge () * getHoehe ();      } } D. Exemplary Implementation another specific figure right triangle:

Public class RechtwinkligesDreieck extends base { figure        public RechtwinkligesDreieck ( final double LENGTH, final double HEIGHT ) {          super ( LENGTH, HEIGHT );      }        @ Override      public double berechneFlaeche () {          return ( getLaenge () * getHoehe ()) / 2;      } } E. executes An example class for extended use which operations on the basis of the interface:

Public class GeometrischerRechner {        public static double berechneGesamtFlaeche (final Geometry [ ] f ) {          double result = 0;          for ( Geometric figure: f ) {              result = figur.berechneFlaeche ();          }          return result;      } } F. Example of an embodiment provides the following class:

Public class Example { Geometric Figure        public static void main ( String [ ] args ) {          / / 1.1. Creation of individual objects          Geometrically square = new rectangle (10, 20);          Geometric triangle = new RechtwinkligesDreieck (10, 20);            / / 1.2. Separate calculation of areas          System.out.println ( "Rectangle area: "                             rechteck.berechneFlaeche ());          System.out.println (" Triangle area: "                             dreieck.berechneFlaeche ());            / / 2.1. Parameters for the transfer to GeometrischerRechner          Geometric [ ] = new Geometric figures [] {                                            rectangle, triangle };            / / 2.2. Calculation and output of total          System.out.println (" Total :"                 GeometrischerRechner.berechneGesamtFlaeche ( figure ) );          } } G. When compiled and run this program, it appears in the console the following output:

Rectangular area: 200.0    Triangle area: 100.0    Total area: 300.0 Example for database access

A classical example of the use of abstract classes comes from the area of ​​the database application. All the methods for access to the database is defined in an abstract class. For each database type a concrete class can be programmed to implement all inherited access methods. In the application, the concrete implemented class must be known at a single point - there the object is created. Then it is treated throughout the rest of the program code in an object of the abstract class type.

The concrete class can be specified in a file outside of the program code as a property, a so-called Property. Thus, no recompilation is needed after exchanged database and there is a high degree of reusability of the application.

  • Object-Oriented Programming