answersLogoWhite

0

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

14y ago

What else can I help you with?

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


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 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 between death tax and estate tax?

Inheritance taxes and estate taxes differ only in who pays and to whom the tax is paid. Learn the differences between inheritance and estate taxes.


What is the difference between inheritance and earnings?

Inheritance is what you get if someone who dies has decided that you should have some of his/her belongings. Earnings are typically your salary, the pay you get for having done some work.


Difference between technical writing and composition writing?

The difference between technical writing and composition writing is that the technical writing deals with writing off on the basis of rules that must be thoroughly proofread, while composition writing deals with writing from your imagination, with no guidelines or rules.


What is the difference between a piano composition and a song?

songs are usually considered to have words.


What is difference between paragraph and composition?

Paragraph is based on one thought or one idea. However, a composition made up of on the paragraphs.


What is the difference between adults and kids sunscreen?

The difference between sunscreen for adults and children is generally the chemical composition. A child's sunscreen will be milder for the skin and will not burn the eyes.


What is the Difference between carbon composition and wire wound resistors' wattage-rating?

sumthing ee


What is composition of objects in a class known as?

It's known as a composition or composite object, however the term aggregate can also be used.