Encapsulation:
Hiding the implementation details behind interfaces is called Encapsulation. The ability to make changes to implementation without breaking the code of others is the key benefit of encapsulation.
To implement Encapsulation:
Make public accessor methods, and force calling code to use those methods rather than directly accessing the instance variable.
Association:
Association is a relationship between 2 objects. (defines the relationship between objects like one-to-one, one-to-many, many-to-one, many-to-many)
eg. Student and Teacher, Student is related to the teacher
UML notation:
Aggregation:
Aggregation is a special type of association between 2 objects, where one object has a "has-a" relationship on another object. ie. one object contains another object. (A directional association between 2 objects)
eg. Class and Student, Class has Student.
UML notation:
Composition:
Composition is a special type of Aggregation, where one object contains another object and the contained object cannot exist with the container object.
eg. Wheel and Car, Wheel cannot exist without the car.
UML notation:
Generalization:
Generalization uses a "is-a" relationship from a Specialization to a Generalization class.
UML notation:
Realization:
Relation between a Class and its object. The object is said to realize the class.
UML notation:
Dependency:
If changes in the structure or behavior in one class affects another related class, then there is a dependancy between those 2 class. The vice-versa need not be true.
UML notation:
Reasons to use Inheritance:
- To promote code reuse
- To use polymorphism
IS-A and HAS-A relationships:
- IS-A relationships can be implemented through inheritance or by interface implementation.
- HAS-A is implemented with Using/Usage (Composition).
Polymorphism:
A Java object that can pass more than one IS-A test can be considered polymorphic.