What is encapsulation in OOPs

OO paradigms

association

An association describes a relationship between two types, in the most common case a connection between two objects of the classes

Association ends

An association forms a kind of bridge between two types: if you start with the instance of the one involved type, you can navigate to the instance of the second type via an object relationship.

  • 1) No statement about navigability. The model does not make any statement about navigability. It is unspecified and should only be defined at a later point in time, for example during software design.
  • 2) Permitted navigation. The model allows navigation via the end of the association.
  • 3) Not allowed navigation. The model forbids navigation via the end of the association.
If you want to highlight the end of an association or strengthen the bond of an association, aggregation and composition are available as a means.

Aggregation

An exact definition is not given in UML2, rather it is pointed out that an aggregation varies depending on the application and modeler. A concrete benefit can be derived, for example, by giving special emphasis to one end of an association.

Basically, the distinction between association and aggregation is difficult. A weak indicator for the sensible use of an aggregation seems to be the existence of transitivities between the modeled classes. That is, if there is a part / whole relationship between A and B and also between B and C, then A must also be part of C.

 

composition

The composition describes the relationship between a whole and its parts. A part can only ever be assigned to exactly one whole. Parts that are connected to a whole via a composition may appear in at most one (0..1) whole.

 

Data encapsulation

In programming, data encapsulation is the term used to describe the hiding of data or information from outside access. Direct access to the internal data structure is prevented and instead takes place via defined interfaces.

Access types

  • public (+)
    • accessible for all versions (including those of other classes),
  • private (-)
    • Only accessible for versions of their own class,
  • protected (#)
    • Only accessible for versions of their own class and specializations of the same,

Benefits of Encapsulation

  • Since the implementation of a class is not known to other classes, the implementation can be changed without affecting the interaction with other classes.
  • Increased clarity, since only the public interface of a class has to be considered.
  • When accessing via an access function, it does not matter from the outside whether this function exists 1: 1 inside the class, is the result of a calculation, or possibly comes from other sources (e.g. a file or database).
  • Significantly improved testability, stability and changeability of the software or its parts (modules).

Disadvantages of the encapsulation

  • Depending on the application, speed losses due to the call of the method (direct access to the data elements would be faster).
  • Additional programming effort for creating the access methods.

Polymorphism

importance

Polymorphism ' (polymorphism = diversity) is next to Encapsulation and Inheritance the third pillar of the OOP. All object-oriented languages support this concept: apparently similar objects can differ in behavior.

The Polymorphism of object-oriented programming is a property that is always related to Inheritance occurs.

  • Means that a single variable can be used for different objects of related classes (at different times) in a program.
  • If the variable with the dot notation variable.methode () is used to call a method, it depends on the object which the variable is currently referring to, which method is actually being executed.

The whole thing is with virtual Methods of Base classes realized. It is only through these methods that this becomes possible "Child classes" using the same methods as that virtual methods of the base classesto overwrite them when the program is running.

For example, in a Base class TFigur a method TFigur.Ma├čeBeechnen exist. This method can be used by a Child class Treieck must be overwritten, as the dimensions of a triangle are now to be calculated.

Inheritance

In object orientation, inheritance is a procedure for building new constructs (classes or prototypes) hierarchically using existing ones.

principle

The definition of a new class can, if necessary, build on the definition of an already existing class so that the new class takes over the characteristics of the existing ones and adds new components. The adoption of the characteristics of the existing class is called inheritance.
In inheritance is between Interface inheritance and Implementation inheritance to distinguish. With interface inheritance, a derived class inherits the signatures of methods, but has to implement the methods itself. With implementation inheritance, the derived class also inherits the implementation from one or more base classes or the implementation of the prototype.
If a new characteristic covers a characteristic that was inherited, then we speak of overwriting.
Inheritance can be used if there are classes that conceptually build on one another. If necessary, class definitions can be divided from the outset in such a way that identical features are combined in the definition of an inheriting class.

Example: interface inheritance

A vehicle has certain attributes, such as maximum speed, maximum payload or color. The motor vehicle class inherits all of these attributes, but can also have additional attributes, e.g. power or torque. Furthermore, a motor vehicle can also have additional methods, such as acceleration, which the vehicle base class does not know.
The passenger car class can then in turn be derived from motor vehicle and have additional attributes such as the number of seats. By deriving from motor vehicle, the passenger vehicle automatically inherits all attributes from vehicle.
A simple is-a rule can be used to determine whether a class is inherited from another class. As soon as the statement (object of the derived class) is a (object of the base class) applies, both classes are in an inheritance relationship.

A passenger car is a motor vehicle, but a passenger car is a vehicle
A vehicle is not a passenger car.
A passenger car is not a seat
A passenger car has a seat.
Interface vererbung.jpg

Example: multiple inheritance

Multiple inheritance - in contrast to multilevel inheritance - is when a class inherits directly from several classes.
An application example for this is the modeling of an amphibious vehicle that inherits the attributes of both land vehicles and water vehicles. This means that the amphibious vehicle has both a number of wheels and a depth.

CategoriesProgramming