What is the is a and has a relation ship in oops concept in

Answers were Sorted based on User's Feedback

Answer / sandya

The relationships are the structure of the object. has-a
relationship can be described in Java code as member fields.
is-a relationship can be described in Java keyword extends.
The is-a relationship has an inheritance feature (like
"extends" or "implements") and has-a relationship has an
encapsulation feature (like private or protected modifier
used before each member field or method).

Let's look at one example. Translate the following perfect
sentences into Java code.

1. Pizza is a food. A Pizza has ingredients, a cost, and
a price.

public class Pizza extends Food //is-a relationship
int ingredients;//has-a relationship
double cost;
double price;

Is This Answer Correct ?    148 Yes 13 No

Answer / raghava


is a relationship is (inheritence)

has a relationship is (composition or Assocation)

Has a relation

public class Address
String street;
String city;



public class Person
String name;
Address address; //has a relationship

Is This Answer Correct ?    82 Yes 6 No

Answer / vashisht rakesh

Yea you right.
is-a represent the inheritence
has-a represents the delegation/association
for example
Chair is a type of Furniture
Furniture has a relationship with chair

Is This Answer Correct ?    33 Yes 5 No

Answer / kishor patil

Inheritance defines the relationship is-a(also called the
superclass-subclass relationship) between a superclass and
it's subclasses. This means that an object of a subclass
is-a superclass object,and can be used wherever an object
the of superclass can be used.

For EX-Suppose person is a superclass of Teacher
class,Driver class,Engineer class.

Here we can say every Teacher is person,Every Driver is
a person,And Every Engineer is a person ,but vice -versa
not true i.e Every person can not be teacher or driver or

Aggregation Defines the relationship has-a(also called
whole-part relatioship)between an instance of class and
it's constituents(also called parts).Aggregation comprises
the usage of objects.

We can say that a composite object built from the
constituent objects that are its parts .

For EX- Vehical is class.

vehical has a wheel,
vehical has a engine,
vehical has a stearing ,
vehical has a gear.

means vehical has a parts like
wheel,engine ,stearing ,gear.

Is This Answer Correct ?    24 Yes 2 No

Answer / b venkat reddy

The "is a" relationship is expressed with inheritance and
"has a" relationship expressed with composition.

Both inheritance and composition allow u to place sub-objects inside your new class.Two main techniques to code reuse are inheritance and composition.

Ex.: IS A ===>> house IS A building

class House {
class Building extends House{

Ex.:HAS A ===>> house HAS A bathroom

class House {
Bathroom b = new Bathroom();

Is This Answer Correct ?    22 Yes 5 No

Answer / gopid123

Ya correct there is relation ship. that is the main concpt
in java

Is This Answer Correct ?    37 Yes 34 No

Answer / crosseyed

Answer 4 and Answer 1 might both give simialr outputs but
their adoption depends on the size of a persons analytic
universe. And answer 4 is the better way to solve most
problems because it is easier to write and modify.

Much code is part of a system describing just the tiny part
of the universe an enterprise wishes to describe. If that
enterprise is a single product pizza manufacturer then for
such an enterprise the relevant universe is only pizza and
no other category of food exists to be extended in software
(profitably) by the manufacturer that is not some form of
pizza. The software engineer might wish to be a
nomenclature trailblazer, extending his code ever outward
in describing the whole universe of food. But pizza makers
are far too stingy to pay for trailblazing.

On the other hand if the enterprise is a supermarket then
the class food might (but shouldn't) be seperated from
classes for school supplies, medications and so on and
extended to include types of food. But is bagel pizza
really pizza? What about pizza crusts? Pizza crusts with
tomato sauce but no other topping? What about club soda
marketed as a cleaning supply? What about mouthwash
marketed as a cure for an alcoholics DT's? Where will we
draw the lines?

If in either case you were to use inventoryItem as a class
and add only properties sufficent to perform an inventory
you may not be trailblazing the catalogue of the worlds
foods and other stuff, but you will have much more easily
written and modified code.

World class descriptions of the universe may be a grand
ideal - but sometimes all you need do is count the number
of pepperoni pizzas! Describe only what is needed for a
purpose. Tofu, after all, can taste like fish or foul and
even Plato might not be able to categorize all its
variations depending on his ability to percieve spices!
But a store has only to tell how much of the various
packages they own sells best and how many of those are left.

Is This Answer Correct ?    3 Yes 10 No

Answer / yogeshwar

is a
Relationship between a Class and its object.
Class Car { ... }

Car maruti800=new Car();

here maruti800 is a car.
Relationship between Car and maruti800 ==> is a

the Person and Address is a suitable example for has a

Is This Answer Correct ?    2 Yes 11 No

Answer / guest

is a nothing but inheritance and has a nothing but extends

Is This Answer Correct ?    15 Yes 103 No

Post New Answer

More Core Java Interview Questions

What is anonymous class?

2 Answers  

Why operator overloading is not in Java?

0 Answers  

how to use crystal reports in java

2 Answers  

How to decrease number of hashings in has

1 Answers  

what is meant by multicast?

1 Answers  

What services that container provides?

2 Answers  

Hi Friends, can you give difference between extending thread class and implementing runnable interface.

4 Answers  

4.1 Supply contracts (in the form of comments specifying pre- and post conditions) for the enqueue() method of the LinkedQueue class given in the Appendix. (2) 4.2 Let Thing be a class which is capable of cloning objects, and consider the code fragment: Thing thing1 = new Thing(); //(1) Thing thing2 = thing1; //(2) Thing thing3 = (Thing) thing1.clone(); //(3) Explain how the objects thing2 and thing3 differ from each other after execution of the statements. (

0 Answers  

What are event-delegation model and event-inheritance model? Which is best?

1 Answers  

Every class extends object but why it is not possible for every object to invoke clone() method. ideally protected methods should be accessible from sub classes. isn't it?

2 Answers  

What is dynamic binding?

2 Answers  

Default layout of panel and frame?

3 Answers