Illustrate inheritance and composition?, JAVA Programming

The 'is a' relationship is used with inheritance and 'has a' relationship is used with composition. Both composition and inheritance allow you to perform sub-objects inside your new class. Two of the main techniques for code reuse are object composition and class inheritance.

378_Untitled.png

 

Inheritance is single-directional. For example man is a human. But human is not a man. Inheritance needs extends key word. Composition: is used when Man has an eye. It is incorrect to say man is an eye. Composition simply defines using instance variables that refer to other objects. The class man will have an instance variable, which refers to an eye object.

 

 Which one to favor, inheritance or composition? The guide is that inheritance could be only used when subclass 'is a' superclass.

1. Don't use inheritance just to get programming code reuse. If there is no 'is a' relationship then require composition for code reuse. Overuse of implementation inheritance will divide all the subclasses, if the superclass is changed.

 

2. Do not add inheritance just to get polymorphism. If there is no 'is a' relationship and all you need is polymorphism then use interface inheritance with composition, which provides you code reuse.

 

Posted Date: 7/25/2012 12:49:02 AM | Location : United States







Related Discussions:- Illustrate inheritance and composition?, Assignment Help, Ask Question on Illustrate inheritance and composition?, Get Answer, Expert's Help, Illustrate inheritance and composition? Discussions

Write discussion on Illustrate inheritance and composition?
Your posts are moderated
Related Questions
how can i implement Mid-Square hashing?

Sal?s Pizza wants to install a system to record orders for pizza and Buffalo chicken wings. When regular customers call Sal?s Pizza on the phone, they are asked their phone number.

An introduction permits the user to add new methods or attributes to an existing class. This can then be explained to an existing class without having to change the structure of th

Illustrate the parser method The below table summarizes the parser methods available to a java programmer.

what is mearnt by a variable in java

Question: (a) (i) Describe the purpose of comments when writing programs in Java. (ii) Name the types of comments available and illustrate with simple examples. (b) (i)

Need Java/JNLP Application - GUI and Backend Projects Project Description: I am looking for assistance in making changes to a complex java application. System launches fro

How many kinds of errors can be found during a program development? List them. 1. Syntax errors 2. Semantic errors 3. Run-time errors

How to Implementing Interfaces in java? To actually utilize this interface you create a class that involves a public double calculateTariff() method and declare in which the cl

Luminous Jewels - The Polishing Game Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various lum