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
Explain The java.net package Each package describes a number of classes, interfaces, exceptions, and errors. For instance, the java.net package contains these, classes, interfa

A target is the class that is being advised. The class can be a third party class or your own class to which you require to add your own custom behavior. By using the concepts of A

What is the problem along with Relational Database and what solution you could suggest for it? When we store object orientated data within RDBMS it required to translate in to

Your first task is to implement the Movie class to store all the relevant information on a single DVD Movie, including its catalog number, title, year of release, rating, rental pe

What is the difference between a while statement and a do while statement? A while statement checks at the starting of a loop to see whether the next loop iteration should occu

Ask1. Storing the Array: Write an application that uses an Array to store 10messages of type String. You will store this Array with 10 messages of your choosing. For example, a mes

In this assignment, you will create 5 objects and get them to interact together. You will create theatres for which patrons will buy tickets from a box office to watch movies.

How do I write a java program that enters a six digit number and takes the sum of the six digits?

Develop a Adobe Air Native Extension Project Description: We are seeking someone that must create an adobe native extension for the subsequent SDK: Develop a Adobe Air Nat

calculate the size of the tract of land by the size of an acre to get the number of acres.