answersLogoWhite

0


Best Answer

The 'is a' relationship is expressed with inheritance and 'has a' relationship is expressed with composition. Both

inheritance and composition allow you to place sub-objects inside your new class. Two of the main techniques for

code reuse are class inheritance and object composition.

Inheritance [ is a ] Vs Composition [ has a ]

Building

Bathroom

House

class Building{

.......

}

class House extends Building{

.........

}

is a [House is a Building]

class House {

Bathroom room = new Bathroom() ;

....

public void getTotMirrors(){

room.getNoMirrors();

....

}

}

is a has a [House has a Bathroom]

has a

Inheritance is uni-directional. For example House is a Building. But Building is not a House. Inheritance uses

extends key word. Composition: is used when House has a Bathroom. It is incorrect to say House is a

Java - Fundamentals

19

Bathroom. Composition simply means using instance variables that refer to other objects. The class House will

have an instance variable, which refers to a Bathroom object.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between composition and inheritance
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between composition and inheritance?

Composition & inheritance are totally different from each other. Composition means to compose the properties by code of same class. In composition we can refer to some other class who hold the same properties but we have to code, we can not directly use that code ,we have to rewrite . Whereas in Inheritance, whenever a class uses the properties of another class and that class has its own properties too then the process of accessing the properties of another class is known as Inheritance... If we think as real life example, If we are inspired by someone, we adore the properties of that person then we add that quality of person in ourselves ,that is composition. But some quality of our parent are automatically in us there is no need to strive, that is inheritance. (sdrishti7@gmail.com)


What is the difference between composition and essay writing?

Essay is a type of composition....


What is the difference between granite and gabbro?

the composition


Is there a difference between estate being willed to you or inheritance?

No. Property that you receive by a will IS an inheritance. Property received from a relative under the laws of intestacy when there was no will is also an inheritance.


What is the difference between sex-linked and maternal inheritance?

they have different meanings


What is the difference between oodbms and ordbms?

on the basis of design, inheritance, relationship


What is the difference between pleiotropy and polygenic inheritance?

tatti kha meri kutey


What is the difference between the composition and texture of a?

texture is how something feels


What is the difference between a liquid and its chemical composition?

A liquid is a compound or a mixture; the chemical composition is representative for this liquid.


What is the difference in composition between a dacite and an andesite?

Dacite is more felsic in composition, containing more silica.


Do you have to pay inheritance tax if it was passed from your grandparents to you?

I assume you know the difference between an inheritance tax and an estate tax? Only a few states have an inheritance tax and the answer would depend on the state.


What is the difference between cytosplasmic and mendelian inheritance?

To make this simple cytoplasmic inheritance is the inheritance of genes in organelles such as mitochondria that do not go through regular mitosis which is Mendelian inheritance. It is a bit more complex than this and can be easily Googled.