Association and Its Types
Association and Its Types
OOP (JAVA)
Association in Java defines the connection between two classes that are set up
through their objects.
Association manages one-to-one, one-to-many, and many-to-many relationships.
In Java, the multiplicity between objects is defined by the Association.
It shows how objects communicate with each other and how they use the
functionality and services provided by that communicated object.
Association manages one-to-one, one-to-many, many-to-one and many-to-
many relationships.
Let's take an example of each relationship to manage by the Association.
1. A person can have only one passport. It defines the one-to-one
2. If we talk about the Association between a College and Student, a College can have many students. It
defines the one-to-many
3. A state can have several cities, and those cities are related to that single state. It defines the many-to-
one
4. A single student can associate with multiple teachers, and multiple students can also be associated
with a single teacher. Both are created or deleted independently, so it defines the many-to-many
INHERITANCE
• Class:
• A class is a group of objects which have common properties.
• It is a template or blueprint from which objects are created.
• Sub Class/Child Class:
• Subclass is a class which inherits the other class.
• It is also called a derived class, extended class, or child class.
• Super Class/Parent Class:
• Superclass is the class from where a subclass inherits the features.
• It is also called a base class or a parent class.
• Reusability:
• As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when
you create a new class.
• You can use the same fields and methods already defined in the previous class.
EXTENDS KEYWORD
The extends keyword indicates that you are making a new class
that derives from an existing class. The meaning of "extends" is
to increase the functionality.
Inheritance represents the IS-A relationship which is also
known as a parent-child relationship.
EXAMPLE
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
TYPES OF INHERITANCE
On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about
interfaces later.
SINGLE INHERITANCE
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
HIERARCHICAL INHERITANCE EXAMPLE
To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes
have the same method and you call it from child class object, there will be ambiguity to call the method of A or B
class.
Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes.
So whether you have same method or different, there will be compile time error.
MULTIPLE INHERITANCE
class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B{//suppose if it were
If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A relationship.
Consider a situation, Employee object contains many information's such as id, name, email Id etc. It contains one
more object named address, which contains its own Information's such as city, state, country, zipcode etc. as
given below.
WHEN USE AGGREGATION?
• Code reuse is also best achieved by aggregation when there is no is-a relationship.
• Inheritance should be used only if the relationship is-a is maintained throughout the lifetime of the objects
involved; otherwise, aggregation is the best choice.
class Operation{ AGGREGATION EXAMPLE
int square(int n){
return n*n;
} }
class Circle{
Operation op;//aggregation
double pi=3.14;
double area(int radius){
op=new Operation();
int rsquare=op.square(radius);//code reus
ability (i.e. delegates the method call).
return pi*rsquare;
}
Association and Aggregation are two important concepts in Object-Oriented Programming (OOP) and often used
interchangeably. However, there is a difference between the two.
Association refers to a relationship between two objects where one object uses the services of another object. It
represents a has-a relationship, such as a teacher and a student, or a car and an engine. Association can be one-
to-one, one-to-many, many-to-one, or many-to-many.
Aggregation refers to a special type of association where an object is a part of another object. In aggregation, an
object can exist independently of the other object. For example, a Department object and a Teacher object,
where a teacher is a part of a department, but can also exist independently.
In summary, Association refers to the relationship between objects and Aggregation refers to a special type of
Association which represents a part-of relationship.
SUPER KEYWORD
class Animal{
Animal(){System.out.println("animal is created");}
}
class Dog extends Animal{
Dog(){
super();
System.out.println("dog is created");
}
}
class TestSuper3{
public static void main(String args[]){
Dog d=new Dog();
}}
• As we know well that default constructor is provided by compiler automatically if there is no
constructor. But, it also adds super() as the first statement.
If you make any method as final, you cannot override class Bike{
it. final void run(){System.out.println("running");}
}