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

Difference between Superclass and Subclass?

7 Answers  


Hi Friends, I am beginner in java. what i know about synchonized keyword is,If more that one 1 thread tries to access a particular resource we can lock the method using synchronized keyword. Then after that how the lock is released and how next thread access that.Please explain with example.

5 Answers  


all are saying java doesn't support multiple inheritance but by default Object class is super class for all the user defined classes and we can extend atmost one class so each class can extend more than one class so java supports multiple inheritance?i am confused with this,pls any one explain me.

4 Answers  


AWT event listeners extends what interface?

1 Answers  


why static class in java or what is use of static class in java

3 Answers   SunGard,






how to handle exceptions in core applications?

2 Answers   CSS,


How can i add a button in applet in java ???

2 Answers  


Can you have an inner class inside a method and what variables can you access?

2 Answers  


What is actual difference between statement,prepared statement and callable statement and when we have to use it? pls post a answer with code and clear explanation. thanks

2 Answers  


What are access modifiers in Java ?

6 Answers  


What is the purpose of premetive data types in java?

2 Answers   TCS, Merrill Lynch,


why should we get the following error ? Exception in main method NoClassDefFoundError:classname could anyone give the detail clarification on how java compiler can look for .class file?

2 Answers  






Categories