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
What is "My Personal Agent"? My personal agent is a computer programme which work automatically and have voice interface.

What are Access Specifiers available in Java? Java has four access specifiers, which are enlisted below: Public: public methods, classes and fields can be accessed by ever

Assignment 3 Project Proposal Documentation Description Marks out of Wtg(%) Due date Assignment 3 100 35% 15/09/14 In the assignment we will start analysing and designing the

Mention any five applications of Artificial Intelligent System? 1)Medical Diagnosis 2)Robots 3) Games 4) Business intelligence 5)Image Recognition.

Explain continue statement with example? It is sometimes essential to exit from the middle of a loop. Sometimes you'll need to begin over at the top of the loop. Sometimes you'

Task 1 Create a new project in Eclipse called Assignment 1. Within this project create a package called task01. 1/ Download the class Date (you must use this class - no

What happens to the static fields of a class during serialization? There are three exceptions in which serialization does not necessarily read and write to the stream. These

Need Simple Desktop Software I am seeking a software that can do the subsequent: 1. Check position for a URL in Google, Yahoo, Bing. 2. Check position for a YouTube video

Project Description: Two co-founders are in search of a developer to join us to launch a tech startup. We have experience at fortune 500 companies, elite consulting firms, etc.,

What is PortableRemoteObject.narrow() method and what is used for?                Ans) Java RMI-IIOP gives a mechanism to narrow the the Object you have received from from your