Boxed(True/False)

Relationship Between Classes | Association, Aggregation, Inheritance - CodeTextPro

Relationship Between Classes:
Association, Aggregation, Inheritance

These terms signify the relationships between classes. These are the building blocks of object oriented programming and very basic stuff.


Association:
Association is a relationship between two objects. In other words, association defines the multiplicity between objects. You may be aware of one-to-one, one-to-many, many-to-one, many to- many all these words define an association between objects. Aggregation is a special form of association. Composition is a special form of aggregation.


Aggregation:
Aggregation is a special case of association. A directional association between objects. When an object ‘has-a’ another object, then you have got an aggregation between them. Direction between
them specified which object contains the other object. Aggregation is also called a “Has-a” relationship.

Let us take an example of car and engine. Engine is a part of each car and both are dependent on each other.


Relationship Between Classes

Generalization/Inheritance:
Generalization uses a “is-a” relationship from a specialization to the generalization class. Common structure and behaviour are used from the specialization to the generalized class. At a very broader level you can understand this as inheritance. Why I take the term inheritance is, you can relate this term very well. Generalization is also called a “Is-a” relationship.


Inheritance is “IS-A” type of relationship. “IS-A” relationship is a totally based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance. Inheritance is a parent-child
relationship where we create a new class by using existing class code. It is just like saying that “A is type of B”. For example is “Apple is a fruit”, “Ferrari is a car”.

For better understanding let us take a real world scenario.
• HOD is a staff member of college.
• All teachers are staff member of college.

Let us take first two assumptions , “HOD is a staff member of college” and “All teachers are staff member of college”. For this assumption we can create a “StaffMember” parent class and inherit
this parent class in “HOD” and “Teacher” class.


Access Modifiers:
An access modifier restricts the access of a class, constructor, data member and method in another class. In java we have four access modifiers:

1. private
2. protected
3. public
4. Default


Private Access Modifier
The scope of private modifier is limited to the class only.

1. Private Data members and methods are only accessible within the class
2. Class and Interface cannot be declared as private
3. If a class has private constructor then you cannot create the object of that class from outside of the class.


Default Access Modifier
When we do not mention any access modifier, it is called default access modifier. The scope of this modifier is limited to the package only. This means that if we have a class with the default access
modifier in a package, only those classes that are in this package can access this class. No other class outside this package can access this class. Similarly, if we have a default method or data member in a class, it would not be visible in the class of another package.



Protected Access Modifier
Protected data member and method are only accessible by the classes of the same package and the subclasses present in any package. You can also say that the protected access modifier is similar to default access modifier with one exception that it has visibility in sub classes. Classes cannot be declared protected. This access modifier is generally used in a parent child relationship.



Public Access Modifier
The members, methods and classes that are declared public can be accessed from anywhere. This modifier doesn’t put any restriction on the access.


Public Access Modifier



Post a Comment

0 Comments