Relationship between Classes

As one starts learning any OOP(Object Oriented Programming) language be it C++, Java, Python etc, one of the concepts which most people fail to understand or go through it superficially is ‘Relationship between Classes’.

In this post I will be covering the following –
· What is Relationship between classes ?
· Why to use it ?
· Types of Relation b/w classes.


If you are reading this post, I am considering that you’ve some knowledge of ‘object’ and ‘classes’. When I was learning Java, I also overlooked this topic, but eventually came to know that this is the backbone of any OOP language. I insist you to please read this post until last and I assure you that you will have a strong hold on this concept.


NOTE- One more thing I must tell you is all the explanation would be in java syntax but the concept is applicable in any programming language(OOP)


WHAT IS RELATIONSHIP b/w CLASSES ?
When two or more classes share their property with one another, then the classes are said to be in some kind of relationship. Properties are data members and member functions.


WHY TO USE RELATIONSHIP b/w CLASSES ?
· To have code reusability in our project.
· Cost cutting(reduces the cost of buying similar resources).
· Reduce Redundancy (repetition) of data.


TYPES OF RELATION b/w CLASSES
1 Inheritance (IS-A Relation)
2 Association (HAS-A Relation)

  • Aggregation (weak bonding b/w classes)
  • Composition (strong bonding b/w classes)

INHERITANCE (IS-A Relation)
· It is a type of relation in which sub-class/child class/derived class possesses the property of its superclass/parent class/base class.
· Properties inherited are data members and member functions except private members and constructors. Inheritance is also known as IS-A relationship.
· Inheritance is achieved using ‘extends’ keyword followed by name of parent/super class.


Let’s understand it with an example,

class Vehicle {
String model;
int price;
public void get(String model, int price) {
this.model = model;
this.price = price;
}
public void show() {
System.out.println("Car Name : " + model + "\nPrice = " + price);
}

class Car extends Vehicle{
String color;
public static void main(String args[]) {

Car c = new Car(); // object of class Car
c.get("C-21",550000); // method inherited from
Vehicle(code reusability) c.color = "Red";
System.out.println("\n" + c.show() + "colour = " + c.color);
}}
OUTPUT -

Car Name : C-21
price = 550000
colour = Red

Here we can see that Car class use properties of Vehicle class along with its own, so we say that Car class is in a relationship with Vehicle class. In programming language it is said as Car IS-A Vehicle as Car class directly uses property of Vehicle class (single level inheritance).
Here we can observe code reusability as we need not define get() and show() methods again in Car class (child class) rather it is being inherited from Vehicle class (parent class).

Real world example -
class Fruit {}
class Mango extends Fruit {}
main() {
Fruit f = new Mango(); // Mango IS-A Fruit
Mango m = new Mango(); // Mango IS-A Mango
// Mango m1 = new Fruit(); // compilation error Fruit IS not A Mango
}

OOP concept offers us many types of inheritance, not going into details of it I am just listing them, detailed and precise explanation is available on the internet.


1 Single level inheritance. (as discussed in above example)

parent <— child (child inherit properties from parent)

2 Multi-level inheritance.

Grandparent <— parent <— child

3 Multiple inheritance.(not supported in java directly through
classes.)
4 Hierarchical inheritance.
5 Hybrid inheritance.


LIMITATION OF IS-A RELATION :
· Since child class inherits the properties of parent class, both the classes are said to be tightly-coupled. It means that if some changes are made in parent class then it would also reflect in child class as well.
· In IS-A relation all properties are inherited although the client may just want some.
So to overcome these limitations programmers prefer to use another type of relation i.e, ‘association relationship b/w classes’.

ASSOCIATION (HAS-A Relation)
In this type of relation properties of a class are used in other class by passing its reference.
Let’s understand it with the help of an example,

class Student {
String studentName = "ADITYA";
String studentId = "CS-123";
public void get() {}
public void show() {}
}
class College {
String name;
String address; 
int facultyCount;
String studentName;
String studentId;
Student std; // reference of Student class created
public void get(String name, String address, int facultyCount) {
std = new Student(); // object of Student class created
this.name = name;
this.address = address;
this.facultyCount = facultyCount;
studentName = std.studentName; // using properties of Student class studentId = std.studentId;
}
public void show() {
System.out.println("Name of college : " + name + "\naddress : " +
address + "\nFaculty Count : " + facultyCount + "\nStudent Name : " +
studentName + "\nStudent ID : " + studentId);
}}

Here as we can see that College class uses property of Student class but in a different way than inheritance. College class contains reference of Student class. So the relationship is known as HAS-A relation, in programming language it is said College HAS-A Student. Now the
programmer can use properties of Student class in College class by creating object.
College class doesn’t require all the property of Student class, rather it requires only the studentName and studentId. In HAS-A relation programmer can use properties depending upon the need and this is the biggest advantage of using this type of relation.
Also any changes made in a class does not affect another class which helps to maintain the integrity of individual classes.

Similarly considering another example,

class Engine {}
class Vehicle {
Engine e = new Engine(); // Vehicle HAS-A Engine
}


AGGREGATION & COMPOSITION
Aggregation & composition are part of association.When two classes are weakly bonded(related) then the relationship is known as aggregation relation. Whereas when two classes are strongly bonded (related) then the relationship is known as composition relation.
Weak-bonded relation – If two classes are related with HAS-A relation, and existence of one class does not affect the existence of other then the two classes are said to be weakly bonded.

Example,
class MusicPlayer {}
class Vehicle {
MusicPlayer mp; // Vehicle HAS-A MusicPlayer
}


Here even if either of the class does not exist, yet classes exist individually. It is not mandatory that every vehicle needs to have a music player and vice versa.
String-bonded relation – If two classes are related with HAS-A relation and existence of one class affects the existence of other then the two classes are said to be strongly bonded.

Example,
class Engine {}
class Vehicle {
Engine e; // Vehicle HAS-A Engine
}


Here if either of the class does not exist, classes have no value individually. It is mandatory that every vehicle needs to have an engine and vice versa.Without a car engine has no value and without the engine car does not work.



Categories: JAVA

Tags:

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: