HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
1. Feel Free to ask and submit anything on and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.

corejava-OOPs: What is the difference between IS-A and HAS-A relationship?

We can maintain the relation between classes in two ways IS-A and HAS-A. Kindly Explain me with proper example.

corejava x 353
OOPs x 49
Posted On : 2013-11-22 19:49:06.0
profile Saksham Kumar - Saksham Kumar


Inheritance represents the IS-A relationship.

************************           Syntax of Inheritance         ***************************

class Subclass-Name extends Superclass-Name  
  //methods and fields  

The keyword extends indicates that you are making a new class that derives from an existing class. In the terminology of Java, a class that is inherited is called  super-class. The new class is called  subclass.
************************             Simple example of Inheritance         ************************

Inheritance IS-A -

As displayed in the above figure, Programmer is the subclass and Employee is the superclass. Relationship between two classes is Programmer IS-A Employee.It means that Programmer is a type of Employee.

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);  
==========================            Output           =============================
Programmer salary is:40000.0
Bonus of programmer is:10000
In the above example Programmer object can access the field of own class as well as of Employee class.


Aggregation represents HAS-A relationship.

If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A relationship.

Consider a situation, Employee object contains information such as id, name, email etc. It contains one more object named address, which contains its own information such as city, state, country, zip code etc. as given below.

class Employee{  
int id;  
String name;  
Address address;//Address is a class  
In such case, Employee has an entity reference address, so relationship is Employee HAS-A address.

**********************            Example of  HAS-A relationship          *************************

Aggregation HAS-A -

In the following example, Employee has an object of Address, address object contains its own information such as city, state, country etc. In such case relationship is Employee HAS-A address.

=========================                ==========================

public class Address {  
String city,state,country;  
public Address(String city, String state, String country) { = city;  
   this.state = state; = country;  

=========================                ==========================

public class Emp {  
int id;  
String name;  
Address address;  
public Emp(int id, String name,Address address) { = id; = name;  
void display(){  
System.out.println(id+" "+name);  
System.out.println(" "+address.state+" ";  
public static void main(String[] args) {  
Address address1=new Address("gzb","UP","india");  
Address address2=new Address("gno","UP","india");  
Emp e=new Emp(111,"varun",address1);  
Emp e2=new Emp(112,"arun",address2);  

Posted On : 2013-11-22 20:56:57
Satisfied : 1 Yes  0 No
profile Rishi Kumar - Rishi Kumar
Reply This Thread

Post Answer
Please Login First to Post Answer: Login login with facebook -