Real-World Example A car abstracts the internal details and exposes the driver to only details that are relevant to the interaction of the driver with the car. For example, when we have a phone call, we don't know the internal processing. In Java, we use the abstract class and interface to achieve abstraction. We never buy a "device," but we always buy something more specific: Read more in-depth information with a class diagram and source code examples here: Abstraction in Java with Example.
Encapsulation Intent Encapsulation refers to combining data and associated functions as a single unit. In OOP, data and functions operating on that data are combined together to form a single unit, which is referred to as a class. Capsule — it is wrapped with different medicines. A Java class is the example of encapsulation. A Java bean is the fully encapsulated class because all the data members are private here. Inheritance Intent Inheritance — this is a relationship between a superclass and its subclasses.
Accordingly, this is a process where one object acquires the members of another; plus, it can have its own. Explanation Inheritance is a reusability mechanism in object-oriented programming in which the common properties of various objects are exploited to form relationships with each other. The abstract and common properties are provided in the superclass, which is available to the more specialized subclasses. When we say that class B is inherited from another class A, then class B is referred to as a derived class or subclassand class A is called as a base class or superclass.
By inheritance, the derived class receives the behavior of the base class so that all the visible member methods and variables of the base class are available in the derived class.
10 OOPS Concepts in Java
Apart from the inherited behavior, the derived class specializes its behavior by adding to or overriding base class behavior. Polymorphism Polymorphism lets us perform a single action in different ways. Polymorphism allows you to define one interface and have multiple implementations We can create functions or reference variables that behave differently in different programmatic context.
Polymorphism means many forms. Association Intent It represents a relationship between two or more objects where all objects have their own lifecycle and there is no owner. The name of an association specifies the nature of the relationship between objects. Association is a relation between two separate classes that is established through their objects.
Association can be one-to-one, one-to-many, many-to-one, and many-to-many. In object-oriented programming, an object communicates to another object to use functionality and services provided by that object.
There are two forms of association: Composition Intent Composition is an association that represents a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted, then all parts are deleted. It has a stronger relationship. It represents a part-of relationship. In composition, both the entities are dependent on each other. The UML representation of a composition relationship shows composition as a filled diamond shape on the containing class end of the lines that connect contained class es to the containing class.
Differences between Composition and Aggregation[ edit ] Composition relationship 1.Has-a and Is-a Relationships
When attempting to represent real-world whole-part relationships, e. When the container is destroyed, the contents are also destroyed, e.
Class diagram - Wikipedia
When representing a software or database relationship, e. When the container is destroyed, the contents are usually not destroyed, e. Thus the aggregation relationship is often "catalog" containment to distinguish it from composition's "physical" containment. In practice, means that any instance of the subtype is also an instance of the superclass. An exemplary tree of generalizations of this form is found in biological classification: The relationship is most easily understood by the phrase 'an A is a B' a human is a mammal, a mammal is an animal.
The UML graphical representation of a Generalization is a hollow triangle shape on the superclass end of the line or tree of lines that connects it to one or more subtypes.
The generalization relationship is also known as the inheritance or "is a" relationship. The superclass base class in the generalization relationship is also known as the "parent", superclass, base class, or base type. The subtype in the specialization relationship is also known as the "child", subclass, derived class, derived type, inheriting class, or inheriting type. Note that this relationship bears no resemblance to the biological parent—child relationship: A is a type of B For example, "an oak is a type of tree", "an automobile is a type of vehicle" Generalization can only be shown on class diagrams and on use case diagrams.
The UML graphical representation of a Realization is a hollow triangle shape on the interface end of the dashed line or tree of lines that connects it to one or more implementers. A plain arrow head is used on the interface end of the dashed line that connects it to its users. In component diagrams, the ball-and-socket graphic convention is used implementors expose a ball or lollipop, whereas users show a socket.
Realizations can only be shown on class or component diagrams. A realization is a relationship between classes, interfaces, components and packages that connects a client element with a supplier element. One class depends on another if the independent class is a parameter variable or local variable of a method of the dependent class.
This is different from an association, where an attribute of the dependent class is an instance of the independent class. Sometimes the relationship between two classes is very weak.
They are not implemented with member variables at all. Rather they might be implemented as member function arguments. Multiplicity[ edit ] This association relationship indicates that at least one of the two related classes make reference to the other. This relationship is usually described as "A has a B" a mother cat has kittens, kittens have a mother cat.
The UML representation of an association is a line connecting the two associated classes. At each end of the line there is optional notation. For example, we can indicate, using an arrowhead that the pointy end is visible from the arrow tail.
We can indicate ownership by the placement of a ball, the role the elements of that end play by supplying a name for the role, and the multiplicity of instances of that entity the range of number of objects that participate in the association from the perspective of the other end.