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




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

OOOOOO!NOooooooooo.

is a relationship is (inheritence)

has a relationship is (composition or Assocation)

Example:
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
and
has-a represents the delegation/association
for example
Chair is a type of Furniture
and
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
Engineer.


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.
Eg.
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

Explain Global variables in Packages?

4 Answers  


what is multithreading?

5 Answers   Virtusa,


what models are available for event handling?

1 Answers  


What is yielding and sleeping? how they different?

2 Answers  


byte a=5; byte b=5; byte c=a+b; System.out.println(c); whats the o/p?

7 Answers   NIIT, Wipro,


what is the form of storage space in java?

5 Answers  


explain System.out.println

107 Answers   IBM, Cognizant, Oracle, IBS, CTS, TCS, Calpine Technologies, Care, LibSys, Spiro Solutions,


what is the diff between Servletcontext and servletconfig?

5 Answers  


Can you make an instance of an abstract class?

5 Answers   Accenture,


What advantage does an entity bean have over a sessin bean?

3 Answers   Adobe,


When finalize method is called?

3 Answers  


Tell me the Importent classes in net package?

0 Answers  




Categories