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 the GregorianCalendar class?

2 Answers   HCL,

what is Portal(web based online portal)?

2 Answers   AIG,

why java does not have operator overloading?

2 Answers  

what is polymorphism with example?types of polymorphism?

15 Answers   HP, Sigma Solve,

What is the difference between Checked and Unchecked exception? Give some examples

2 Answers   Ness Technologies,

What are wrapper classes?

11 Answers   Noah Solutions,

If all the methods in abstract class are declared as abstract then what is difference between abstract class and in interface?

8 Answers   Infosys, Synechron,

Hi friends i want display Triangle shap stars(*) please can tell me any one java code logic? * *** ***** ******* Like this

2 Answers   TCS, NIIT,

when,where and how to use abstract class and interface

2 Answers  

System.out & System.in are final static data member of System class but we can change there reference through setOut() & setIn() method how...

2 Answers   TCS, FCS,

Adapter classes?

3 Answers  

What is Transient and volatile

10 Answers   iFlex,