Hey, Tea Lovers! Today let’s look at the most asked interview question, IS-A & HAS-A relationship. Both beginners and experienced people get this question in their interviews. This post is one of the many in the series of Interview Questions, where we don’t just hang you with one line answer, but rather try to explain it so that it gets fits into your head.
I would be happy to connect with you guys on social media. It’s @coderstea on Twitter, Linkedin, Facebook, Instagram, and YouTube.
Please Subscribe to the newsletter to know about the latest posts from CodersTea.
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 are IS-A And HAS-A Relationship
IS-A And HAS-A are relationships between objects. This helps to design applications with 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="example@gmail.com";
}
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);
}
}
Code language: JavaScript (javascript)
Output
Employee Id : EMP_047
Phone Number : 987654321
Email Id : example@gmail.com
Salary : 10000
Bonus : 1000
Code language: JavaScript (javascript)
In the above example, the Manager object can access the field of its class as well as of the 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 class objects. The composition represents the HAS-A relationship. For example, a Car has an Engine, or College has a Student.
Let’s Discuss some examples, 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);
}
}
}
Code language: JavaScript (javascript)
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]
...
Code language: CSS (css)
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 debate and explanation. But simply put, the composition has more advantages in terms of flexibility, and 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-A | HAS-A |
1 | IS-A simply means Inheritance | HAS-A simply means the Composition / Reference |
2 | IS-A is a way of saying that this object is a type of that object | HAS-A object instance of a class has a reference to an instance of another class |
3 | IS-A objects should be the same type | HAS-A can use different types of objects |
4 | IS-A is a Generalization / Specialization relationship in OOPS | HAS-A is an Association / Composition relationship |
5 | IS-A / Inheritance is Static binding (compile-time binding) | HAS-A / Composition is Dynamic binding (run-time binding) |
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 Questions, where we don’t just hang you with one line answer, but rather try to explain it so that it gets fits into your head.
You can learn more about other Java features such as Stream API, Best Practices, and Functional 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 with your interview. HAKUNA MATATA!!!
I would be happy to connect with you guys on social media. It’s @coderstea on Twitter, Linkedin, Facebook, Instagram, and YouTube.
Please Subscribe to the newsletter to know about the latest posts from CodersTea.