Home » Interview Question: IS-A & HAS-A Relationship

Interview Question: IS-A & HAS-A Relationship

by Mahesh More
62 views
ISA vs HAS A relationships

Hey, Tea Lovers! Today let’s look at the most asked interview question, IS-A & HAS-A relationship. Both beginners and experienced persons get this question in their interviews. This post is one of the many in the series of Interview Question, where we don’t just hang you with one line answer, rather try to explain it so that it gets fit into your head.

I know you are in a hurry to prepare for the interview, so I will make this post as short as possible, So prepare your cup of tea to sip and code.

What is IS-A And HAS-A Relationship

IS-A And HAS-A is a relationship between objects. This helps to design applications with a maximized readability and portability and code reusability.

An IS-A relationship is referred to the Inheritance and the Has-A relationship is referred to as Compositions.

IS-A Relationship between Objects

Inheritance represents the IS-A relationship, which is also known as the Parent-Child relationship.

IS-A relationship is a way of saying: This object is a type of that object

As displayed in the above figure, the Manager is the subclass and the Employee is the superclass. The relationship between these two classes will be an IS-A relation i.e Manager IS-A Employee. It means that the Manager is the type of Employee class. This also applies to Interface’s implementation.

Code

class Employee {
	String employee_id="EMP_047";
	int phoneNumber=987654321;
	String emailId="[email protected]";
}
public class Manager extends Employee {
	int salary=10000;
	int bonus=1000;
	public static void main(String args[]){
		Manager manager = new Manager();
                // inherited values
		System.out.println("Employee Id : " + manager.employee_id);
		System.out.println("Phone Number : " + manager.phoneNumber);
		System.out.println("Email Id : " + manager.emailId);
                // manager class own values
		System.out.println("Salary : " + manager.salary);
		System.out.println("Bonus : " + manager.bonus);
	}
}

Output

Employee Id : EMP_047
Phone Number : 987654321
Email Id : [email protected]
Salary : 10000
Bonus : 1000

In the above example, the Manager object can access the field of own class as well as of Employee class i.e. code reusability.

HAS-A Relationship between Objects

HAS-A relationships between objects mean the use of instance variables that are referenced to other objects. Simply put, using other classes object. The composition represents the HAS-A relationship. For example, Car has an Engine, or College has a Student.

Let’s Discuss some example, Consider a situation where the Employee bonus has to calculate based on appraisal rating. Here we have an Employee object which contains information such as id, appraisal rating, etc. It also contains one more object named Rule Engine containing the business logic that calculates the bonus amount.

In the above diagram, Employee has an object of RuleEngine containing the business logic that calculates the bonus amount. The Employee has a RuleEngine.

In the reverse case, where an Employee is an object within RuleEgnine, it would have been called as RuleEgnine has an Employee.

Code

class RuleEngine {
	public int getBonusAmount(int appraisalRating) {
	    if(appraisalRating >= 8 ) {
	    	return 8000;
	    }
	    else if(appraisalRating >= 4 ) {
	    	return 5000;
	    }
	    return 3000;
	}
}
public class Employee {
	private String employee_id;
	private int appraisalRating;
	private RuleEngine ruleEngine;
	public Employee(String employee_id, int appraisalRating) {
		this.employee_id = employee_id;
		this.appraisalRating = appraisalRating;
		this.ruleEngine = new RuleEngine();
	}
	@Override
	public String toString() {
		return "Employee [employee_id=" + employee_id + ", "
				+ "appraisalRating=" + appraisalRating + ", "
						+ "BonusAmount=" + ruleEngine.getBonusAmount(appraisalRating) + "]";
	}
	public static void main(String[] args) {
		for (int i = 0; i < 15; i++) {
			Employee employee = new Employee("EMP_0"+i, i);
			System.out.println(employee);
		}
	}
}

Output

Employee [employee_id=EMP_00, appraisalRating=0, BonusAmount=3000]
Employee [employee_id=EMP_01, appraisalRating=1, BonusAmount=3000]
Employee [employee_id=EMP_02, appraisalRating=2, BonusAmount=3000]
Employee [employee_id=EMP_03, appraisalRating=3, BonusAmount=3000]
Employee [employee_id=EMP_04, appraisalRating=4, BonusAmount=5000]
...

Composition or Inheritance: Which one to use?

IS-A or Inheritance should use only if both the object relationship will be maintained for the full lifespan.

HAS-A should be used when the objects are not of the same type or no inheritance/IS-A relationship is possible.

There is a saying that,

Composition over Inheritance .

It requires its own debate and explanation. But simply put, the composition has more advantage in terms of flexibility, readability and most of the design patterns embrace the composition. It does not mean don’t use inheritance at all, but a great mixture of these two will have more impact on the code, a good one that is.

Difference between IS-A & HAS-A Relationship

Sr No.IS-AHAS-A
1IS-A simply means InheritanceHAS-A simply means the Composition / Reference
2IS-A is a way of saying that this object is a type of that objectHAS-A object instance of a class has a reference to an instance of another class
3IS-A objects should be the same typeHAS-A can use a different type of objects
4IS-A basically a Generalization / Specialization relationship in OOPSHAS-A is an Association / Composition relationship
5IS-A / Inheritance is Static binding (compile-time binding)HAS-A / Composition is Dynamic binding (run-time binding)
Difference between IS-A & HAS-A Relationship

Conclusion

That’s it for this post. We looked at what is IS-A & HAS-A relationship, the differences between them, and many more tips on when and what to use.

This post is one of the many in the series of Interview Question, where we don’t just hang you with one line answer, rather try to explain it so that it gets fit into your head.

You can learn more about other Java features such as Stream APIBest PracticesFunctional Programming.

The full source code can be found on GitHub and the Full project is available here.

Please feel free to suggest any changes, such as additional info, or if you have any questions on some points let us know in the comments. We will be happy to help you.

See you in the next post and good luck for your interview. HAKUNA MATATA!!!

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy