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

I do not understand how to do this? can someone help me?

Write a code in java to explain the while loop in Java? // This is the Hello program in Java class Hello { public static void main (String args[]) { System.out.print("

write a java program to test for class Person Student faculty Employee

Determine the Java Compiler Structure Source code for every class is in a .java file. Compile every class to produce -.class? file. Sometimes, multiple .class files are pack

Separating your business logic with other functionality such as services, Loggers etc, making classess more cohesive.

String is immutable in java : you can't change a string object but can replace it by creating a new object instance. Creating a new instance is rather expensive.   //Non ef

What is the use of UnicastRemoteObject in RMI? Ans) The UnicastRemoteObject class gives support for point-to-point active object references using TCP streams. Objects that need

Elaborate the ‘+ ' operator and its constraints along with the help of examples? The + Operator Used on Strings. The + operator could also be used to add string variables or te

Want an expert with knowledge of UML diagrams and writing experience for pages writing including diagrams. This project needs 3-4 pages of technical writing about tickets reserv