<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ttbrown4</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ttbrown4"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Ttbrown4"/>
	<updated>2026-07-14T05:47:35Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54770</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54770"/>
		<updated>2011-11-07T14:29:41Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=When to use Inheritance: Lecture 17=&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
'''&lt;br /&gt;
== Different Perspectives on Inheritance ==&lt;br /&gt;
In this article we will consider different perspectives on when to use inheritance and when not to use inheritance. Let us look at the following reasons that are considered when using inheritance:&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. el al, 2005 &amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process: &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software assets appropriately&lt;br /&gt;
#* Package names should be a category classification that encapsulating all the data&lt;br /&gt;
#* Interface names should be an unifying theme that groups classes together &lt;br /&gt;
#* Class names should be proper nouns&lt;br /&gt;
#* Method names should be simple action verbs&lt;br /&gt;
# Being consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
The items listed above are not a comprehensive guideline for the proper implementation of inheritance through code reuse.They are merely provided as a starting point for a programmer to consider during the preliminary states of software development. Any although inheritance provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application. For instance the below example displays the guideline presented in the above list.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// The following code is used at a small organization to determine what the &lt;br /&gt;
// shipping cost of their packages are. &lt;br /&gt;
// This code demonstrates how inheritance can be used effectively to expand the &lt;br /&gt;
// functionality of a given program. &lt;br /&gt;
// Also observe the how the above guideline are followed. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IS-A ===&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;lt;ref&amp;gt; When to Use Inheritance &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&amp;lt;ref&amp;gt;  &lt;br /&gt;
Inheritance - Proper Inheritance and Substitutability&amp;lt;/ref&amp;gt;&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]&amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
=== Public Interfaces ===&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java: The Complete Reference&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt;Software Engineering Observations &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new Rectangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Even a class as simple as this one has problems. Consider what happens when a user leverages&lt;br /&gt;
// inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Just having a similar interface is not sufficient for subclass/superclass or class/interface relationship; consistent behavior is also required. In particular, a sub class should not modify the behavior of the super class.Consider the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle] &amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;to make a class B a subclass of class A or to make  an implementer of interface A.&lt;br /&gt;
If class B models a role played by class A, especially a temporary role, then B should not be a subclass of A. Instead referencing should be used.&lt;br /&gt;
Inheritance between a superclass A and a subclass B is appropriate if it promotes code reuse,each object of the sub class  “is an” object of the super class, and the public interface of class B includes the interface of class A and the behavior of the methods in this interface in both classes is similar in both classes.&lt;br /&gt;
Inheritance, especially a deep inheritance tree  is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it hard to code and to follow the flow of execution.If you have a class with behavior that applies to only some of the objects of the class, then con-&lt;br /&gt;
sider splitting the class into two classes associated by inheritance either directly or through a common abstract class or interface. When designing a class B that will be very similar to an existing class A, use referencing if you want the functionality of A but not the interface,and use inheritance only when you want the functionality and interface of A.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&lt;br /&gt;
&lt;br /&gt;
Tips for Effective Software Reuse. http://www.infoq.com/articles/vijay-narayanan-software-reuse, Oct. 2011.&lt;br /&gt;
&lt;br /&gt;
When to Use Inheritance. http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Inheritance — Proper Inheritance and Substitutability. http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Liskovs Substitution. Principle http://javaboutique.internet.com/tutorials/JavaOO, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Scildt, herbert. ''Java: The Complete Reference,'' McGraw Hill, 2007.&lt;br /&gt;
&lt;br /&gt;
Software Engineering Observations. http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
*http://en.wikipedia.org/wiki/Don't_Repeat_Yourself&lt;br /&gt;
*http://en.wikipedia.org/wiki/Software_library&lt;br /&gt;
*http://stackoverflow.com/questions/4348557/liskovs-substitution-principle-how-to-model-square-and-rectangle&lt;br /&gt;
*http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html&lt;br /&gt;
*http://www.dlugosz.com/Perl6/web/isa-inheritance.html&lt;br /&gt;
*http://inheritingjava.blogspot.com/2011/01/chapter-14-coupling-and-cohesion.html&lt;br /&gt;
*http://www.builderau.com.au/program/java/soa/Dynamic-Classification-with-Interfaces-and-Delegation/0,339024620,320269511,00.htm&lt;br /&gt;
*http://www.objectmentor.com/resources/articles/lsp.pdf&lt;br /&gt;
*http://home.cogeco.ca/~ve3ll/jatutor5.htm&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54769</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54769"/>
		<updated>2011-11-07T14:28:46Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Costs of Using Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=When to use Inheritance: Lecture 17=&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
'''&lt;br /&gt;
== Different Perspectives on Inheritance ==&lt;br /&gt;
In this article we will consider different perspectives on when to use inheritance and when not to use inheritance. Let us look at the following reasons that are considered when using inheritance:&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. el al, 2005 &amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process: &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software assets appropriately&lt;br /&gt;
#* Package names should be a category classification that encapsulating all the data&lt;br /&gt;
#* Interface names should be an unifying theme that groups classes together &lt;br /&gt;
#* Class names should be proper nouns&lt;br /&gt;
#* Method names should be simple action verbs&lt;br /&gt;
# Being consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
The items listed above are not a comprehensive guideline for the proper implementation of inheritance through code reuse.They are merely provided as a starting point for a programmer to consider during the preliminary states of software development. Any although inheritance provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application. For instance the below example displays the guideline presented in the above list.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// The following code is used at a small organization to determine what the &lt;br /&gt;
// shipping cost of their packages are. &lt;br /&gt;
// This code demonstrates how inheritance can be used effectively to expand the &lt;br /&gt;
// functionality of a given program. &lt;br /&gt;
// Also observe the how the above guideline are followed. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IS-A ===&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;lt;ref&amp;gt; When to Use Inheritance &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&amp;lt;ref&amp;gt;  &lt;br /&gt;
Inheritance - Proper Inheritance and Substitutability&amp;lt;/ref&amp;gt;&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]&amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
=== Public Interfaces ===&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java: The Complete Reference&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt;Software Engineering Observations &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Even a class as simple as this one has problems. Consider what happens when a user leverages&lt;br /&gt;
// inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Just having a similar interface is not sufficient for subclass/superclass or class/interface relationship; consistent behavior is also required. In particular, a sub class should not modify the behavior of the super class.Consider the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle] &amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;to make a class B a subclass of class A or to make  an implementer of interface A.&lt;br /&gt;
If class B models a role played by class A, especially a temporary role, then B should not be a subclass of A. Instead referencing should be used.&lt;br /&gt;
Inheritance between a superclass A and a subclass B is appropriate if it promotes code reuse,each object of the sub class  “is an” object of the super class, and the public interface of class B includes the interface of class A and the behavior of the methods in this interface in both classes is similar in both classes.&lt;br /&gt;
Inheritance, especially a deep inheritance tree  is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it hard to code and to follow the flow of execution.If you have a class with behavior that applies to only some of the objects of the class, then con-&lt;br /&gt;
sider splitting the class into two classes associated by inheritance either directly or through a common abstract class or interface. When designing a class B that will be very similar to an existing class A, use referencing if you want the functionality of A but not the interface,and use inheritance only when you want the functionality and interface of A.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&lt;br /&gt;
&lt;br /&gt;
Tips for Effective Software Reuse. http://www.infoq.com/articles/vijay-narayanan-software-reuse, Oct. 2011.&lt;br /&gt;
&lt;br /&gt;
When to Use Inheritance. http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Inheritance — Proper Inheritance and Substitutability. http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Liskovs Substitution. Principle http://javaboutique.internet.com/tutorials/JavaOO, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Scildt, herbert. ''Java: The Complete Reference,'' McGraw Hill, 2007.&lt;br /&gt;
&lt;br /&gt;
Software Engineering Observations. http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
*http://en.wikipedia.org/wiki/Don't_Repeat_Yourself&lt;br /&gt;
*http://en.wikipedia.org/wiki/Software_library&lt;br /&gt;
*http://stackoverflow.com/questions/4348557/liskovs-substitution-principle-how-to-model-square-and-rectangle&lt;br /&gt;
*http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html&lt;br /&gt;
*http://www.dlugosz.com/Perl6/web/isa-inheritance.html&lt;br /&gt;
*http://inheritingjava.blogspot.com/2011/01/chapter-14-coupling-and-cohesion.html&lt;br /&gt;
*http://www.builderau.com.au/program/java/soa/Dynamic-Classification-with-Interfaces-and-Delegation/0,339024620,320269511,00.htm&lt;br /&gt;
*http://www.objectmentor.com/resources/articles/lsp.pdf&lt;br /&gt;
*http://home.cogeco.ca/~ve3ll/jatutor5.htm&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54768</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54768"/>
		<updated>2011-11-07T14:22:09Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Different Perspectives on Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=When to use Inheritance: Lecture 17=&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
'''&lt;br /&gt;
== Different Perspectives on Inheritance ==&lt;br /&gt;
In this article we will consider different perspectives on when to use inheritance and when not to use inheritance. Let us look at the following reasons that are considered when using inheritance:&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. el al, 2005 &amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process: &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software assets appropriately&lt;br /&gt;
#* Package names should be a category classification that encapsulating all the data&lt;br /&gt;
#* Interface names should be an unifying theme that groups classes together &lt;br /&gt;
#* Class names should be proper nouns&lt;br /&gt;
#* Method names should be simple action verbs&lt;br /&gt;
# Being consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
The items listed above are not a comprehensive guideline for the proper implementation of inheritance through code reuse.They are merely provided as a starting point for a programmer to consider during the preliminary states of software development. Any although inheritance provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application. For instance the below example displays the guideline presented in the above list.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// The following code is used at a small organization to determine what the &lt;br /&gt;
// shipping cost of their packages are. &lt;br /&gt;
// This code demonstrates how inheritance can be used effectively to expand the &lt;br /&gt;
// functionality of a given program. &lt;br /&gt;
// Also observe the how the above guideline are followed. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IS-A ===&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;lt;ref&amp;gt; When to Use Inheritance &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&amp;lt;ref&amp;gt;  &lt;br /&gt;
Inheritance - Proper Inheritance and Substitutability&amp;lt;/ref&amp;gt;&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]&amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
=== Public Interfaces ===&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java: The Complete Reference&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt;Software Engineering Observations &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Just having a similar interface is not sufficient for subclass/superclass or class/interface relationship; consistent behavior is also required. In particular, a sub class should not modify the behavior of the super class.Consider the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle] &amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;to make a class B a subclass of class A or to make  an implementer of interface A.&lt;br /&gt;
If class B models a role played by class A, especially a temporary role, then B should not be a subclass of A. Instead referencing should be used.&lt;br /&gt;
Inheritance between a superclass A and a subclass B is appropriate if it promotes code reuse,each object of the sub class  “is an” object of the super class, and the public interface of class B includes the interface of class A and the behavior of the methods in this interface in both classes is similar in both classes.&lt;br /&gt;
Inheritance, especially a deep inheritance tree  is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it hard to code and to follow the flow of execution.If you have a class with behavior that applies to only some of the objects of the class, then con-&lt;br /&gt;
sider splitting the class into two classes associated by inheritance either directly or through a common abstract class or interface. When designing a class B that will be very similar to an existing class A, use referencing if you want the functionality of A but not the interface,and use inheritance only when you want the functionality and interface of A.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&lt;br /&gt;
&lt;br /&gt;
Tips for Effective Software Reuse. http://www.infoq.com/articles/vijay-narayanan-software-reuse, Oct. 2011.&lt;br /&gt;
&lt;br /&gt;
When to Use Inheritance. http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Inheritance — Proper Inheritance and Substitutability. http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Liskovs Substitution. Principle http://javaboutique.internet.com/tutorials/JavaOO, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Scildt, herbert. ''Java: The Complete Reference,'' McGraw Hill, 2007.&lt;br /&gt;
&lt;br /&gt;
Software Engineering Observations. http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
*http://en.wikipedia.org/wiki/Don't_Repeat_Yourself&lt;br /&gt;
*http://en.wikipedia.org/wiki/Software_library&lt;br /&gt;
*http://stackoverflow.com/questions/4348557/liskovs-substitution-principle-how-to-model-square-and-rectangle&lt;br /&gt;
*http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html&lt;br /&gt;
*http://www.dlugosz.com/Perl6/web/isa-inheritance.html&lt;br /&gt;
*http://inheritingjava.blogspot.com/2011/01/chapter-14-coupling-and-cohesion.html&lt;br /&gt;
*http://www.builderau.com.au/program/java/soa/Dynamic-Classification-with-Interfaces-and-Delegation/0,339024620,320269511,00.htm&lt;br /&gt;
*http://www.objectmentor.com/resources/articles/lsp.pdf&lt;br /&gt;
*http://home.cogeco.ca/~ve3ll/jatutor5.htm&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54762</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54762"/>
		<updated>2011-11-04T02:54:55Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Additional Resources */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=When to use Inheritance: Lecture 17=&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
'''&lt;br /&gt;
== Different Perspectives on Inheritance ==&lt;br /&gt;
In this article we will consider different perspectives on when to use inheritance and when not to use inheritance. Let us look at the following reasons that are considered when using inheritance:&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. el al, 2005 &amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process: &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IS-A ===&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;lt;ref&amp;gt; When to Use Inheritance &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&amp;lt;ref&amp;gt;  &lt;br /&gt;
Inheritance - Proper Inheritance and Substitutability&amp;lt;/ref&amp;gt;&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]&amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
=== Public Interfaces ===&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java: The Complete Reference&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt;Software Engineering Observations &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Just having a similar interface is not sufficient for subclass/superclass or class/interface relationship; consistent behavior is also required. In particular, a sub class should not modify the behavior of the super class.Consider the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle] &amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;to make a class B a subclass of class A or to make  an implementer of interface A.&lt;br /&gt;
If class B models a role played by class A, especially a temporary role, then B should not be a subclass of A. Instead referencing should be used.&lt;br /&gt;
Inheritance between a superclass A and a subclass B is appropriate if it promotes code reuse,each object of the sub class  “is an” object of the super class, and the public interface of class B includes the interface of class A and the behavior of the methods in this interface in both classes is similar in both classes.&lt;br /&gt;
Inheritance, especially a deep inheritance tree  is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it hard to code and to follow the flow of execution.If you have a class with behavior that applies to only some of the objects of the class, then con-&lt;br /&gt;
sider splitting the class into two classes associated by inheritance either directly or through a common abstract class or interface. When designing a class B that will be very similar to an existing class A, use referencing if you want the functionality of A but not the interface,and use inheritance only when you want the functionality and interface of A.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&lt;br /&gt;
&lt;br /&gt;
Tips for Effective Software Reuse. http://www.infoq.com/articles/vijay-narayanan-software-reuse, Oct. 2011.&lt;br /&gt;
&lt;br /&gt;
When to Use Inheritance. http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Inheritance — Proper Inheritance and Substitutability. http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Liskovs Substitution. Principle http://javaboutique.internet.com/tutorials/JavaOO, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Scildt, herbert. ''Java: The Complete Reference,'' McGraw Hill, 2007.&lt;br /&gt;
&lt;br /&gt;
Software Engineering Observations. http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
*http://en.wikipedia.org/wiki/Don't_Repeat_Yourself&lt;br /&gt;
*http://en.wikipedia.org/wiki/Software_library&lt;br /&gt;
*http://stackoverflow.com/questions/4348557/liskovs-substitution-principle-how-to-model-square-and-rectangle&lt;br /&gt;
*http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html&lt;br /&gt;
*http://www.dlugosz.com/Perl6/web/isa-inheritance.html&lt;br /&gt;
*http://inheritingjava.blogspot.com/2011/01/chapter-14-coupling-and-cohesion.html&lt;br /&gt;
*http://www.builderau.com.au/program/java/soa/Dynamic-Classification-with-Interfaces-and-Delegation/0,339024620,320269511,00.htm&lt;br /&gt;
*http://www.objectmentor.com/resources/articles/lsp.pdf&lt;br /&gt;
*http://home.cogeco.ca/~ve3ll/jatutor5.htm&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54761</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54761"/>
		<updated>2011-11-04T02:54:28Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Full Reference Information */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=When to use Inheritance: Lecture 17=&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
'''&lt;br /&gt;
== Different Perspectives on Inheritance ==&lt;br /&gt;
In this article we will consider different perspectives on when to use inheritance and when not to use inheritance. Let us look at the following reasons that are considered when using inheritance:&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. el al, 2005 &amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process: &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IS-A ===&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;lt;ref&amp;gt; When to Use Inheritance &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&amp;lt;ref&amp;gt;  &lt;br /&gt;
Inheritance - Proper Inheritance and Substitutability&amp;lt;/ref&amp;gt;&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]&amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
=== Public Interfaces ===&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java: The Complete Reference&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt;Software Engineering Observations &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Just having a similar interface is not sufficient for subclass/superclass or class/interface relationship; consistent behavior is also required. In particular, a sub class should not modify the behavior of the super class.Consider the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle] &amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;to make a class B a subclass of class A or to make  an implementer of interface A.&lt;br /&gt;
If class B models a role played by class A, especially a temporary role, then B should not be a subclass of A. Instead referencing should be used.&lt;br /&gt;
Inheritance between a superclass A and a subclass B is appropriate if it promotes code reuse,each object of the sub class  “is an” object of the super class, and the public interface of class B includes the interface of class A and the behavior of the methods in this interface in both classes is similar in both classes.&lt;br /&gt;
Inheritance, especially a deep inheritance tree  is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it hard to code and to follow the flow of execution.If you have a class with behavior that applies to only some of the objects of the class, then con-&lt;br /&gt;
sider splitting the class into two classes associated by inheritance either directly or through a common abstract class or interface. When designing a class B that will be very similar to an existing class A, use referencing if you want the functionality of A but not the interface,and use inheritance only when you want the functionality and interface of A.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&lt;br /&gt;
&lt;br /&gt;
Tips for Effective Software Reuse. http://www.infoq.com/articles/vijay-narayanan-software-reuse, Oct. 2011.&lt;br /&gt;
&lt;br /&gt;
When to Use Inheritance. http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Inheritance — Proper Inheritance and Substitutability. http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Liskovs Substitution. Principle http://javaboutique.internet.com/tutorials/JavaOO, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Scildt, herbert. ''Java: The Complete Reference,'' McGraw Hill, 2007.&lt;br /&gt;
&lt;br /&gt;
Software Engineering Observations. http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. Agile Web Development with Rails: Third Edition: The Pragmatic Programmers LLC, 2009&lt;br /&gt;
&lt;br /&gt;
*http://en.wikipedia.org/wiki/Don't_Repeat_Yourself&lt;br /&gt;
*http://en.wikipedia.org/wiki/Software_library&lt;br /&gt;
&lt;br /&gt;
*http://stackoverflow.com/questions/4348557/liskovs-substitution-principle-how-to-model-square-and-rectangle&lt;br /&gt;
*http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html&lt;br /&gt;
*http://www.dlugosz.com/Perl6/web/isa-inheritance.html&lt;br /&gt;
*http://inheritingjava.blogspot.com/2011/01/chapter-14-coupling-and-cohesion.html&lt;br /&gt;
*http://www.builderau.com.au/program/java/soa/Dynamic-Classification-with-Interfaces-and-Delegation/0,339024620,320269511,00.htm&lt;br /&gt;
*http://www.objectmentor.com/resources/articles/lsp.pdf&lt;br /&gt;
*http://home.cogeco.ca/~ve3ll/jatutor5.htm&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54760</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54760"/>
		<updated>2011-11-04T02:54:07Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Full Reference Information */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=When to use Inheritance: Lecture 17=&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
'''&lt;br /&gt;
== Different Perspectives on Inheritance ==&lt;br /&gt;
In this article we will consider different perspectives on when to use inheritance and when not to use inheritance. Let us look at the following reasons that are considered when using inheritance:&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. el al, 2005 &amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process: &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IS-A ===&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;lt;ref&amp;gt; When to Use Inheritance &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&amp;lt;ref&amp;gt;  &lt;br /&gt;
Inheritance - Proper Inheritance and Substitutability&amp;lt;/ref&amp;gt;&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]&amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
=== Public Interfaces ===&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java: The Complete Reference&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt;Software Engineering Observations &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Just having a similar interface is not sufficient for subclass/superclass or class/interface relationship; consistent behavior is also required. In particular, a sub class should not modify the behavior of the super class.Consider the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle] &amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;to make a class B a subclass of class A or to make  an implementer of interface A.&lt;br /&gt;
If class B models a role played by class A, especially a temporary role, then B should not be a subclass of A. Instead referencing should be used.&lt;br /&gt;
Inheritance between a superclass A and a subclass B is appropriate if it promotes code reuse,each object of the sub class  “is an” object of the super class, and the public interface of class B includes the interface of class A and the behavior of the methods in this interface in both classes is similar in both classes.&lt;br /&gt;
Inheritance, especially a deep inheritance tree  is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it hard to code and to follow the flow of execution.If you have a class with behavior that applies to only some of the objects of the class, then con-&lt;br /&gt;
sider splitting the class into two classes associated by inheritance either directly or through a common abstract class or interface. When designing a class B that will be very similar to an existing class A, use referencing if you want the functionality of A but not the interface,and use inheritance only when you want the functionality and interface of A.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&lt;br /&gt;
&lt;br /&gt;
Tips for Effective Software Reuse. http://www.infoq.com/articles/vijay-narayanan-software-reuse, Oct. 2011.&lt;br /&gt;
&lt;br /&gt;
When to Use Inheritance. http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Inheritance — Proper Inheritance and Substitutability. http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Liskovs Substitution. Principle http://javaboutique.internet.com/tutorials/JavaOO, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Scildt, herbert. ''Java: The Complete Reference,'' McGraw Hill, 2007.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Software Engineering Observations. http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. Agile Web Development with Rails: Third Edition: The Pragmatic Programmers LLC, 2009&lt;br /&gt;
&lt;br /&gt;
*http://en.wikipedia.org/wiki/Don't_Repeat_Yourself&lt;br /&gt;
*http://en.wikipedia.org/wiki/Software_library&lt;br /&gt;
&lt;br /&gt;
*http://stackoverflow.com/questions/4348557/liskovs-substitution-principle-how-to-model-square-and-rectangle&lt;br /&gt;
*http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html&lt;br /&gt;
*http://www.dlugosz.com/Perl6/web/isa-inheritance.html&lt;br /&gt;
*http://inheritingjava.blogspot.com/2011/01/chapter-14-coupling-and-cohesion.html&lt;br /&gt;
*http://www.builderau.com.au/program/java/soa/Dynamic-Classification-with-Interfaces-and-Delegation/0,339024620,320269511,00.htm&lt;br /&gt;
*http://www.objectmentor.com/resources/articles/lsp.pdf&lt;br /&gt;
*http://home.cogeco.ca/~ve3ll/jatutor5.htm&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54759</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54759"/>
		<updated>2011-11-04T02:50:07Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=When to use Inheritance: Lecture 17=&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
'''&lt;br /&gt;
== Different Perspectives on Inheritance ==&lt;br /&gt;
In this article we will consider different perspectives on when to use inheritance and when not to use inheritance. Let us look at the following reasons that are considered when using inheritance:&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. el al, 2005 &amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process: &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IS-A ===&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;lt;ref&amp;gt; When to Use Inheritance &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&amp;lt;ref&amp;gt;  &lt;br /&gt;
Inheritance - Proper Inheritance and Substitutability&amp;lt;/ref&amp;gt;&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]&amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
=== Public Interfaces ===&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java: The Complete Reference&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt;Software Engineering Observations &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Just having a similar interface is not sufficient for subclass/superclass or class/interface relationship; consistent behavior is also required. In particular, a sub class should not modify the behavior of the super class.Consider the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle] &amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;to make a class B a subclass of class A or to make  an implementer of interface A.&lt;br /&gt;
If class B models a role played by class A, especially a temporary role, then B should not be a subclass of A. Instead referencing should be used.&lt;br /&gt;
Inheritance between a superclass A and a subclass B is appropriate if it promotes code reuse,each object of the sub class  “is an” object of the super class, and the public interface of class B includes the interface of class A and the behavior of the methods in this interface in both classes is similar in both classes.&lt;br /&gt;
Inheritance, especially a deep inheritance tree  is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it hard to code and to follow the flow of execution.If you have a class with behavior that applies to only some of the objects of the class, then con-&lt;br /&gt;
sider splitting the class into two classes associated by inheritance either directly or through a common abstract class or interface. When designing a class B that will be very similar to an existing class A, use referencing if you want the functionality of A but not the interface,and use inheritance only when you want the functionality and interface of A.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&lt;br /&gt;
&lt;br /&gt;
Tips for Effective Software Reuse. http://www.infoq.com/articles/vijay-narayanan-software-reuse, Oct. 2011.&lt;br /&gt;
&lt;br /&gt;
When to Use Inheritance. http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Inheritance — Proper Inheritance and Substitutability. http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Liskovs Substitution. Principle http://javaboutique.internet.com/tutorials/JavaOO, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Software Engineering Observations. http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. Agile Web Development with Rails: Third Edition: The Pragmatic Programmers LLC, 2009&lt;br /&gt;
&lt;br /&gt;
*http://en.wikipedia.org/wiki/Don't_Repeat_Yourself&lt;br /&gt;
*http://en.wikipedia.org/wiki/Software_library&lt;br /&gt;
&lt;br /&gt;
*http://stackoverflow.com/questions/4348557/liskovs-substitution-principle-how-to-model-square-and-rectangle&lt;br /&gt;
*http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html&lt;br /&gt;
*http://www.dlugosz.com/Perl6/web/isa-inheritance.html&lt;br /&gt;
*http://inheritingjava.blogspot.com/2011/01/chapter-14-coupling-and-cohesion.html&lt;br /&gt;
*http://www.builderau.com.au/program/java/soa/Dynamic-Classification-with-Interfaces-and-Delegation/0,339024620,320269511,00.htm&lt;br /&gt;
*http://www.objectmentor.com/resources/articles/lsp.pdf&lt;br /&gt;
*http://home.cogeco.ca/~ve3ll/jatutor5.htm&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54758</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54758"/>
		<updated>2011-11-04T02:48:16Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Code Reuse */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=When to use Inheritance: Lecture 17=&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
'''&lt;br /&gt;
== Different Perspectives on Inheritance ==&lt;br /&gt;
In this article we will consider different perspectives on when to use inheritance and when not to use inheritance. Let us look at the following reasons that are considered when using inheritance:&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. el al, 2005 &amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process: &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IS-A ===&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;lt;ref&amp;gt; When to Use Inheritance &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&amp;lt;ref&amp;gt;  &lt;br /&gt;
Inheritance - Proper Inheritance and Substitutability&amp;lt;/ref&amp;gt;&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]&amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
=== Public Interfaces ===&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java Reference Book&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt;Software Engineering Observations &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Just having a similar interface is not sufficient for subclass/superclass or class/interface relationship; consistent behavior is also required. In particular, a sub class should not modify the behavior of the super class.Consider the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle] &amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;to make a class B a subclass of class A or to make  an implementer of interface A.&lt;br /&gt;
If class B models a role played by class A, especially a temporary role, then B should not be a subclass of A. Instead referencing should be used.&lt;br /&gt;
Inheritance between a superclass A and a subclass B is appropriate if it promotes code reuse,each object of the sub class  “is an” object of the super class, and the public interface of class B includes the interface of class A and the behavior of the methods in this interface in both classes is similar in both classes.&lt;br /&gt;
Inheritance, especially a deep inheritance tree  is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it hard to code and to follow the flow of execution.If you have a class with behavior that applies to only some of the objects of the class, then con-&lt;br /&gt;
sider splitting the class into two classes associated by inheritance either directly or through a common abstract class or interface. When designing a class B that will be very similar to an existing class A, use referencing if you want the functionality of A but not the interface,and use inheritance only when you want the functionality and interface of A.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&lt;br /&gt;
&lt;br /&gt;
Tips for Effective Software Reuse. http://www.infoq.com/articles/vijay-narayanan-software-reuse, Oct. 2011.&lt;br /&gt;
&lt;br /&gt;
When to Use Inheritance. http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Inheritance — Proper Inheritance and Substitutability. http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Liskovs Substitution. Principle http://javaboutique.internet.com/tutorials/JavaOO, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Software Engineering Observations. http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. Agile Web Development with Rails: Third Edition: The Pragmatic Programmers LLC, 2009&lt;br /&gt;
&lt;br /&gt;
*http://en.wikipedia.org/wiki/Don't_Repeat_Yourself&lt;br /&gt;
*http://en.wikipedia.org/wiki/Software_library&lt;br /&gt;
&lt;br /&gt;
*http://stackoverflow.com/questions/4348557/liskovs-substitution-principle-how-to-model-square-and-rectangle&lt;br /&gt;
*http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html&lt;br /&gt;
*http://www.dlugosz.com/Perl6/web/isa-inheritance.html&lt;br /&gt;
*http://inheritingjava.blogspot.com/2011/01/chapter-14-coupling-and-cohesion.html&lt;br /&gt;
*http://www.builderau.com.au/program/java/soa/Dynamic-Classification-with-Interfaces-and-Delegation/0,339024620,320269511,00.htm&lt;br /&gt;
*http://www.objectmentor.com/resources/articles/lsp.pdf&lt;br /&gt;
*http://home.cogeco.ca/~ve3ll/jatutor5.htm&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54757</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54757"/>
		<updated>2011-11-04T02:46:58Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Full Reference Information */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=When to use Inheritance: Lecture 17=&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
'''&lt;br /&gt;
== Different Perspectives on Inheritance ==&lt;br /&gt;
In this article we will consider different perspectives on when to use inheritance and when not to use inheritance. Let us look at the following reasons that are considered when using inheritance:&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process: &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IS-A ===&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;lt;ref&amp;gt; When to Use Inheritance &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&amp;lt;ref&amp;gt;  &lt;br /&gt;
Inheritance - Proper Inheritance and Substitutability&amp;lt;/ref&amp;gt;&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]&amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
=== Public Interfaces ===&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java Reference Book&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt;Software Engineering Observations &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Just having a similar interface is not sufficient for subclass/superclass or class/interface relationship; consistent behavior is also required. In particular, a sub class should not modify the behavior of the super class.Consider the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle] &amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;to make a class B a subclass of class A or to make  an implementer of interface A.&lt;br /&gt;
If class B models a role played by class A, especially a temporary role, then B should not be a subclass of A. Instead referencing should be used.&lt;br /&gt;
Inheritance between a superclass A and a subclass B is appropriate if it promotes code reuse,each object of the sub class  “is an” object of the super class, and the public interface of class B includes the interface of class A and the behavior of the methods in this interface in both classes is similar in both classes.&lt;br /&gt;
Inheritance, especially a deep inheritance tree  is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it hard to code and to follow the flow of execution.If you have a class with behavior that applies to only some of the objects of the class, then con-&lt;br /&gt;
sider splitting the class into two classes associated by inheritance either directly or through a common abstract class or interface. When designing a class B that will be very similar to an existing class A, use referencing if you want the functionality of A but not the interface,and use inheritance only when you want the functionality and interface of A.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&lt;br /&gt;
&lt;br /&gt;
Tips for Effective Software Reuse. http://www.infoq.com/articles/vijay-narayanan-software-reuse, Oct. 2011.&lt;br /&gt;
&lt;br /&gt;
When to Use Inheritance. http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Inheritance — Proper Inheritance and Substitutability. http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Liskovs Substitution. Principle http://javaboutique.internet.com/tutorials/JavaOO, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Software Engineering Observations. http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. Agile Web Development with Rails: Third Edition: The Pragmatic Programmers LLC, 2009&lt;br /&gt;
&lt;br /&gt;
*http://en.wikipedia.org/wiki/Don't_Repeat_Yourself&lt;br /&gt;
*http://en.wikipedia.org/wiki/Software_library&lt;br /&gt;
&lt;br /&gt;
*http://stackoverflow.com/questions/4348557/liskovs-substitution-principle-how-to-model-square-and-rectangle&lt;br /&gt;
*http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html&lt;br /&gt;
*http://www.dlugosz.com/Perl6/web/isa-inheritance.html&lt;br /&gt;
*http://inheritingjava.blogspot.com/2011/01/chapter-14-coupling-and-cohesion.html&lt;br /&gt;
*http://www.builderau.com.au/program/java/soa/Dynamic-Classification-with-Interfaces-and-Delegation/0,339024620,320269511,00.htm&lt;br /&gt;
*http://www.objectmentor.com/resources/articles/lsp.pdf&lt;br /&gt;
*http://home.cogeco.ca/~ve3ll/jatutor5.htm&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54756</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54756"/>
		<updated>2011-11-04T02:46:03Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Full Reference Information */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=When to use Inheritance: Lecture 17=&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
'''&lt;br /&gt;
== Different Perspectives on Inheritance ==&lt;br /&gt;
In this article we will consider different perspectives on when to use inheritance and when not to use inheritance. Let us look at the following reasons that are considered when using inheritance:&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process: &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IS-A ===&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;lt;ref&amp;gt; When to Use Inheritance &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&amp;lt;ref&amp;gt;  &lt;br /&gt;
Inheritance - Proper Inheritance and Substitutability&amp;lt;/ref&amp;gt;&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]&amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
=== Public Interfaces ===&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java Reference Book&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt;Software Engineering Observations &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Just having a similar interface is not sufficient for subclass/superclass or class/interface relationship; consistent behavior is also required. In particular, a sub class should not modify the behavior of the super class.Consider the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle] &amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;to make a class B a subclass of class A or to make  an implementer of interface A.&lt;br /&gt;
If class B models a role played by class A, especially a temporary role, then B should not be a subclass of A. Instead referencing should be used.&lt;br /&gt;
Inheritance between a superclass A and a subclass B is appropriate if it promotes code reuse,each object of the sub class  “is an” object of the super class, and the public interface of class B includes the interface of class A and the behavior of the methods in this interface in both classes is similar in both classes.&lt;br /&gt;
Inheritance, especially a deep inheritance tree  is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it hard to code and to follow the flow of execution.If you have a class with behavior that applies to only some of the objects of the class, then con-&lt;br /&gt;
sider splitting the class into two classes associated by inheritance either directly or through a common abstract class or interface. When designing a class B that will be very similar to an existing class A, use referencing if you want the functionality of A but not the interface,and use inheritance only when you want the functionality and interface of A.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Tips for Effective Software Reuse. http://www.infoq.com/articles/vijay-narayanan-software-reuse, Oct. 2011.&lt;br /&gt;
&lt;br /&gt;
When to Use Inheritance. http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Inheritance — Proper Inheritance and Substitutability. http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Liskovs Substitution. Principle http://javaboutique.internet.com/tutorials/JavaOO, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Software Engineering Observations. http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. Agile Web Development with Rails: Third Edition: The Pragmatic Programmers LLC, 2009&lt;br /&gt;
&lt;br /&gt;
*http://en.wikipedia.org/wiki/Don't_Repeat_Yourself&lt;br /&gt;
*http://en.wikipedia.org/wiki/Software_library&lt;br /&gt;
&lt;br /&gt;
*http://stackoverflow.com/questions/4348557/liskovs-substitution-principle-how-to-model-square-and-rectangle&lt;br /&gt;
*http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html&lt;br /&gt;
*http://www.dlugosz.com/Perl6/web/isa-inheritance.html&lt;br /&gt;
*http://inheritingjava.blogspot.com/2011/01/chapter-14-coupling-and-cohesion.html&lt;br /&gt;
*http://www.builderau.com.au/program/java/soa/Dynamic-Classification-with-Interfaces-and-Delegation/0,339024620,320269511,00.htm&lt;br /&gt;
*http://www.objectmentor.com/resources/articles/lsp.pdf&lt;br /&gt;
*http://home.cogeco.ca/~ve3ll/jatutor5.htm&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54755</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54755"/>
		<updated>2011-11-04T02:45:24Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Full Reference Information */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=When to use Inheritance: Lecture 17=&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
'''&lt;br /&gt;
== Different Perspectives on Inheritance ==&lt;br /&gt;
In this article we will consider different perspectives on when to use inheritance and when not to use inheritance. Let us look at the following reasons that are considered when using inheritance:&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process: &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IS-A ===&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;lt;ref&amp;gt; When to Use Inheritance &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&amp;lt;ref&amp;gt;  &lt;br /&gt;
Inheritance - Proper Inheritance and Substitutability&amp;lt;/ref&amp;gt;&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]&amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
=== Public Interfaces ===&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java Reference Book&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt;Software Engineering Observations &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Just having a similar interface is not sufficient for subclass/superclass or class/interface relationship; consistent behavior is also required. In particular, a sub class should not modify the behavior of the super class.Consider the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle] &amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;to make a class B a subclass of class A or to make  an implementer of interface A.&lt;br /&gt;
If class B models a role played by class A, especially a temporary role, then B should not be a subclass of A. Instead referencing should be used.&lt;br /&gt;
Inheritance between a superclass A and a subclass B is appropriate if it promotes code reuse,each object of the sub class  “is an” object of the super class, and the public interface of class B includes the interface of class A and the behavior of the methods in this interface in both classes is similar in both classes.&lt;br /&gt;
Inheritance, especially a deep inheritance tree  is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it hard to code and to follow the flow of execution.If you have a class with behavior that applies to only some of the objects of the class, then con-&lt;br /&gt;
sider splitting the class into two classes associated by inheritance either directly or through a common abstract class or interface. When designing a class B that will be very similar to an existing class A, use referencing if you want the functionality of A but not the interface,and use inheritance only when you want the functionality and interface of A.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Tips for Effective Software Reuse. http://www.infoq.com/articles/vijay-narayanan-software-reuse, Oct. 2011.&lt;br /&gt;
&lt;br /&gt;
When to Use Inheritance. http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Inheritance — proper inheritance and substitutability. http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Liskovs Substitution. Principle http://javaboutique.internet.com/tutorials/JavaOO, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Software Engineering Observations. http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. Agile Web Development with Rails: Third Edition: The Pragmatic Programmers LLC, 2009&lt;br /&gt;
&lt;br /&gt;
*http://en.wikipedia.org/wiki/Don't_Repeat_Yourself&lt;br /&gt;
*http://en.wikipedia.org/wiki/Software_library&lt;br /&gt;
&lt;br /&gt;
*http://stackoverflow.com/questions/4348557/liskovs-substitution-principle-how-to-model-square-and-rectangle&lt;br /&gt;
*http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html&lt;br /&gt;
*http://www.dlugosz.com/Perl6/web/isa-inheritance.html&lt;br /&gt;
*http://inheritingjava.blogspot.com/2011/01/chapter-14-coupling-and-cohesion.html&lt;br /&gt;
*http://www.builderau.com.au/program/java/soa/Dynamic-Classification-with-Interfaces-and-Delegation/0,339024620,320269511,00.htm&lt;br /&gt;
*http://www.objectmentor.com/resources/articles/lsp.pdf&lt;br /&gt;
*http://home.cogeco.ca/~ve3ll/jatutor5.htm&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54754</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54754"/>
		<updated>2011-11-04T02:44:37Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=When to use Inheritance: Lecture 17=&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
'''&lt;br /&gt;
== Different Perspectives on Inheritance ==&lt;br /&gt;
In this article we will consider different perspectives on when to use inheritance and when not to use inheritance. Let us look at the following reasons that are considered when using inheritance:&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process: &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IS-A ===&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;lt;ref&amp;gt; When to Use Inheritance &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&amp;lt;ref&amp;gt;  &lt;br /&gt;
Inheritance - Proper Inheritance and Substitutability&amp;lt;/ref&amp;gt;&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]&amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
=== Public Interfaces ===&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java Reference Book&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt;Software Engineering Observations &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Just having a similar interface is not sufficient for subclass/superclass or class/interface relationship; consistent behavior is also required. In particular, a sub class should not modify the behavior of the super class.Consider the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle] &amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;to make a class B a subclass of class A or to make  an implementer of interface A.&lt;br /&gt;
If class B models a role played by class A, especially a temporary role, then B should not be a subclass of A. Instead referencing should be used.&lt;br /&gt;
Inheritance between a superclass A and a subclass B is appropriate if it promotes code reuse,each object of the sub class  “is an” object of the super class, and the public interface of class B includes the interface of class A and the behavior of the methods in this interface in both classes is similar in both classes.&lt;br /&gt;
Inheritance, especially a deep inheritance tree  is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it hard to code and to follow the flow of execution.If you have a class with behavior that applies to only some of the objects of the class, then con-&lt;br /&gt;
sider splitting the class into two classes associated by inheritance either directly or through a common abstract class or interface. When designing a class B that will be very similar to an existing class A, use referencing if you want the functionality of A but not the interface,and use inheritance only when you want the functionality and interface of A.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Tips for Effective Software Reuse. http://www.infoq.com/articles/vijay-narayanan-software-reuse, Oct. 2011.&lt;br /&gt;
&lt;br /&gt;
When to Use Inheritance. http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Inheritance — proper inheritance and substitutability. http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Liskovs Substitution. Principle http://javaboutique.internet.com/tutorials/JavaOO, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. Agile Web Development with Rails: Third Edition: The Pragmatic Programmers LLC, 2009&lt;br /&gt;
&lt;br /&gt;
*http://en.wikipedia.org/wiki/Don't_Repeat_Yourself&lt;br /&gt;
*http://en.wikipedia.org/wiki/Software_library&lt;br /&gt;
&lt;br /&gt;
*http://stackoverflow.com/questions/4348557/liskovs-substitution-principle-how-to-model-square-and-rectangle&lt;br /&gt;
*http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html&lt;br /&gt;
*http://www.dlugosz.com/Perl6/web/isa-inheritance.html&lt;br /&gt;
*http://inheritingjava.blogspot.com/2011/01/chapter-14-coupling-and-cohesion.html&lt;br /&gt;
*http://www.builderau.com.au/program/java/soa/Dynamic-Classification-with-Interfaces-and-Delegation/0,339024620,320269511,00.htm&lt;br /&gt;
*http://www.objectmentor.com/resources/articles/lsp.pdf&lt;br /&gt;
*http://home.cogeco.ca/~ve3ll/jatutor5.htm&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54753</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54753"/>
		<updated>2011-11-04T02:43:30Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Inheritance vs Delegation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=When to use Inheritance: Lecture 17=&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
'''&lt;br /&gt;
== Different Perspectives on Inheritance ==&lt;br /&gt;
In this article we will consider different perspectives on when to use inheritance and when not to use inheritance. Let us look at the following reasons that are considered when using inheritance:&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process: &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IS-A ===&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;lt;ref&amp;gt; When to Use Inheritance &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&amp;lt;ref&amp;gt;  &lt;br /&gt;
Inheritance - Proper Inheritance and Substitutability&amp;lt;/ref&amp;gt;&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]&amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
=== Public Interfaces ===&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java Reference Book&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt; Software Engineering Observations&lt;br /&gt;
http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Just having a similar interface is not sufficient for subclass/superclass or class/interface relationship; consistent behavior is also required. In particular, a sub class should not modify the behavior of the super class.Consider the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle] &amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;to make a class B a subclass of class A or to make  an implementer of interface A.&lt;br /&gt;
If class B models a role played by class A, especially a temporary role, then B should not be a subclass of A. Instead referencing should be used.&lt;br /&gt;
Inheritance between a superclass A and a subclass B is appropriate if it promotes code reuse,each object of the sub class  “is an” object of the super class, and the public interface of class B includes the interface of class A and the behavior of the methods in this interface in both classes is similar in both classes.&lt;br /&gt;
Inheritance, especially a deep inheritance tree  is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it hard to code and to follow the flow of execution.If you have a class with behavior that applies to only some of the objects of the class, then con-&lt;br /&gt;
sider splitting the class into two classes associated by inheritance either directly or through a common abstract class or interface. When designing a class B that will be very similar to an existing class A, use referencing if you want the functionality of A but not the interface,and use inheritance only when you want the functionality and interface of A.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Tips for Effective Software Reuse. http://www.infoq.com/articles/vijay-narayanan-software-reuse, Oct. 2011.&lt;br /&gt;
&lt;br /&gt;
When to Use Inheritance. http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Inheritance — proper inheritance and substitutability. http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Liskovs Substitution. Principle http://javaboutique.internet.com/tutorials/JavaOO, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. Agile Web Development with Rails: Third Edition: The Pragmatic Programmers LLC, 2009&lt;br /&gt;
&lt;br /&gt;
*http://en.wikipedia.org/wiki/Don't_Repeat_Yourself&lt;br /&gt;
*http://en.wikipedia.org/wiki/Software_library&lt;br /&gt;
&lt;br /&gt;
*http://stackoverflow.com/questions/4348557/liskovs-substitution-principle-how-to-model-square-and-rectangle&lt;br /&gt;
*http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html&lt;br /&gt;
*http://www.dlugosz.com/Perl6/web/isa-inheritance.html&lt;br /&gt;
*http://inheritingjava.blogspot.com/2011/01/chapter-14-coupling-and-cohesion.html&lt;br /&gt;
*http://www.builderau.com.au/program/java/soa/Dynamic-Classification-with-Interfaces-and-Delegation/0,339024620,320269511,00.htm&lt;br /&gt;
*http://www.objectmentor.com/resources/articles/lsp.pdf&lt;br /&gt;
*http://home.cogeco.ca/~ve3ll/jatutor5.htm&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54752</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54752"/>
		<updated>2011-11-04T02:37:40Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* IS-A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=When to use Inheritance: Lecture 17=&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
'''&lt;br /&gt;
== Different Perspectives on Inheritance ==&lt;br /&gt;
In this article we will consider different perspectives on when to use inheritance and when not to use inheritance. Let us look at the following reasons that are considered when using inheritance:&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process: &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IS-A ===&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;lt;ref&amp;gt; When to Use Inheritance &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&amp;lt;ref&amp;gt;  &lt;br /&gt;
Inheritance - Proper Inheritance and Substitutability&amp;lt;/ref&amp;gt;&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]&amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
=== Public Interfaces ===&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java Reference Book&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt; Software Engineering Observations&lt;br /&gt;
http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation &amp;lt;ref&amp;gt; http://javaboutique.internet.com/tutorials/JavaOO/&amp;lt;/ref&amp;gt; have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Just having a similar interface is not sufficient for subclass/superclass or class/interface relationship; consistent behavior is also required. In particular, a sub class should not modify the behavior of the super class.Consider the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle] &amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;to make a class B a subclass of class A or to make  an implementer of interface A.&lt;br /&gt;
If class B models a role played by class A, especially a temporary role, then B should not be a subclass of A. Instead referencing should be used.&lt;br /&gt;
Inheritance between a superclass A and a subclass B is appropriate if it promotes code reuse,each object of the sub class  “is an” object of the super class, and the public interface of class B includes the interface of class A and the behavior of the methods in this interface in both classes is similar in both classes.&lt;br /&gt;
Inheritance, especially a deep inheritance tree  is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it hard to code and to follow the flow of execution.If you have a class with behavior that applies to only some of the objects of the class, then con-&lt;br /&gt;
sider splitting the class into two classes associated by inheritance either directly or through a common abstract class or interface. When designing a class B that will be very similar to an existing class A, use referencing if you want the functionality of A but not the interface,and use inheritance only when you want the functionality and interface of A.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Tips for Effective Software Reuse. http://www.infoq.com/articles/vijay-narayanan-software-reuse, Oct. 2011.&lt;br /&gt;
&lt;br /&gt;
When to Use Inheritance. http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Inheritance — proper inheritance and substitutability. http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Liskovs Substitution. Principle http://javaboutique.internet.com/tutorials/JavaOO, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. Agile Web Development with Rails: Third Edition: The Pragmatic Programmers LLC, 2009&lt;br /&gt;
&lt;br /&gt;
*http://en.wikipedia.org/wiki/Don't_Repeat_Yourself&lt;br /&gt;
*http://en.wikipedia.org/wiki/Software_library&lt;br /&gt;
&lt;br /&gt;
*http://stackoverflow.com/questions/4348557/liskovs-substitution-principle-how-to-model-square-and-rectangle&lt;br /&gt;
*http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html&lt;br /&gt;
*http://www.dlugosz.com/Perl6/web/isa-inheritance.html&lt;br /&gt;
*http://inheritingjava.blogspot.com/2011/01/chapter-14-coupling-and-cohesion.html&lt;br /&gt;
*http://www.builderau.com.au/program/java/soa/Dynamic-Classification-with-Interfaces-and-Delegation/0,339024620,320269511,00.htm&lt;br /&gt;
*http://www.objectmentor.com/resources/articles/lsp.pdf&lt;br /&gt;
*http://home.cogeco.ca/~ve3ll/jatutor5.htm&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54751</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54751"/>
		<updated>2011-11-04T02:35:51Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=When to use Inheritance: Lecture 17=&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
'''&lt;br /&gt;
== Different Perspectives on Inheritance ==&lt;br /&gt;
In this article we will consider different perspectives on when to use inheritance and when not to use inheritance. Let us look at the following reasons that are considered when using inheritance:&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process: &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== IS-A ===&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;lt;ref&amp;gt; When to Use Inheritance &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&amp;lt;ref&amp;gt;  &lt;br /&gt;
Inheritance - Proper Inheritance and Substitutability&amp;lt;/ref&amp;gt;&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
=== Public Interfaces ===&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
=== Polymorphism ===&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java Reference Book&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt; Software Engineering Observations&lt;br /&gt;
http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation &amp;lt;ref&amp;gt; http://javaboutique.internet.com/tutorials/JavaOO/&amp;lt;/ref&amp;gt; have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Just having a similar interface is not sufficient for subclass/superclass or class/interface relationship; consistent behavior is also required. In particular, a sub class should not modify the behavior of the super class.Consider the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle] &amp;lt;ref&amp;gt; Liskov's Substitution Principle &amp;lt;/ref&amp;gt;to make a class B a subclass of class A or to make  an implementer of interface A.&lt;br /&gt;
If class B models a role played by class A, especially a temporary role, then B should not be a subclass of A. Instead referencing should be used.&lt;br /&gt;
Inheritance between a superclass A and a subclass B is appropriate if it promotes code reuse,each object of the sub class  “is an” object of the super class, and the public interface of class B includes the interface of class A and the behavior of the methods in this interface in both classes is similar in both classes.&lt;br /&gt;
Inheritance, especially a deep inheritance tree  is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it hard to code and to follow the flow of execution.If you have a class with behavior that applies to only some of the objects of the class, then con-&lt;br /&gt;
sider splitting the class into two classes associated by inheritance either directly or through a common abstract class or interface. When designing a class B that will be very similar to an existing class A, use referencing if you want the functionality of A but not the interface,and use inheritance only when you want the functionality and interface of A.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Tips for Effective Software Reuse. http://www.infoq.com/articles/vijay-narayanan-software-reuse, Oct. 2011.&lt;br /&gt;
&lt;br /&gt;
When to Use Inheritance. http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Inheritance — proper inheritance and substitutability. http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
Liskovs Substitution. Principle http://javaboutique.internet.com/tutorials/JavaOO, Oct 2011.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Additional Resources==&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. Agile Web Development with Rails: Third Edition: The Pragmatic Programmers LLC, 2009&lt;br /&gt;
&lt;br /&gt;
*http://en.wikipedia.org/wiki/Don't_Repeat_Yourself&lt;br /&gt;
*http://en.wikipedia.org/wiki/Software_library&lt;br /&gt;
&lt;br /&gt;
*http://stackoverflow.com/questions/4348557/liskovs-substitution-principle-how-to-model-square-and-rectangle&lt;br /&gt;
*http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html&lt;br /&gt;
*http://www.dlugosz.com/Perl6/web/isa-inheritance.html&lt;br /&gt;
*http://inheritingjava.blogspot.com/2011/01/chapter-14-coupling-and-cohesion.html&lt;br /&gt;
*http://www.builderau.com.au/program/java/soa/Dynamic-Classification-with-Interfaces-and-Delegation/0,339024620,320269511,00.htm&lt;br /&gt;
*http://www.objectmentor.com/resources/articles/lsp.pdf&lt;br /&gt;
*http://home.cogeco.ca/~ve3ll/jatutor5.htm&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54719</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54719"/>
		<updated>2011-11-03T03:37:55Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &lt;br /&gt;
http://www.infoq.com/articles/vijay-narayanan-software-reuse &amp;lt;/ref&amp;gt;.:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java Reference Book&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt; Software Engineering Observations&lt;br /&gt;
http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation &amp;lt;ref&amp;gt; http://javaboutique.internet.com/tutorials/JavaOO/&amp;lt;/ref&amp;gt;==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54716</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54716"/>
		<updated>2011-11-03T03:25:18Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation &amp;lt;ref&amp;gt; http://javaboutique.internet.com/tutorials/JavaOO/&amp;lt;/ref&amp;gt;==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &lt;br /&gt;
http://www.infoq.com/articles/vijay-narayanan-software-reuse &amp;lt;/ref&amp;gt;.:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java Reference Book&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt; Software Engineering Observations&lt;br /&gt;
http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54705</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54705"/>
		<updated>2011-11-03T02:54:09Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Inheritance vs Delegation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &lt;br /&gt;
http://www.infoq.com/articles/vijay-narayanan-software-reuse &amp;lt;/ref&amp;gt;.:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java Reference Book&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt; Software Engineering Observations&lt;br /&gt;
http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation &amp;lt;ref&amp;gt; http://javaboutique.internet.com/tutorials/JavaOO/&amp;lt;/ref&amp;gt;==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54703</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54703"/>
		<updated>2011-11-03T02:53:18Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* IS-A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &lt;br /&gt;
http://www.infoq.com/articles/vijay-narayanan-software-reuse &amp;lt;/ref&amp;gt;.:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java Reference Book&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt; Software Engineering Observations&lt;br /&gt;
http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the [http://javaboutique.internet.com/tutorials/JavaOO Liskov's Substitution Principle]. The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54701</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54701"/>
		<updated>2011-11-03T02:50:17Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Code Reuse */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &lt;br /&gt;
http://www.infoq.com/articles/vijay-narayanan-software-reuse &amp;lt;/ref&amp;gt;.:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java Reference Book&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt; Software Engineering Observations&lt;br /&gt;
http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54699</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54699"/>
		<updated>2011-11-03T02:49:09Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process &amp;lt;ref&amp;gt; 2&amp;lt;/ref&amp;gt;.:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; Java Reference Book&amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt; Software Engineering Observations&lt;br /&gt;
http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm &amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54696</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54696"/>
		<updated>2011-11-03T02:46:50Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Conclusion: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process &amp;lt;ref&amp;gt; 2&amp;lt;/ref&amp;gt;.:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; &amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt; 3&amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54693</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54693"/>
		<updated>2011-11-03T02:43:08Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Conclusion: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process &amp;lt;ref&amp;gt; 2&amp;lt;/ref&amp;gt;.:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; &amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt; 3&amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54691</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54691"/>
		<updated>2011-11-03T02:40:42Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* IS-A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process &amp;lt;ref&amp;gt; 2&amp;lt;/ref&amp;gt;.:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; &amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt; 3&amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
# A car is a vehicle.&lt;br /&gt;
# A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
&lt;br /&gt;
http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#[21.6](Circle and Ellipse)&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx (Is-A relationship)&lt;br /&gt;
&lt;br /&gt;
http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html(Inheritance)&lt;br /&gt;
&lt;br /&gt;
http://javaboutique.internet.com/tutorials/JavaOO/(LSP)&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=1(Inheritance vs Delegation)&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54689</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54689"/>
		<updated>2011-11-03T02:39:16Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process &amp;lt;ref&amp;gt; 2&amp;lt;/ref&amp;gt;.:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; &amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt; 3&amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
1. A car is a vehicle.&lt;br /&gt;
2. A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
&lt;br /&gt;
http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#[21.6](Circle and Ellipse)&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx (Is-A relationship)&lt;br /&gt;
&lt;br /&gt;
http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html(Inheritance)&lt;br /&gt;
&lt;br /&gt;
http://javaboutique.internet.com/tutorials/JavaOO/(LSP)&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=1(Inheritance vs Delegation)&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54688</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54688"/>
		<updated>2011-11-03T02:37:09Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process &amp;lt;ref&amp;gt; 2&amp;lt;/ref&amp;gt;.:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; &amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt; 3&amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
1. A car is a vehicle.&lt;br /&gt;
2. A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
&lt;br /&gt;
http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#[21.6](Circle and Ellipse)&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx (Is-A relationship)&lt;br /&gt;
&lt;br /&gt;
http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html(Inheritance)&lt;br /&gt;
&lt;br /&gt;
http://javaboutique.internet.com/tutorials/JavaOO/(LSP)&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=1(Inheritance vs Delegation)&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54687</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54687"/>
		<updated>2011-11-03T02:36:32Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process &amp;lt;ref&amp;gt; 2&amp;lt;/ref&amp;gt;.:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. &amp;lt;ref&amp;gt; &amp;lt;/ref&amp;gt;This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler. In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. &amp;lt;ref&amp;gt; 3&amp;lt;/ref&amp;gt; Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
1. A car is a vehicle.&lt;br /&gt;
2. A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
&lt;br /&gt;
http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#[21.6](Circle and Ellipse)&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx (Is-A relationship)&lt;br /&gt;
&lt;br /&gt;
http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html(Inheritance)&lt;br /&gt;
&lt;br /&gt;
http://javaboutique.internet.com/tutorials/JavaOO/(LSP)&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=1(Inheritance vs Delegation)&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54686</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54686"/>
		<updated>2011-11-03T02:32:31Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Code Reuse */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process &amp;lt;ref&amp;gt; 2&amp;lt;/ref&amp;gt;.:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” (put link to external definition) is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. {site from book}This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler.&lt;br /&gt;
Software Engineering Observations&lt;br /&gt;
In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. [2] Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
2http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
/*&lt;br /&gt;
Output           &lt;br /&gt;
       Inside Area for Rectangle.&lt;br /&gt;
       Area is  45&lt;br /&gt;
       Inside Area for Triangle.&lt;br /&gt;
       Area is 40&lt;br /&gt;
*/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
1. A car is a vehicle.&lt;br /&gt;
2. A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
&lt;br /&gt;
http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#[21.6](Circle and Ellipse)&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx (Is-A relationship)&lt;br /&gt;
&lt;br /&gt;
http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html(Inheritance)&lt;br /&gt;
&lt;br /&gt;
http://javaboutique.internet.com/tutorials/JavaOO/(LSP)&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=1(Inheritance vs Delegation)&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54685</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54685"/>
		<updated>2011-11-03T02:31:33Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Code Reuse */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process &amp;lt;ref&amp;gt; Tips for Effective Software Reuse &lt;br /&gt;
http://www.infoq.com/articles/vijay-narayanan-software-reuse&lt;br /&gt;
&amp;lt;\ref&amp;gt;.:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” (put link to external definition) is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. {site from book}This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler.&lt;br /&gt;
Software Engineering Observations&lt;br /&gt;
In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. [2] Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
2http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
/*&lt;br /&gt;
Output           &lt;br /&gt;
       Inside Area for Rectangle.&lt;br /&gt;
       Area is  45&lt;br /&gt;
       Inside Area for Triangle.&lt;br /&gt;
       Area is 40&lt;br /&gt;
*/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
1. A car is a vehicle.&lt;br /&gt;
2. A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
&lt;br /&gt;
http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#[21.6](Circle and Ellipse)&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx (Is-A relationship)&lt;br /&gt;
&lt;br /&gt;
http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html(Inheritance)&lt;br /&gt;
&lt;br /&gt;
http://javaboutique.internet.com/tutorials/JavaOO/(LSP)&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=1(Inheritance vs Delegation)&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54684</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54684"/>
		<updated>2011-11-03T02:25:42Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* What is Inheritance? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” (put link to external definition) is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. {site from book}This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler.&lt;br /&gt;
Software Engineering Observations&lt;br /&gt;
In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. [2] Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
2http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
/*&lt;br /&gt;
Output           &lt;br /&gt;
       Inside Area for Rectangle.&lt;br /&gt;
       Area is  45&lt;br /&gt;
       Inside Area for Triangle.&lt;br /&gt;
       Area is 40&lt;br /&gt;
*/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
1. A car is a vehicle.&lt;br /&gt;
2. A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
&lt;br /&gt;
http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#[21.6](Circle and Ellipse)&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx (Is-A relationship)&lt;br /&gt;
&lt;br /&gt;
http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html(Inheritance)&lt;br /&gt;
&lt;br /&gt;
http://javaboutique.internet.com/tutorials/JavaOO/(LSP)&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=1(Inheritance vs Delegation)&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54683</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54683"/>
		<updated>2011-11-03T02:24:00Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Code Reuse */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance? ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.&amp;lt;ref&amp;gt;Frakes, W.B. and Kyo Kang, (2005), &amp;quot;Software Reuse Research: Status and Future&amp;quot;, IEEE Transactions on Software Engineering, 31(7), July, pp. 529-536.&amp;lt;/ref&amp;gt; A [http://en.wikipedia.org/wiki/Software_library software library] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself] design principle. And although inheritance provides this capability a few things should be considered during the design process:&lt;br /&gt;
&lt;br /&gt;
# Focus on well-defined general aspects of the software program,&lt;br /&gt;
#* look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
# Name software asses appropriately&lt;br /&gt;
#* Package should an category classification encapsulating all the data&lt;br /&gt;
#* Interfaces should be unifying theme that group of classes&lt;br /&gt;
#* Classes should be proper nouns&lt;br /&gt;
#* Methods should be simple action verbs&lt;br /&gt;
# Being  consistent is critical for reusing code over time&lt;br /&gt;
#* This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of an inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” (put link to external definition) is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. {site from book}This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler.&lt;br /&gt;
Software Engineering Observations&lt;br /&gt;
In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. [2] Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
2http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
/*&lt;br /&gt;
Output           &lt;br /&gt;
       Inside Area for Rectangle.&lt;br /&gt;
       Area is  45&lt;br /&gt;
       Inside Area for Triangle.&lt;br /&gt;
       Area is 40&lt;br /&gt;
*/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
1. A car is a vehicle.&lt;br /&gt;
2. A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
&lt;br /&gt;
http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#[21.6](Circle and Ellipse)&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx (Is-A relationship)&lt;br /&gt;
&lt;br /&gt;
http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html(Inheritance)&lt;br /&gt;
&lt;br /&gt;
http://javaboutique.internet.com/tutorials/JavaOO/(LSP)&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=1(Inheritance vs Delegation)&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54682</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54682"/>
		<updated>2011-11-03T01:54:44Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance? ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.[1] A software library [external link] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the DRY {link to DRY} design principle. And although inheritance provides this capability a few things should be considered during the design process:&lt;br /&gt;
1.	Focus on well-defined general aspects of the software program,&lt;br /&gt;
a.	look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
1.	Name software asses appropriately, in example:&lt;br /&gt;
a.	Package should an category classification encapsulating all the data&lt;br /&gt;
b.	Interfaces should be unifying theme that group of classes&lt;br /&gt;
c.	Classes should be proper nouns&lt;br /&gt;
d.	Methods should be simple action verbs&lt;br /&gt;
1.	Being  consistent is critical for reusing code over time&lt;br /&gt;
a.	This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
 &lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” (put link to external definition) is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. {site from book}This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler.&lt;br /&gt;
Software Engineering Observations&lt;br /&gt;
In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. [2] Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
2http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
          &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
/*&lt;br /&gt;
Output           &lt;br /&gt;
       Inside Area for Rectangle.&lt;br /&gt;
       Area is  45&lt;br /&gt;
       Inside Area for Triangle.&lt;br /&gt;
       Area is 40&lt;br /&gt;
*/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
1. A car is a vehicle.&lt;br /&gt;
2. A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
&lt;br /&gt;
http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#[21.6](Circle and Ellipse)&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx (Is-A relationship)&lt;br /&gt;
&lt;br /&gt;
http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html(Inheritance)&lt;br /&gt;
&lt;br /&gt;
http://javaboutique.internet.com/tutorials/JavaOO/(LSP)&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=1(Inheritance vs Delegation)&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54681</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54681"/>
		<updated>2011-11-03T01:49:09Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* IS-A vs HAS-A */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance? ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.[1] A software library [external link] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the DRY {link to DRY} design principle. And although inheritance provides this capability a few things should be considered during the design process:&lt;br /&gt;
1.	Focus on well-defined general aspects of the software program,&lt;br /&gt;
a.	look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
1.	Name software asses appropriately, in example:&lt;br /&gt;
a.	Package should an category classification encapsulating all the data&lt;br /&gt;
b.	Interfaces should be unifying theme that group of classes&lt;br /&gt;
c.	Classes should be proper nouns&lt;br /&gt;
d.	Methods should be simple action verbs&lt;br /&gt;
1.	Being  consistent is critical for reusing code over time&lt;br /&gt;
a.	This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
 &lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
1. A car is a vehicle.&lt;br /&gt;
2. A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” (put link to external definition) is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. {site from book}This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler.&lt;br /&gt;
Software Engineering Observations&lt;br /&gt;
In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. [2] Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
2http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
           &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           &lt;br /&gt;
           Inside Area for Rectangle.&lt;br /&gt;
           Area is  45&lt;br /&gt;
           Inside Area for Triangle.&lt;br /&gt;
           Area is 40&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
&lt;br /&gt;
http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#[21.6](Circle and Ellipse)&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx (Is-A relationship)&lt;br /&gt;
&lt;br /&gt;
http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html(Inheritance)&lt;br /&gt;
&lt;br /&gt;
http://javaboutique.internet.com/tutorials/JavaOO/(LSP)&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=1(Inheritance vs Delegation)&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54680</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54680"/>
		<updated>2011-11-03T01:48:27Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== What is Inheritance? ==&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==IS-A vs HAS-A==&lt;br /&gt;
&lt;br /&gt;
Perspectives of Inheritance&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.[1] A software library [external link] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the DRY {link to DRY} design principle. And although inheritance provides this capability a few things should be considered during the design process:&lt;br /&gt;
1.	Focus on well-defined general aspects of the software program,&lt;br /&gt;
a.	look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
1.	Name software asses appropriately, in example:&lt;br /&gt;
a.	Package should an category classification encapsulating all the data&lt;br /&gt;
b.	Interfaces should be unifying theme that group of classes&lt;br /&gt;
c.	Classes should be proper nouns&lt;br /&gt;
d.	Methods should be simple action verbs&lt;br /&gt;
1.	Being  consistent is critical for reusing code over time&lt;br /&gt;
a.	This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
 &lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
1. A car is a vehicle.&lt;br /&gt;
2. A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” (put link to external definition) is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. {site from book}This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler.&lt;br /&gt;
Software Engineering Observations&lt;br /&gt;
In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. [2] Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
2http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
           &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           &lt;br /&gt;
           Inside Area for Rectangle.&lt;br /&gt;
           Area is  45&lt;br /&gt;
           Inside Area for Triangle.&lt;br /&gt;
           Area is 40&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Scenarios	&lt;br /&gt;
! Inheritance	&lt;br /&gt;
! Delegation&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Polymorphism	&lt;br /&gt;
| If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	&lt;br /&gt;
|If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Interface	&lt;br /&gt;
| If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	&lt;br /&gt;
|if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
! Efficiency&lt;br /&gt;
| If B is a subclass, there is direct execution of any inherited methods.&lt;br /&gt;
| if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
|-&lt;br /&gt;
! Amount of Code&lt;br /&gt;
| If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.&lt;br /&gt;
| If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
|-&lt;br /&gt;
! Dynamic Changeability&lt;br /&gt;
| If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods.&lt;br /&gt;
| If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
&lt;br /&gt;
http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#[21.6](Circle and Ellipse)&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx (Is-A relationship)&lt;br /&gt;
&lt;br /&gt;
http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html(Inheritance)&lt;br /&gt;
&lt;br /&gt;
http://javaboutique.internet.com/tutorials/JavaOO/(LSP)&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=1(Inheritance vs Delegation)&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54674</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54674"/>
		<updated>2011-11-03T01:29:09Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Inheritance vs Delegation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
When to use inheritance&lt;br /&gt;
&lt;br /&gt;
What is Inheritance?&lt;br /&gt;
Is-a vs Has-a&lt;br /&gt;
&lt;br /&gt;
Perspectives of Inheritance&lt;br /&gt;
1. Code Reuse&lt;br /&gt;
2. Is-A&lt;br /&gt;
3. Public Interfaces&lt;br /&gt;
4. Polymorphism&lt;br /&gt;
&lt;br /&gt;
Costs of Using Inheritance&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
--------------------------------------------------------------------------------------------------------&lt;br /&gt;
What is Inheritance?&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
==IS-A vs HAS-A==&lt;br /&gt;
&lt;br /&gt;
Perspectives of Inheritance&lt;br /&gt;
&lt;br /&gt;
==Code Reuse==&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.[1] A software library [external link] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the DRY {link to DRY} design principle. And although inheritance provides this capability a few things should be considered during the design process:&lt;br /&gt;
1.	Focus on well-defined general aspects of the software program,&lt;br /&gt;
a.	look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
1.	Name software asses appropriately, in example:&lt;br /&gt;
a.	Package should an category classification encapsulating all the data&lt;br /&gt;
b.	Interfaces should be unifying theme that group of classes&lt;br /&gt;
c.	Classes should be proper nouns&lt;br /&gt;
d.	Methods should be simple action verbs&lt;br /&gt;
1.	Being  consistent is critical for reusing code over time&lt;br /&gt;
a.	This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
 &lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== IS-A ==&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
1. A car is a vehicle.&lt;br /&gt;
2. A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
== Public Interfaces ==&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Polymorphism ==&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” (put link to external definition) is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. {site from book}This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler.&lt;br /&gt;
Software Engineering Observations&lt;br /&gt;
In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. [2] Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
2http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
           &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           &lt;br /&gt;
           Inside Area for Rectangle.&lt;br /&gt;
           Area is  45&lt;br /&gt;
           Inside Area for Triangle.&lt;br /&gt;
           Area is 40&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Costs of Using Inheritance ==&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
Scenarios	Inheritance	Delegation&lt;br /&gt;
Polymorphism	If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
Interface	If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
Efficiency	If B is a subclass, there is direct execution of any inherited methods.	if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
Amount of Code	If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.	If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
Dynamic Changeability	If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods. 	If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
&lt;br /&gt;
http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#[21.6](Circle and Ellipse)&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx (Is-A relationship)&lt;br /&gt;
&lt;br /&gt;
http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html(Inheritance)&lt;br /&gt;
&lt;br /&gt;
http://javaboutique.internet.com/tutorials/JavaOO/(LSP)&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=1(Inheritance vs Delegation)&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54671</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54671"/>
		<updated>2011-11-03T01:26:26Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;----------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
When to use inheritance&lt;br /&gt;
&lt;br /&gt;
What is Inheritance?&lt;br /&gt;
Is-a vs Has-a&lt;br /&gt;
&lt;br /&gt;
Perspectives of Inheritance&lt;br /&gt;
1. Code Reuse&lt;br /&gt;
2. Is-A&lt;br /&gt;
3. Public Interfaces&lt;br /&gt;
4. Polymorphism&lt;br /&gt;
&lt;br /&gt;
Costs of Using Inheritance&lt;br /&gt;
&lt;br /&gt;
== Inheritance vs Delegation ==&lt;br /&gt;
--------------------------------------------------------------------------------------------------------&lt;br /&gt;
What is Inheritance?&lt;br /&gt;
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.&lt;br /&gt;
&lt;br /&gt;
IS-A vs HAS-A&lt;br /&gt;
&lt;br /&gt;
Perspectives of Inheritance&lt;br /&gt;
Code Reuse&lt;br /&gt;
A milestone on the pathway to an elegant implementation of inheritance is software reuse. Software reuse, better known as code reuse, is the practice of using existing software or software knowledge to build new software programs.[1] A software library [external link] is a practical example of code reuse. Whether it is a large corporation or a collegiate engineering project, programmers utilize existing code repositories to reduce time spent during the development phase. In a well-designed multi-tier software program where inheritance is properly implemented code reuse is achieved through using the DRY {link to DRY} design principle. And although inheritance provides this capability a few things should be considered during the design process:&lt;br /&gt;
1.	Focus on well-defined general aspects of the software program,&lt;br /&gt;
a.	look for areas within the program where a library can be utilized or possibly developed for future reuse&lt;br /&gt;
1.	Name software asses appropriately, in example:&lt;br /&gt;
a.	Package should an category classification encapsulating all the data&lt;br /&gt;
b.	Interfaces should be unifying theme that group of classes&lt;br /&gt;
c.	Classes should be proper nouns&lt;br /&gt;
d.	Methods should be simple action verbs&lt;br /&gt;
1.	Being  consistent is critical for reusing code over time&lt;br /&gt;
a.	This is most common with products that have a long life-span and continually evolve in functionality&lt;br /&gt;
 &lt;br /&gt;
By no means are the items listed above a comprehensive guideline for the proper implementation of inheritance through code reuse. It is merely provided as potential assets to programmer looking for a foundational starting point. Any although inherit ace provides the ability to reuse any code; the practically of that implementation is strictly dependent on the programmer and the requirements of the application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Parcel_Dimension {&lt;br /&gt;
    double width;&lt;br /&gt;
    double height;&lt;br /&gt;
    double depth;&lt;br /&gt;
    &lt;br /&gt;
    // constructor when all dimension specified&lt;br /&gt;
    Parcel (double w, double h, double d){&lt;br /&gt;
        weight = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
    }&lt;br /&gt;
        &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ParcelWeight extends Parcel_Dimension {&lt;br /&gt;
    double weight; // weight of parcel&lt;br /&gt;
&lt;br /&gt;
    // constructor for parcel weight&lt;br /&gt;
    ParcelWeight(double w, double h, double d, double m)&lt;br /&gt;
        width = w;&lt;br /&gt;
        height = h;&lt;br /&gt;
        depth = d;&lt;br /&gt;
        weight = m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipping_Cost extends ParcelWeight {&lt;br /&gt;
    double cost;&lt;br /&gt;
    &lt;br /&gt;
    // constructor for shiping price&lt;br /&gt;
    Shipping_Cost(double w, double h, double d, double m, double c)&lt;br /&gt;
&lt;br /&gt;
    super(w, h, d, m);&lt;br /&gt;
        cost c;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Shipment_Printout{&lt;br /&gt;
    public static void main(String args[]){&lt;br /&gt;
        Shipment_cost letter1 = new Shipment_cost (10, 20, 15, 10, 3.66)&lt;br /&gt;
        &lt;br /&gt;
        System.out.println(&amp;quot; Weight of letter is: &amp;quot; + letter1.weight);&lt;br /&gt;
        System.out.println(&amp;quot; Shipping cost: &amp;quot; + letter1.cost);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
IS-A&lt;br /&gt;
Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends. When one object is a specialized version of another object, there is an &amp;quot;is a&amp;quot; relationship between them. Here are a few examples of the &amp;quot;is a&amp;quot; relationship:&lt;br /&gt;
&lt;br /&gt;
1. A car is a vehicle.&lt;br /&gt;
2. A rectangle is a shape.&lt;br /&gt;
&lt;br /&gt;
When an &amp;quot;is a&amp;quot; relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an &amp;quot;is a&amp;quot; relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.&lt;br /&gt;
&lt;br /&gt;
Consider the following  implementation of the Circle and Ellipse classes&lt;br /&gt;
using inheritance. In the implementation Circle will be the sub-classclass of the Ellipse, hence it doesn’t need to implement anything but a constructor. From a geometrical perspective, every circle “is a” ellipse. Furthermore, there are certainly good opportunities for code reuse between them. Therefore, it seems natural to make the Circle class a subclass of the Ellipse class. But is this a good decision?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Ellipse&lt;br /&gt;
{&lt;br /&gt;
private int x, y, width, height;&lt;br /&gt;
public Ellipse(int x, int y, int w, int h) {&lt;br /&gt;
this.x = x; this.y = y; width = w; height = h;&lt;br /&gt;
}&lt;br /&gt;
public int getWidth() { return width; }&lt;br /&gt;
public int getHeight() { return height; }&lt;br /&gt;
public void setSize(int w, int h) { width = w; height = h; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Circle extends Ellipse&lt;br /&gt;
{&lt;br /&gt;
public Circle(int x, int y, int width) {&lt;br /&gt;
super(x, y, width, width);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Because of the fact that subclasses inherit all methods of their superclasses, the Circle&lt;br /&gt;
class now inherits a setSize method that has two parameters. A call to this method&lt;br /&gt;
can make the width and height of the Circle unequal, a rather undesirable outcome.&lt;br /&gt;
A setSize method for Squares should just take one parameter.&lt;br /&gt;
This can be avoided by nullifying the negative effects of the inherited setSize method by overriding it in the subclass. For example, we might add the following method to the Circle class:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void setSize(int w, int h) &lt;br /&gt;
{&lt;br /&gt;
width = h;&lt;br /&gt;
height = h; &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
thereby allowing users to modify the size of the circle.&lt;br /&gt;
&lt;br /&gt;
Our problem here is that the subclass does not have behavior consistent with&lt;br /&gt;
the behavior of its superclass. Such consistent behavior is necessary for elegant&lt;br /&gt;
code. This thereby ensure the Principle of Least Astonishment.&lt;br /&gt;
&lt;br /&gt;
Let us consider the inconsistent behavior between the Ellipse Class and the Circle class, with respect to the Liskov's Substitution Principle(LSP) .&lt;br /&gt;
The setSize method of the Ellipse class has the behavior of modifying the&lt;br /&gt;
width independently of the height. A setSize method of the Cirlce class can-&lt;br /&gt;
not have that behavior and still preserve squareness, and therefore the Circle class’&lt;br /&gt;
setSize method does not do everything that the Ellipse class’ setSize&lt;br /&gt;
method does. The conclusion is that Circle should not be a subclass of Ellipse.&lt;br /&gt;
&lt;br /&gt;
Public Interfaces&lt;br /&gt;
              &lt;br /&gt;
If class S responds to all the messages that class C responds to, and then some, it seems appropriate for S to be a subclass of C. Let us consider the following case:&lt;br /&gt;
A video store application which has a Person class, with name, address fields. It also has a Staff class, which has name, address fields and a Customer Class which also name, address fields. A simple object oriented approach would be to say that a Customer &amp;quot;is a&amp;quot; Person also  Staff “is a” Customer, therefore create classes as so. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private String address;&lt;br /&gt;
public String getAddress() &lt;br /&gt;
{&lt;br /&gt;
  return address; &lt;br /&gt;
}&lt;br /&gt;
…&lt;br /&gt;
}&lt;br /&gt;
public class &lt;br /&gt;
public class Customer extends Person&lt;br /&gt;
{&lt;br /&gt;
private String CustomerID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Staff extends Person&lt;br /&gt;
{&lt;br /&gt;
private String StaffID;&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This system works fine until we have someone who is both a customer and a member of staff. Assuming that we don't really want our everyone list to have the same person in twice, once as a Customer and once as a Staff, we make an arbitrary choice between:&lt;br /&gt;
It would be overly complex and difficult to maintain if you attempt to have a set of derived classes that implement the People. This is especially true given that the above example is very simple - in most real applications, things will be more complex. In this case, we would go with different approach. I would implement the Person class and include in it a collection of &amp;quot;roles&amp;quot;. Each person could have one or more roles such as &amp;quot;Customer&amp;quot; and &amp;quot;Staff”. This will make it easier to add roles as new requirements are discovered. For example, you may simply have a base &amp;quot;Role&amp;quot; class, and derive new roles from them. In this way, the Person object can be considered to exist permanently, but the person’s roles can come and go. Furthermore, there is no duplication of data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Polymorphism&lt;br /&gt;
&lt;br /&gt;
Polymorphism, Greek for “many forms” (put link to external definition) is a feature that allows one interface to be used for a general class of actions. Moreover, the concept of polymorphism is often expressed by the phrase, “one interface, multiple methods,” which means implies that a general interface should be associated with a group of related actives. {site from book}This in turn provides a situation that aids in the reduction of a programs complexity by allowing the same interface to be used throughout different situations The responsibility of knowing which method needs to be evoke is left up to the compiler.&lt;br /&gt;
Software Engineering Observations&lt;br /&gt;
In respect to inheritance polymorphism should be used when a programmer needs to deal in generalities and let the execution-time environment handle the specifics. [2] Proper use of polymorphism promotes extensibility, and software that invokes polymorphic behavior is independent of the object types to which messages are sent.&lt;br /&gt;
2http://faculty.inverhills.mnscu.edu/speng/cs1126/Notes/Polymorphism/What%20is%20Polymorphism.htm&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Using run-time polymorphism&lt;br /&gt;
class Figure_2D{&lt;br /&gt;
           double dim1;&lt;br /&gt;
           double dim2;&lt;br /&gt;
           &lt;br /&gt;
           Figure_2D(double a, double b){&lt;br /&gt;
                       dim1 = a;&lt;br /&gt;
                       dim2 = b;&lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Area for Figure is undefined.&amp;quot;);&lt;br /&gt;
                       return 0;&lt;br /&gt;
           }&lt;br /&gt;
}&lt;br /&gt;
class Rectangle extends Figure_2D{&lt;br /&gt;
           Rectangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for rectangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Rectangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
class Triangle extends Figure_2D{&lt;br /&gt;
           Triangle (double a, double b){&lt;br /&gt;
                       super(a, b);&lt;br /&gt;
           }&lt;br /&gt;
           // overide area for right triangle&lt;br /&gt;
           double area(){    &lt;br /&gt;
                       System.out.println(&amp;quot;Inside Area for Triangle.&amp;quot;);&lt;br /&gt;
                       return dim1*dim2/2;&lt;br /&gt;
           }&lt;br /&gt;
}           &lt;br /&gt;
           &lt;br /&gt;
           &lt;br /&gt;
class FindAreas{&lt;br /&gt;
           public static void main (String args[]) {&lt;br /&gt;
                       Figure r new REctangel (9, 5);&lt;br /&gt;
                       Triangle t = new Triangle(10, 8);&lt;br /&gt;
                       Figure figref;&lt;br /&gt;
                       &lt;br /&gt;
                       figref = r;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());&lt;br /&gt;
                       figref = t;&lt;br /&gt;
                       System.out.println(&amp;quot;Area is&amp;quot; + figref.area());        &lt;br /&gt;
           }&lt;br /&gt;
           &lt;br /&gt;
           &lt;br /&gt;
           Inside Area for Rectangle.&lt;br /&gt;
           Area is  45&lt;br /&gt;
           Inside Area for Triangle.&lt;br /&gt;
           Area is 40&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Costs of Using Inheritance&lt;br /&gt;
There are even more costs to be considered when using inheritance, in addition to those we’ve already addressed. &lt;br /&gt;
&lt;br /&gt;
One problem with inheritance, especially a deep inheritance tree with many generations, is that the code for the methods of a class low in the tree is spread out among all its ancestors higher in the tree, which makes it harder for the reader of the code to follow the flow of execution. That is, suppose someone is reading code and sees that a method foo is invoked on an object. If the object’s class does not implement foo, then the reader needs to look to the object’s immediate superclass. If that class does not implement foo, then a further search up the inheritance hierarchy needs to be made. To complicate matters, foo may invoke another method bar on the same object. There need be little relationship between the locations of foo and bar in the inheritance tree, and so the reader again needs to start at the object’s class and search up the inheritance tree, to find the implementation of bar. Matters are even worse if the reader is not sure of the object’s class and knows, for example, only that the object could belong to any of the subclasses of a given class. In such cases, it is impossible to figure out exactly which method body of which class gets executed at any given time. &lt;br /&gt;
&lt;br /&gt;
Another problem with inheritance is that all subclasses are very tightly tied with their superclasses. This coupling comes from the fact that, to guarantee certain behavior in a subclass, that subclass needs to know significant parts of the implementation of the methods of the superclasses.&lt;br /&gt;
&lt;br /&gt;
Eg:&lt;br /&gt;
Let's examine the superclass and subclass coupling problems. The following Stack class extends Java's ArrayListclass to make it behave like a stack:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Stack extends ArrayList&lt;br /&gt;
{   private int stack_pointer = 0;&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; articles.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Even a class as simple as this one has problems. Consider what happens when a user leverages inheritance and uses theArrayList's clear() method to pop everything off the stack:&lt;br /&gt;
Stack a_stack = new Stack();&lt;br /&gt;
a_stack.push(&amp;quot;1&amp;quot;);&lt;br /&gt;
a_stack.push(&amp;quot;2&amp;quot;);&lt;br /&gt;
a_stack.clear();&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage. (Java's Stackclass has exactly this problem; don't use it.)&lt;br /&gt;
&lt;br /&gt;
Inheritance vs Delegation &lt;br /&gt;
Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS‐A relationship for re‐use; delegation uses the HAS‐A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, both are alternatives for fixing a problem, with one more appropriate than the other in some situations. In this chapter, we will study the nature of delegation, see how we can convert an inheriting class to a delegating one, compare the advantages and disadvantages of the two approaches, and identify scenarios in which they should be used.&lt;br /&gt;
&lt;br /&gt;
The main reason, as mentioned, before, for using inheritance is sharing of code among multiple classes. However, as shown in the figure, while inheritance implies code reusability, code reusability does not require inheritance. Delegation is an alternative approach to allow multiple classes to share code. In the case of inheritance, a reusing class has an IS‐A relationship with a reused class. Thus, it inherits code from the reused class. In the case of delegation, the reused class HAS‐A reference to the reused class.&lt;br /&gt;
This reference allows it to delegate tasks to the reused class.&lt;br /&gt;
We shall consider the same case of the Stack class extending the Java's ArrayListclass to make it behave like a stack. As discussed before, the problem is that the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. &lt;br /&gt;
One solution to the undesirable method-inheritance problem is for Stack to override all ArrayList methods that can modify the array's state, so the overrides either manipulate the stack pointer correctly or throw an exception. &lt;br /&gt;
&lt;br /&gt;
This approach has two disadvantages. First, if you override everything, the base class should really be an interface, not a class. There's no point in implementation inheritance if you don't use any of the inherited methods. Second, and more importantly, you don't want a stack to support all ArrayList methods. That pesky removeRange() method isn't useful, for example. The only reasonable way to implement a useless method is to have it throw an exception, since it should never be called. This approach effectively moves what would be a compile-time error into run-time. &lt;br /&gt;
&lt;br /&gt;
A better solution to the base-class issue is encapsulating the data structure instead of using inheritance. Here's a new-and-improved version of Stack:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;             &lt;br /&gt;
class Stack&lt;br /&gt;
{  &lt;br /&gt;
    private int stack_pointer = 0;&lt;br /&gt;
   private ArrayList the_data = new ArrayList();&lt;br /&gt;
   public void push( Object article )&lt;br /&gt;
   {   the_data.add( stack_pointer++, article );&lt;br /&gt;
   }&lt;br /&gt;
   public Object pop()&lt;br /&gt;
   {   return the_data.remove( --stack_pointer );&lt;br /&gt;
   }&lt;br /&gt;
   public void push_many( Object[] articles )&lt;br /&gt;
   {   for( int i = 0; i &amp;lt; o.length; ++i )&lt;br /&gt;
           push( articles[i] );&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us consider different scenarios under which inheritance and delegation fares each other:&lt;br /&gt;
Scenarios	Inheritance	Delegation&lt;br /&gt;
Polymorphism	If B is a subclass of A, then subtype polymorphism is possible and so an object of class B can be used anywhere an object of class A .	If B is composed with A, then this subtype polymorphism does not apply to B.&lt;br /&gt;
Interface	If B is a subclass of A, then B inherits all methods of A, and so the interface of B must include all the methods in the interface of A, whether B wants them all or not. Furthermore, it is usually not appropriate to “void out” or nullify the methods of A that B doesn’t want	if B is composed with A, then the public interface of B need not be related at all to the public interface of A, and so you have the flexibility to design B exactly the way you want.&lt;br /&gt;
Efficiency	If B is a subclass, there is direct execution of any inherited methods.	if B forwards requests to A, then the methods of B must call methods of A, which results in slightly higher overhead costs.&lt;br /&gt;
Amount of Code	If B is a subclass of A, you need to implement in B only the methods of B that aren’t already inherited from A.	If B forwards requests to A, then you must implement all of B’s methods yourself. Although many of these methods might merely call a corresponding method in A, there is still more code to write and therefore more chance of errors.&lt;br /&gt;
Dynamic Changeability	If B is a subclass of A, then at run-time, there is no way to change the behavior of the inherited methods. 	If B forwards requests to A, then at run-time, B can change the object of class A or a subclass of A to which it forwards requests.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
&lt;br /&gt;
http://www.kev.pulo.com.au/pp/RESOURCES/C++-FAQ/proper-inheritance.html#[21.6](Circle and Ellipse)&lt;br /&gt;
&lt;br /&gt;
http://msdn.microsoft.com/en-us/library/27db6csx(v=vs.80).aspx (Is-A relationship)&lt;br /&gt;
&lt;br /&gt;
http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Inheritance-intro.html(Inheritance)&lt;br /&gt;
&lt;br /&gt;
http://javaboutique.internet.com/tutorials/JavaOO/(LSP)&lt;br /&gt;
&lt;br /&gt;
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html?page=1(Inheritance vs Delegation)&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54641</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54641"/>
		<updated>2011-11-02T18:07:37Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: Created page with &amp;quot;test 1&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;test 1&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=54640</id>
		<title>CSC/ECE 517 Fall 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=54640"/>
		<updated>2011-11-02T18:07:19Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Link title]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a cs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ri]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b tj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c cm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c sj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c ka]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d sr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e vs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a sc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e dm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e an]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e lm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g vn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g jn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i zf]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g rn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h hs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d gs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b ns]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b jp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a av]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f jm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ad]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e kt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e gp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b qu]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c bs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2c rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a ca]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b rv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f vh]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3a oe]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h rr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4b ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i sd]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d mt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ls]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ch]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4c ap]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4h sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4e cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4e gs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4a ga]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f sl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4c dm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4g as]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4g nv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4g ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4h kp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4h as]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4j fw]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i lc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch17 5b uo]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch17 5b br]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch5 5d he]]&lt;br /&gt;
&lt;br /&gt;
*[[trial]]&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50350</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50350"/>
		<updated>2011-09-22T20:18:47Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Full Reference Information */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
The original Ruby on Rails platform was extracted from the web-based project-management tool called [http://en.wikipedia.org/wiki/Basecamp_(software) Basecamp] developed by 37signals. Ruby on Rails’ creator, David Heinemeier Hansson, began work on the Rails in 2003 and released it as open source code in 2004, but it wasn’t until 2005 that Hansson shared commit rights to the project&amp;lt;ref&amp;gt;Rails Core Team Profiles&amp;lt;/ref&amp;gt;. Hansson designed Ruby on Rails to be an out of the box development framework that includes everything a programmer needs to create database-driven web applications according to the Model-View-Control pattern of separation. Ruby on Rails is based on the two key paradigms of [http://en.wikipedia.org/wiki/Convention_over_Configuration Configuration over Convention] and [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself].&lt;br /&gt;
&lt;br /&gt;
=History of Rails Development=&lt;br /&gt;
Throughout the development of the Rails framework there have been four significant releases alongside of the usual patches, fixes, and upgrades. &lt;br /&gt;
&lt;br /&gt;
===Rails 1.0===&lt;br /&gt;
Initially, Rails 1.0 was released 15 months after the original source was made available. This version contained the structural foundation of what would become a powerful open source motivator for the Ruby platform.  Though there were patches and code adjustments in Rails 1.2 the next major release was Rails 2.0.&lt;br /&gt;
&lt;br /&gt;
===Rails 2.0 ===&lt;br /&gt;
Rails 2.0 was a major facelift for the framework and much of the effort went into improving the resources and polishing the overall package by making it more lean. Among some of these improvements was a focus on increasing security by adding a new module to work with HTTP Basic Authentication and providing a built-in mechanism for dealing with CRSF attacks. Other improvements were in the areas of changing certain syntactical nomenclature as well as simplifying the format for a few templates. &lt;br /&gt;
&lt;br /&gt;
===Rails 2.3===&lt;br /&gt;
The next major update was Rails 2.3 at it was the most structural update prior to Rails 3.0 being released. The two primary changes in the architecture of the Rails application was the complete integration of the Rack modular web server interface and a renewed support for Rails Engines&amp;lt;ref&amp;gt;Ruby on Rails 2.3 Release Notes&amp;lt;/ref&amp;gt;.  Alongside of these changes were updates to templates, the addition of built-in support for HTTP Digest Authentication, nested forms models, Metal, and improved caching performance among other things.&lt;br /&gt;
&lt;br /&gt;
===Rails 3.0===&lt;br /&gt;
Between Rails 2.3 and the release of Rails 3.0, Rails announced that it would merge with [http://en.wikipedia.org/wiki/Merb Merb] and that Rails 3.0 would be the successor to both Rails 2 and Merb. Merb which is short for Mongrel+Erb, key feature that prompted the join Rails was its component modularity, extensible API design, and vertical scalability.&lt;br /&gt;
&lt;br /&gt;
=Rails 2 vs. Rails 3=&lt;br /&gt;
&lt;br /&gt;
As you can see a lot changed since the first release of Rails. We will explore a few of the key areas that changed between Rails 2 and Rails 3 that will affect a developer's everyday coding.&lt;br /&gt;
&lt;br /&gt;
==Rails Application Scripts==&lt;br /&gt;
&lt;br /&gt;
When you create a new Rails application, a directory named &amp;lt;tt&amp;gt;script&amp;lt;/tt&amp;gt; is generated. In Rails 2, this directory is populated with a set of scripts that can be executed by the programmer. A few of the more important ones are as follows&amp;lt;ref&amp;gt;Ruby 2009, pp 257-258 &amp;lt;/ref&amp;gt;:&lt;br /&gt;
*&amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt;: Used to generate code for controllers, mailers, models, scaffolds and other sets of code.&lt;br /&gt;
*&amp;lt;tt&amp;gt;destroy&amp;lt;/tt&amp;gt;: Used to destroy code created by the &amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
*&amp;lt;tt&amp;gt;server&amp;lt;/tt&amp;gt;: Starts up the Rails application in a self-contained web server.&lt;br /&gt;
*&amp;lt;tt&amp;gt;plugin&amp;lt;/tt&amp;gt;: Helps with the installation and administration of plug-ins to the Rails framework.&lt;br /&gt;
*&amp;lt;tt&amp;gt;performance&amp;lt;/tt&amp;gt; directory: Contains scripts used by the programmer to help understand the performance characteristics of the application being built.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with all of these scripts and compiles them all into a single script named &amp;lt;tt&amp;gt;rails&amp;lt;/tt&amp;gt;. With this script, the programmer can access all of the functionality that was available with the separate scripts. To make things even easier, the necessity to call this script outright is even unnecessary. By using the operating systems installed &amp;lt;tt&amp;gt;'''rails'''&amp;lt;/tt&amp;gt; command (ex, &amp;lt;tt&amp;gt;/usr/bin/rails&amp;lt;/tt&amp;gt; on Linux systems) in the root of any Rails 3 application, it will know to call this script.&lt;br /&gt;
&lt;br /&gt;
==Gem Dependencies==&lt;br /&gt;
&lt;br /&gt;
The ability to extend the Rails framework using a Gem is a very powerful feature. In Rails 2, the &amp;lt;tt&amp;gt;config/environment.rb&amp;lt;/tt&amp;gt; file had to be edited in order to tell the Rails application to require specific Gems using the &amp;lt;tt&amp;gt;'''config.gem'''&amp;lt;/tt&amp;gt; method. To ensure that the Gems were actually installed on the system running the Rails application, the programmer had to execute the &amp;lt;tt&amp;gt;rake gem:install&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with the need to have that manual process of installing Gems by utilizing a file named &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; in the root of the application as well as the Bundler. The Bundler looks at the contents &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; and automatically installs any Gems that are missing from the system. This allows the programmer to focus on more important issues.&lt;br /&gt;
&lt;br /&gt;
==Routing DSL==&lt;br /&gt;
&lt;br /&gt;
The routing DSL for Rails went through a major reconditioning and has actually been renamed to Action Dispatch. This rewrite took the logic out of the Action Controller and is now a standalone piece of software resulting in a much cleaner implementation. As well, the definition of routes for each application are now named within your Application module instead of the Action Controller. The difference can be seen in the beginning line of the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
  map.resources :posts&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
AppName::Application.routes do&lt;br /&gt;
  resources :posts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With all these changes, the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file now has a new syntax. Here are some of the major differences&amp;lt;ref&amp;gt;Reza 2010&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Default Route===&lt;br /&gt;
&lt;br /&gt;
The differences between the default route are minor between the two versions, but the Rails 3 version is much more explicit, as the parentheses indicate parameters that are optional. Also worth noting is that this route is commented out in Rails 3 by default. The reasoning behind this is that there is a big push for a RESTful architecture with the Rails application and this type of route isn't really recommended for that kind of application. It can be uncommented but be warned that doing so will make all actions in every controller accessible via GET requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect ':controller/:action/:id'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match '/:controller(/:action(/:id))'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Regular Routes===&lt;br /&gt;
The way in which a regular route is defined is much more streamlined and readable. In Rails 2, you had to define a key for the controller and action. With Rails 3, this definition is streamlined with a &amp;lt;tt&amp;gt;:to&amp;lt;/tt&amp;gt; key where you define in a specific format the controller and action responsible for the request. Here is an example to illustrate this point:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect 'products/:id', :controller =&amp;gt; 'products', :action =&amp;gt; 'view'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'products/:id', :to =&amp;gt; 'catalog#view'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Routes===&lt;br /&gt;
Setting up named routes has changed a little bit as well. The rules for the regular routes apply here but the way that you define the name of the route has changed. Now, you utilize the &amp;lt;tt&amp;gt;:as&amp;lt;/tt&amp;gt; key to define the name of the route as shown here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.logout '/logout', :controller =&amp;gt; 'sessions', :action =&amp;gt; 'destroy'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'logout', :to =&amp;gt; 'sessions#destroy', :as =&amp;gt; &amp;quot;logout&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Empty Route===&lt;br /&gt;
The empty route defines where to route the application when a user navigates to the root of the website (for normal HTML sites, this is the same as the index page). Rails 2 had a quick way of defining it but in true Ruby fashion, Rails 3 makes it even simpler to define this route as you can see here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.root :controller =&amp;gt; &amp;quot;welcome&amp;quot;, :action =&amp;gt; 'show'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
root :to =&amp;gt; 'welcome#show'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RESTful Resources===&lt;br /&gt;
RESTful resources are very important and have been a part of Rails since version 1.2. Utilizing the &amp;lt;tt&amp;gt;rails generate scaffold&amp;lt;/tt&amp;gt; command, Rails will generate the route, controller, views and model necessary to have the basic REST functionality for a model. As you can see from the following example, defining a RESTful route is simple in either version. Rails 3 grants you the ability to use this line for multiple resources as well, saving lines of code and making it more readable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.resources :products&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
# Single resource definition&lt;br /&gt;
resources :products&lt;br /&gt;
# Multiple resource definitions&lt;br /&gt;
resources :products, :categories, :comments&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, Rails 3 provides you with the ability to extend the routing beyond the seven basic RESTful actions.  There are multiple ways in which you can extend this functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Defining extra collection RESTful actions within a block.&lt;br /&gt;
resources :products do&lt;br /&gt;
  collection do&lt;br /&gt;
    get  :sold&lt;br /&gt;
    post :on_offer&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# An inline extension.&lt;br /&gt;
resources :products do&lt;br /&gt;
  get :sold, :on =&amp;gt; :member&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Active Record==&lt;br /&gt;
Active Record, like all of the core Rails components, got a major overhaul. &lt;br /&gt;
&lt;br /&gt;
===Arel and the Query Interface===&lt;br /&gt;
The most prominent changes made that will be apparent to developers will be the integration with [https://github.com/nkallen/arel Arel]. With this integration, all of the core methods for Active Record return relations, allowing them to be chained together to develop complex queries. A full list and their functionality can be found in the [http://guides.rubyonrails.org/active_record_querying.html Rails Guide]. This will take the place of the options hash that would normally get passed in to one of the Active Record methods. To illustrate this, say we wanted to get a list of the first 10 products ordered alphabetically:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:order =&amp;gt; &amp;quot;name DESC&amp;quot;, :limit =&amp;gt; 10)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.order(&amp;quot;name DESC&amp;quot;).limit(10)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, there are a lot of methods that are losing the options parameter in their definitions&amp;lt;ref&amp;gt;Naik 2010&amp;lt;/ref&amp;gt;, including query methods find and all as well as calculation methods count and average. Also, the &amp;lt;tt&amp;gt;:all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;:first&amp;lt;/tt&amp;gt; parameters for the find method are being deprecated in favor of the &amp;lt;tt&amp;gt;all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;first&amp;lt;/tt&amp;gt; methods. Here are some examples of how things are changing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Changes to the find method.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:all)&lt;br /&gt;
Product.find(:first)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.all&lt;br /&gt;
Product.first&lt;br /&gt;
&lt;br /&gt;
# Examples of method options being removed.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.all(:joins =&amp;gt; :categories)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.joins(&amp;quot;categories&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Scopes===&lt;br /&gt;
The &amp;lt;tt&amp;gt;named_scope&amp;lt;/tt&amp;gt; definition has been changed to just &amp;lt;tt&amp;gt;scope&amp;lt;/tt&amp;gt;. The options hash is also being deprecated and should be replaced with the new methods provided by the Arel integration. Translating existing Rails 2 named scopes is easy enough:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
class Product&lt;br /&gt;
  named_scope :food, :conditions =&amp;gt; { :type =&amp;gt; &amp;quot;food&amp;quot; }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
class Product&lt;br /&gt;
  scope :food, where(:type =&amp;gt; &amp;quot;food&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==JavaScript Support&amp;lt;ref&amp;gt;Gadbois 2010&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
There are many JavaScript libraries available to programmers to make their web applications more dynamic and smooth looking. Whenever you created a Rails 2 application, it would automatically install the Prototype and Script.aculo.us frameworks. Rails also provided helpers for these two JavaScript frameworks so that a few method calls were all that you needed to make your application more fluid.&lt;br /&gt;
&lt;br /&gt;
Rails 3 introduces the idea of Unobtrusive JavaScript (UJS). The JavaScript becomes unobtrusive by not being written in with the HTML objects producing cleaner code that is easier to debug. This is mostly done with the utilization of HTML 5 custom attributes such as data-method, data-confirm, data-remote and data-disable-with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;http://host.com&amp;quot; id=&amp;quot;create-post&amp;quot; method=&amp;quot;post&amp;quot; data-remote=&amp;quot;true&amp;quot; data-confirm=&amp;quot;Are you sure you want to submit?&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By doing this, it also frees Rails from being dependent on the Prototype framework, allowing it to become ''JavaScript framework agnostic''. With its current implementation, the framework can support Prototype, jQuery and MooTools.&lt;br /&gt;
&lt;br /&gt;
It might also be helpful to point out that as of Rails 3.1, the jQuery framework has replaced Prototype as the default JavaScript framework. Programmers that wish to still use Prototype must remember to specify this when they create the Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new myapp -j prototype&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
[http://rubyonrails.org/core &amp;quot;The Core Team&amp;quot;]. Riding Rails. Retrieved September 2011.&lt;br /&gt;
&lt;br /&gt;
[http://guides.rubyonrails.org/2_3_release_notes.html Rails Ruby on Rails 2.3 Release Notes&amp;quot;]. Riding Rails. Retrieved September 2011.&lt;br /&gt;
&lt;br /&gt;
Gadbois, John. ''Using Unobtrusive JavaScript and AJAX with Rails 3'' http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Lindsaar, Mikel. &amp;quot;Ruby on Rails 3.0 Release Notes&amp;quot; http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
&lt;br /&gt;
Naik, Pratik. &amp;quot;Active Record Query Interface 3.0&amp;quot; http://m.onkey.org/active-record-query-interface , 2010.&lt;br /&gt;
&lt;br /&gt;
Reza, Rizwan. &amp;quot;The Lowdown on Routes in Rails 3&amp;quot; http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. ''Agile Web Development with Rails: Third Edition'': The Pragmatic Programmers LLC, 2009.&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
* [http://weblog.rubyonrails.org/2005/12/13/rails-1-0-party-like-its-one-oh-oh/ Rails 1.0: Party like it's one oh oh!]&lt;br /&gt;
* [http://weblog.rubyonrails.org/2007/12/7/rails-2-0-it-s-done Rails 2.0: It's done!] &lt;br /&gt;
* [http://weblog.rubyonrails.org/2009/3/16/rails-2-3-templates-engines-rack-metal-much-more Rails 2.3: Templates, Engines, Rack, Metal, much more!]&lt;br /&gt;
* [http://weblog.rubyonrails.org/2010/8/29/rails-3-0-it-s-done Rails 3.0: It's Ready]&lt;br /&gt;
&lt;br /&gt;
* [http://guides.rubyonrails.org/2_3_release_notes.html Rails Ruby on Rails 2.3 Release Notes]&lt;br /&gt;
* [http://guides.rubyonrails.org/3_0_release_notes.html Rails Ruby on Rails 3.0 Release Notes]&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50349</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50349"/>
		<updated>2011-09-22T20:16:00Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
The original Ruby on Rails platform was extracted from the web-based project-management tool called [http://en.wikipedia.org/wiki/Basecamp_(software) Basecamp] developed by 37signals. Ruby on Rails’ creator, David Heinemeier Hansson, began work on the Rails in 2003 and released it as open source code in 2004, but it wasn’t until 2005 that Hansson shared commit rights to the project&amp;lt;ref&amp;gt;Rails Core Team Profiles&amp;lt;/ref&amp;gt;. Hansson designed Ruby on Rails to be an out of the box development framework that includes everything a programmer needs to create database-driven web applications according to the Model-View-Control pattern of separation. Ruby on Rails is based on the two key paradigms of [http://en.wikipedia.org/wiki/Convention_over_Configuration Configuration over Convention] and [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself].&lt;br /&gt;
&lt;br /&gt;
=History of Rails Development=&lt;br /&gt;
Throughout the development of the Rails framework there have been four significant releases alongside of the usual patches, fixes, and upgrades. &lt;br /&gt;
&lt;br /&gt;
===Rails 1.0===&lt;br /&gt;
Initially, Rails 1.0 was released 15 months after the original source was made available. This version contained the structural foundation of what would become a powerful open source motivator for the Ruby platform.  Though there were patches and code adjustments in Rails 1.2 the next major release was Rails 2.0.&lt;br /&gt;
&lt;br /&gt;
===Rails 2.0 ===&lt;br /&gt;
Rails 2.0 was a major facelift for the framework and much of the effort went into improving the resources and polishing the overall package by making it more lean. Among some of these improvements was a focus on increasing security by adding a new module to work with HTTP Basic Authentication and providing a built-in mechanism for dealing with CRSF attacks. Other improvements were in the areas of changing certain syntactical nomenclature as well as simplifying the format for a few templates. &lt;br /&gt;
&lt;br /&gt;
===Rails 2.3===&lt;br /&gt;
The next major update was Rails 2.3 at it was the most structural update prior to Rails 3.0 being released. The two primary changes in the architecture of the Rails application was the complete integration of the Rack modular web server interface and a renewed support for Rails Engines&amp;lt;ref&amp;gt;Ruby on Rails 2.3 Release Notes&amp;lt;/ref&amp;gt;.  Alongside of these changes were updates to templates, the addition of built-in support for HTTP Digest Authentication, nested forms models, Metal, and improved caching performance among other things.&lt;br /&gt;
&lt;br /&gt;
===Rails 3.0===&lt;br /&gt;
Between Rails 2.3 and the release of Rails 3.0, Rails announced that it would merge with [http://en.wikipedia.org/wiki/Merb Merb] and that Rails 3.0 would be the successor to both Rails 2 and Merb. Merb which is short for Mongrel+Erb, key feature that prompted the join Rails was its component modularity, extensible API design, and vertical scalability.&lt;br /&gt;
&lt;br /&gt;
=Rails 2 vs. Rails 3=&lt;br /&gt;
&lt;br /&gt;
As you can see a lot changed since the first release of Rails. We will explore a few of the key areas that changed between Rails 2 and Rails 3 that will affect a developer's everyday coding.&lt;br /&gt;
&lt;br /&gt;
==Rails Application Scripts==&lt;br /&gt;
&lt;br /&gt;
When you create a new Rails application, a directory named &amp;lt;tt&amp;gt;script&amp;lt;/tt&amp;gt; is generated. In Rails 2, this directory is populated with a set of scripts that can be executed by the programmer. A few of the more important ones are as follows&amp;lt;ref&amp;gt;Ruby 2009, pp 257-258 &amp;lt;/ref&amp;gt;:&lt;br /&gt;
*&amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt;: Used to generate code for controllers, mailers, models, scaffolds and other sets of code.&lt;br /&gt;
*&amp;lt;tt&amp;gt;destroy&amp;lt;/tt&amp;gt;: Used to destroy code created by the &amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
*&amp;lt;tt&amp;gt;server&amp;lt;/tt&amp;gt;: Starts up the Rails application in a self-contained web server.&lt;br /&gt;
*&amp;lt;tt&amp;gt;plugin&amp;lt;/tt&amp;gt;: Helps with the installation and administration of plug-ins to the Rails framework.&lt;br /&gt;
*&amp;lt;tt&amp;gt;performance&amp;lt;/tt&amp;gt; directory: Contains scripts used by the programmer to help understand the performance characteristics of the application being built.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with all of these scripts and compiles them all into a single script named &amp;lt;tt&amp;gt;rails&amp;lt;/tt&amp;gt;. With this script, the programmer can access all of the functionality that was available with the separate scripts. To make things even easier, the necessity to call this script outright is even unnecessary. By using the operating systems installed &amp;lt;tt&amp;gt;'''rails'''&amp;lt;/tt&amp;gt; command (ex, &amp;lt;tt&amp;gt;/usr/bin/rails&amp;lt;/tt&amp;gt; on Linux systems) in the root of any Rails 3 application, it will know to call this script.&lt;br /&gt;
&lt;br /&gt;
==Gem Dependencies==&lt;br /&gt;
&lt;br /&gt;
The ability to extend the Rails framework using a Gem is a very powerful feature. In Rails 2, the &amp;lt;tt&amp;gt;config/environment.rb&amp;lt;/tt&amp;gt; file had to be edited in order to tell the Rails application to require specific Gems using the &amp;lt;tt&amp;gt;'''config.gem'''&amp;lt;/tt&amp;gt; method. To ensure that the Gems were actually installed on the system running the Rails application, the programmer had to execute the &amp;lt;tt&amp;gt;rake gem:install&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with the need to have that manual process of installing Gems by utilizing a file named &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; in the root of the application as well as the Bundler. The Bundler looks at the contents &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; and automatically installs any Gems that are missing from the system. This allows the programmer to focus on more important issues.&lt;br /&gt;
&lt;br /&gt;
==Routing DSL==&lt;br /&gt;
&lt;br /&gt;
The routing DSL for Rails went through a major reconditioning and has actually been renamed to Action Dispatch. This rewrite took the logic out of the Action Controller and is now a standalone piece of software resulting in a much cleaner implementation. As well, the definition of routes for each application are now named within your Application module instead of the Action Controller. The difference can be seen in the beginning line of the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
  map.resources :posts&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
AppName::Application.routes do&lt;br /&gt;
  resources :posts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With all these changes, the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file now has a new syntax. Here are some of the major differences&amp;lt;ref&amp;gt;Reza 2010&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Default Route===&lt;br /&gt;
&lt;br /&gt;
The differences between the default route are minor between the two versions, but the Rails 3 version is much more explicit, as the parentheses indicate parameters that are optional. Also worth noting is that this route is commented out in Rails 3 by default. The reasoning behind this is that there is a big push for a RESTful architecture with the Rails application and this type of route isn't really recommended for that kind of application. It can be uncommented but be warned that doing so will make all actions in every controller accessible via GET requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect ':controller/:action/:id'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match '/:controller(/:action(/:id))'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Regular Routes===&lt;br /&gt;
The way in which a regular route is defined is much more streamlined and readable. In Rails 2, you had to define a key for the controller and action. With Rails 3, this definition is streamlined with a &amp;lt;tt&amp;gt;:to&amp;lt;/tt&amp;gt; key where you define in a specific format the controller and action responsible for the request. Here is an example to illustrate this point:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect 'products/:id', :controller =&amp;gt; 'products', :action =&amp;gt; 'view'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'products/:id', :to =&amp;gt; 'catalog#view'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Routes===&lt;br /&gt;
Setting up named routes has changed a little bit as well. The rules for the regular routes apply here but the way that you define the name of the route has changed. Now, you utilize the &amp;lt;tt&amp;gt;:as&amp;lt;/tt&amp;gt; key to define the name of the route as shown here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.logout '/logout', :controller =&amp;gt; 'sessions', :action =&amp;gt; 'destroy'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'logout', :to =&amp;gt; 'sessions#destroy', :as =&amp;gt; &amp;quot;logout&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Empty Route===&lt;br /&gt;
The empty route defines where to route the application when a user navigates to the root of the website (for normal HTML sites, this is the same as the index page). Rails 2 had a quick way of defining it but in true Ruby fashion, Rails 3 makes it even simpler to define this route as you can see here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.root :controller =&amp;gt; &amp;quot;welcome&amp;quot;, :action =&amp;gt; 'show'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
root :to =&amp;gt; 'welcome#show'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RESTful Resources===&lt;br /&gt;
RESTful resources are very important and have been a part of Rails since version 1.2. Utilizing the &amp;lt;tt&amp;gt;rails generate scaffold&amp;lt;/tt&amp;gt; command, Rails will generate the route, controller, views and model necessary to have the basic REST functionality for a model. As you can see from the following example, defining a RESTful route is simple in either version. Rails 3 grants you the ability to use this line for multiple resources as well, saving lines of code and making it more readable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.resources :products&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
# Single resource definition&lt;br /&gt;
resources :products&lt;br /&gt;
# Multiple resource definitions&lt;br /&gt;
resources :products, :categories, :comments&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, Rails 3 provides you with the ability to extend the routing beyond the seven basic RESTful actions.  There are multiple ways in which you can extend this functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Defining extra collection RESTful actions within a block.&lt;br /&gt;
resources :products do&lt;br /&gt;
  collection do&lt;br /&gt;
    get  :sold&lt;br /&gt;
    post :on_offer&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# An inline extension.&lt;br /&gt;
resources :products do&lt;br /&gt;
  get :sold, :on =&amp;gt; :member&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Active Record==&lt;br /&gt;
Active Record, like all of the core Rails components, got a major overhaul. &lt;br /&gt;
&lt;br /&gt;
===Arel and the Query Interface===&lt;br /&gt;
The most prominent changes made that will be apparent to developers will be the integration with [https://github.com/nkallen/arel Arel]. With this integration, all of the core methods for Active Record return relations, allowing them to be chained together to develop complex queries. A full list and their functionality can be found in the [http://guides.rubyonrails.org/active_record_querying.html Rails Guide]. This will take the place of the options hash that would normally get passed in to one of the Active Record methods. To illustrate this, say we wanted to get a list of the first 10 products ordered alphabetically:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:order =&amp;gt; &amp;quot;name DESC&amp;quot;, :limit =&amp;gt; 10)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.order(&amp;quot;name DESC&amp;quot;).limit(10)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, there are a lot of methods that are losing the options parameter in their definitions&amp;lt;ref&amp;gt;Naik 2010&amp;lt;/ref&amp;gt;, including query methods find and all as well as calculation methods count and average. Also, the &amp;lt;tt&amp;gt;:all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;:first&amp;lt;/tt&amp;gt; parameters for the find method are being deprecated in favor of the &amp;lt;tt&amp;gt;all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;first&amp;lt;/tt&amp;gt; methods. Here are some examples of how things are changing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Changes to the find method.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:all)&lt;br /&gt;
Product.find(:first)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.all&lt;br /&gt;
Product.first&lt;br /&gt;
&lt;br /&gt;
# Examples of method options being removed.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.all(:joins =&amp;gt; :categories)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.joins(&amp;quot;categories&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Scopes===&lt;br /&gt;
The &amp;lt;tt&amp;gt;named_scope&amp;lt;/tt&amp;gt; definition has been changed to just &amp;lt;tt&amp;gt;scope&amp;lt;/tt&amp;gt;. The options hash is also being deprecated and should be replaced with the new methods provided by the Arel integration. Translating existing Rails 2 named scopes is easy enough:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
class Product&lt;br /&gt;
  named_scope :food, :conditions =&amp;gt; { :type =&amp;gt; &amp;quot;food&amp;quot; }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
class Product&lt;br /&gt;
  scope :food, where(:type =&amp;gt; &amp;quot;food&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==JavaScript Support&amp;lt;ref&amp;gt;Gadbois 2010&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
There are many JavaScript libraries available to programmers to make their web applications more dynamic and smooth looking. Whenever you created a Rails 2 application, it would automatically install the Prototype and Script.aculo.us frameworks. Rails also provided helpers for these two JavaScript frameworks so that a few method calls were all that you needed to make your application more fluid.&lt;br /&gt;
&lt;br /&gt;
Rails 3 introduces the idea of Unobtrusive JavaScript (UJS). The JavaScript becomes unobtrusive by not being written in with the HTML objects producing cleaner code that is easier to debug. This is mostly done with the utilization of HTML 5 custom attributes such as data-method, data-confirm, data-remote and data-disable-with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;http://host.com&amp;quot; id=&amp;quot;create-post&amp;quot; method=&amp;quot;post&amp;quot; data-remote=&amp;quot;true&amp;quot; data-confirm=&amp;quot;Are you sure you want to submit?&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By doing this, it also frees Rails from being dependent on the Prototype framework, allowing it to become ''JavaScript framework agnostic''. With its current implementation, the framework can support Prototype, jQuery and MooTools.&lt;br /&gt;
&lt;br /&gt;
It might also be helpful to point out that as of Rails 3.1, the jQuery framework has replaced Prototype as the default JavaScript framework. Programmers that wish to still use Prototype must remember to specify this when they create the Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new myapp -j prototype&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
[http://rubyonrails.org/core &amp;quot;The Core Team&amp;quot;]. Riding Rails. Retrieved September 2011.&lt;br /&gt;
&lt;br /&gt;
[http://weblog.rubyonrails.org/2009/3/16/rails-2-3-templates-engines-rack-metal-much-more &amp;quot;Rails 2.3: Templates, Engines, Rack, Metal, much more!&amp;quot;]. Riding Rails. Retrieved September 2011.&lt;br /&gt;
&lt;br /&gt;
Gadbois, John. ''Using Unobtrusive JavaScript and AJAX with Rails 3'' http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Lindsaar, Mikel. &amp;quot;Ruby on Rails 3.0 Release Notes&amp;quot; http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
&lt;br /&gt;
Naik, Pratik. &amp;quot;Active Record Query Interface 3.0&amp;quot; http://m.onkey.org/active-record-query-interface , 2010.&lt;br /&gt;
&lt;br /&gt;
Reza, Rizwan. &amp;quot;The Lowdown on Routes in Rails 3&amp;quot; http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. ''Agile Web Development with Rails: Third Edition'': The Pragmatic Programmers LLC, 2009.&lt;br /&gt;
&lt;br /&gt;
== External Links ==&lt;br /&gt;
* [http://weblog.rubyonrails.org/2005/12/13/rails-1-0-party-like-its-one-oh-oh/ Rails 1.0: Party like it's one oh oh!]&lt;br /&gt;
* [http://weblog.rubyonrails.org/2007/12/7/rails-2-0-it-s-done Rails 2.0: It's done!] &lt;br /&gt;
* [http://weblog.rubyonrails.org/2009/3/16/rails-2-3-templates-engines-rack-metal-much-more Rails 2.3: Templates, Engines, Rack, Metal, much more!]&lt;br /&gt;
* [http://weblog.rubyonrails.org/2010/8/29/rails-3-0-it-s-done Rails 3.0: It's Ready]&lt;br /&gt;
&lt;br /&gt;
* [http://guides.rubyonrails.org/2_3_release_notes.html Rails Ruby on Rails 2.3 Release Notes]&lt;br /&gt;
* [http://guides.rubyonrails.org/3_0_release_notes.html Rails Ruby on Rails 3.0 Release Notes]&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50344</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50344"/>
		<updated>2011-09-22T19:53:44Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
The original Ruby on Rails platform was extracted from the web-based project-management tool called [http://en.wikipedia.org/wiki/Basecamp_(software) Basecamp] developed by 37signals. Ruby on Rails’ creator, David Heinemeier Hansson, began work on the Rails in 2003 and released it as open source code in 2004, but it wasn’t until 2005 that Hansson shared commit rights to the project&amp;lt;ref&amp;gt;Rails Core Team Profiles&amp;lt;/ref&amp;gt;. Hansson designed Ruby on Rails to be an out of the box development framework that includes everything a programmer needs to create database-driven web applications according to the Model-View-Control pattern of separation. Ruby on Rails is based on the two key paradigms of [http://en.wikipedia.org/wiki/Convention_over_Configuration Configuration over Convention] and [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself].&lt;br /&gt;
&lt;br /&gt;
=History of Rails Development=&lt;br /&gt;
Throughout the development of the Rails framework there have been four significant releases alongside of the usual patches, fixes, and upgrades. &lt;br /&gt;
&lt;br /&gt;
===Rails 1.0===&lt;br /&gt;
Initially, Rails 1.0 was released 15 months after the original source was made available. This version contained the structural foundation of what would become a powerful open source motivator for the Ruby platform.  Though there were patches and code adjustments in Rails 1.2 the next major release was Rails 2.0.&lt;br /&gt;
&lt;br /&gt;
===Rails 2.0 ===&lt;br /&gt;
Rails 2.0 was a major facelift for the framework and much of the effort went into improving the resources and polishing the overall package by making it more lean. Among some of these improvements was a focus on increasing security by adding a new module to work with HTTP Basic Authentication and providing a built-in mechanism for dealing with CRSF attacks. Other improvements were in the areas of changing certain syntactical nomenclature as well as simplifying the format for a few templates. &lt;br /&gt;
&lt;br /&gt;
===Rails 2.3===&lt;br /&gt;
The next major update was Rails 2.3 at it was the most structural update prior to Rails 3.0 being released. The two primary changes in the architecture of the Rails application was the complete integration of the Rack modular web server interface and a renewed support for Rails Engines. {link 2} Alongside of these changes were updates to templates, the addition of built-in support for HTTP Digest Authentication, nested forms models, Metal, and improved caching performance among other things.&lt;br /&gt;
&lt;br /&gt;
===Rails 3.0===&lt;br /&gt;
Between Rails 2.3 and the release of Rails 3.0, Rails announced that it would merge with Merb and that Rails 3.0 would be the successor to both Rails 2 and Merb. Merb which is short for Mongrel+Erb, key feature that prompted the join Rails was its component modularity, extensible API design, and vertical scalability.&lt;br /&gt;
&lt;br /&gt;
=Rails 2 vs. Rails 3=&lt;br /&gt;
&lt;br /&gt;
As you can see a lot changed since the first release of Rails. We will explore a few of the key areas that changed between Rails 2 and Rails 3 that will affect a developer's everyday coding.&lt;br /&gt;
&lt;br /&gt;
==Rails Application Scripts==&lt;br /&gt;
&lt;br /&gt;
When you create a new Rails application, a directory named &amp;lt;tt&amp;gt;script&amp;lt;/tt&amp;gt; is generated. In Rails 2, this directory is populated with a set of scripts that can be executed by the programmer. A few of the more important ones are as follows&amp;lt;ref&amp;gt;Ruby 2009, pp 257-258 &amp;lt;/ref&amp;gt;:&lt;br /&gt;
*&amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt;: Used to generate code for controllers, mailers, models, scaffolds and other sets of code.&lt;br /&gt;
*&amp;lt;tt&amp;gt;destroy&amp;lt;/tt&amp;gt;: Used to destroy code created by the &amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
*&amp;lt;tt&amp;gt;server&amp;lt;/tt&amp;gt;: Starts up the Rails application in a self-contained web server.&lt;br /&gt;
*&amp;lt;tt&amp;gt;plugin&amp;lt;/tt&amp;gt;: Helps with the installation and administration of plug-ins to the Rails framework.&lt;br /&gt;
*&amp;lt;tt&amp;gt;performance&amp;lt;/tt&amp;gt; directory: Contains scripts used by the programmer to help understand the performance characteristics of the application being built.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with all of these scripts and compiles them all into a single script named &amp;lt;tt&amp;gt;rails&amp;lt;/tt&amp;gt;. With this script, the programmer can access all of the functionality that was available with the separate scripts. To make things even easier, the necessity to call this script outright is even unnecessary. By using the operating systems installed &amp;lt;tt&amp;gt;'''rails'''&amp;lt;/tt&amp;gt; command (ex, &amp;lt;tt&amp;gt;/usr/bin/rails&amp;lt;/tt&amp;gt; on Linux systems) in the root of any Rails 3 application, it will know to call this script.&lt;br /&gt;
&lt;br /&gt;
==Gem Dependencies==&lt;br /&gt;
&lt;br /&gt;
The ability to extend the Rails framework using a Gem is a very powerful feature. In Rails 2, the &amp;lt;tt&amp;gt;config/environment.rb&amp;lt;/tt&amp;gt; file had to be edited in order to tell the Rails application to require specific Gems using the &amp;lt;tt&amp;gt;'''config.gem'''&amp;lt;/tt&amp;gt; method. To ensure that the Gems were actually installed on the system running the Rails application, the programmer had to execute the &amp;lt;tt&amp;gt;rake gem:install&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with the need to have that manual process of installing Gems by utilizing a file named &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; in the root of the application as well as the Bundler. The Bundler looks at the contents &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; and automatically installs any Gems that are missing from the system. This allows the programmer to focus on more important issues.&lt;br /&gt;
&lt;br /&gt;
==Routing DSL==&lt;br /&gt;
&lt;br /&gt;
The routing DSL for Rails went through a major reconditioning and has actually been renamed to Action Dispatch. This rewrite took the logic out of the Action Controller and is now a standalone piece of software resulting in a much cleaner implementation. As well, the definition of routes for each application are now named within your Application module instead of the Action Controller. The difference can be seen in the beginning line of the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
  map.resources :posts&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
AppName::Application.routes do&lt;br /&gt;
  resources :posts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With all these changes, the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file now has a new syntax. Here are some of the major differences&amp;lt;ref&amp;gt;Reza 2010&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Default Route===&lt;br /&gt;
&lt;br /&gt;
The differences between the default route are minor between the two versions, but the Rails 3 version is much more explicit, as the parentheses indicate parameters that are optional. Also worth noting is that this route is commented out in Rails 3 by default. The reasoning behind this is that there is a big push for a RESTful architecture with the Rails application and this type of route isn't really recommended for that kind of application. It can be uncommented but be warned that doing so will make all actions in every controller accessible via GET requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect ':controller/:action/:id'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match '/:controller(/:action(/:id))'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Regular Routes===&lt;br /&gt;
The way in which a regular route is defined is much more streamlined and readable. In Rails 2, you had to define a key for the controller and action. With Rails 3, this definition is streamlined with a &amp;lt;tt&amp;gt;:to&amp;lt;/tt&amp;gt; key where you define in a specific format the controller and action responsible for the request. Here is an example to illustrate this point:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect 'products/:id', :controller =&amp;gt; 'products', :action =&amp;gt; 'view'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'products/:id', :to =&amp;gt; 'catalog#view'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Routes===&lt;br /&gt;
Setting up named routes has changed a little bit as well. The rules for the regular routes apply here but the way that you define the name of the route has changed. Now, you utilize the &amp;lt;tt&amp;gt;:as&amp;lt;/tt&amp;gt; key to define the name of the route as shown here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.logout '/logout', :controller =&amp;gt; 'sessions', :action =&amp;gt; 'destroy'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'logout', :to =&amp;gt; 'sessions#destroy', :as =&amp;gt; &amp;quot;logout&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Empty Route===&lt;br /&gt;
The empty route defines where to route the application when a user navigates to the root of the website (for normal HTML sites, this is the same as the index page). Rails 2 had a quick way of defining it but in true Ruby fashion, Rails 3 makes it even simpler to define this route as you can see here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.root :controller =&amp;gt; &amp;quot;welcome&amp;quot;, :action =&amp;gt; 'show'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
root :to =&amp;gt; 'welcome#show'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RESTful Resources===&lt;br /&gt;
RESTful resources are very important and have been a part of Rails since version 1.2. Utilizing the &amp;lt;tt&amp;gt;rails generate scaffold&amp;lt;/tt&amp;gt; command, Rails will generate the route, controller, views and model necessary to have the basic REST functionality for a model. As you can see from the following example, defining a RESTful route is simple in either version. Rails 3 grants you the ability to use this line for multiple resources as well, saving lines of code and making it more readable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.resources :products&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
# Single resource definition&lt;br /&gt;
resources :products&lt;br /&gt;
# Multiple resource definitions&lt;br /&gt;
resources :products, :categories, :comments&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, Rails 3 provides you with the ability to extend the routing beyond the seven basic RESTful actions.  There are multiple ways in which you can extend this functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Defining extra collection RESTful actions within a block.&lt;br /&gt;
resources :products do&lt;br /&gt;
  collection do&lt;br /&gt;
    get  :sold&lt;br /&gt;
    post :on_offer&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# An inline extension.&lt;br /&gt;
resources :products do&lt;br /&gt;
  get :sold, :on =&amp;gt; :member&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Active Record==&lt;br /&gt;
Active Record, like all of the core Rails components, got a major overhaul. &lt;br /&gt;
&lt;br /&gt;
===Arel and the Query Interface===&lt;br /&gt;
The most prominent changes made that will be apparent to developers will be the integration with [https://github.com/nkallen/arel Arel]. With this integration, all of the core methods for Active Record return relations, allowing them to be chained together to develop complex queries. A full list and their functionality can be found in the [http://guides.rubyonrails.org/active_record_querying.html Rails Guide]. This will take the place of the options hash that would normally get passed in to one of the Active Record methods. To illustrate this, say we wanted to get a list of the first 10 products ordered alphabetically:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:order =&amp;gt; &amp;quot;name DESC&amp;quot;, :limit =&amp;gt; 10)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.order(&amp;quot;name DESC&amp;quot;).limit(10)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, there are a lot of methods that are losing the options parameter in their definitions&amp;lt;ref&amp;gt;Naik 2010&amp;lt;/ref&amp;gt;, including query methods find and all as well as calculation methods count and average. Also, the &amp;lt;tt&amp;gt;:all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;:first&amp;lt;/tt&amp;gt; parameters for the find method are being deprecated in favor of the &amp;lt;tt&amp;gt;all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;first&amp;lt;/tt&amp;gt; methods. Here are some examples of how things are changing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Changes to the find method.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:all)&lt;br /&gt;
Product.find(:first)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.all&lt;br /&gt;
Product.first&lt;br /&gt;
&lt;br /&gt;
# Examples of method options being removed.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.all(:joins =&amp;gt; :categories)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.joins(&amp;quot;categories&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Scopes===&lt;br /&gt;
The &amp;lt;tt&amp;gt;named_scope&amp;lt;/tt&amp;gt; definition has been changed to just &amp;lt;tt&amp;gt;scope&amp;lt;/tt&amp;gt;. The options hash is also being deprecated and should be replaced with the new methods provided by the Arel integration. Translating existing Rails 2 named scopes is easy enough:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
class Product&lt;br /&gt;
  named_scope :food, :conditions =&amp;gt; { :type =&amp;gt; &amp;quot;food&amp;quot; }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
class Product&lt;br /&gt;
  scope :food, where(:type =&amp;gt; &amp;quot;food&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==JavaScript Support&amp;lt;ref&amp;gt;Gadbois 2010&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
There are many JavaScript libraries available to programmers to make their web applications more dynamic and smooth looking. Whenever you created a Rails 2 application, it would automatically install the Prototype and Script.aculo.us frameworks. Rails also provided helpers for these two JavaScript frameworks so that a few method calls were all that you needed to make your application more fluid.&lt;br /&gt;
&lt;br /&gt;
Rails 3 introduces the idea of Unobtrusive JavaScript (UJS). The JavaScript becomes unobtrusive by not being written in with the HTML objects producing cleaner code that is easier to debug. This is mostly done with the utilization of HTML 5 custom attributes such as data-method, data-confirm, data-remote and data-disable-with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;http://host.com&amp;quot; id=&amp;quot;create-post&amp;quot; method=&amp;quot;post&amp;quot; data-remote=&amp;quot;true&amp;quot; data-confirm=&amp;quot;Are you sure you want to submit?&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By doing this, it also frees Rails from being dependent on the Prototype framework, allowing it to become ''JavaScript framework agnostic''. With its current implementation, the framework can support Prototype, jQuery and MooTools.&lt;br /&gt;
&lt;br /&gt;
It might also be helpful to point out that as of Rails 3.1, the jQuery framework has replaced Prototype as the default JavaScript framework. Programmers that wish to still use Prototype must remember to specify this when they create the Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new myapp -j prototype&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&amp;quot;The Core Team&amp;quot;. Riding Rails. Retrieved September 2011.http://rubyonrails.org/core&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 1.0: Party like it's one oh oh!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2005/12/13/rails-1-0-party-like-its-one-oh-oh/&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.0: It's done!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2007/12/7/rails-2-0-it-s-done&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.3: Templates, Engines, Rack, Metal, much more!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2009/3/16/rails-2-3-templates-engines-rack-metal-much-more&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails Ruby on Rails 2.3 Release Notes&amp;quot; Riding Rails. Retrieved September 2011. http://guides.rubyonrails.org/2_3_release_notes.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Gadbois, John. ''Using Unobtrusive JavaScript and AJAX with Rails 3'' http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Lindsaar, Mikel. &amp;quot;Ruby on Rails 3.0 Release Notes&amp;quot; http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
&lt;br /&gt;
Naik, Pratik. &amp;quot;Active Record Query Interface 3.0&amp;quot; http://m.onkey.org/active-record-query-interface , 2010.&lt;br /&gt;
&lt;br /&gt;
Reza, Rizwan. &amp;quot;The Lowdown on Routes in Rails 3&amp;quot; http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. ''Agile Web Development with Rails: Third Edition'': The Pragmatic Programmers LLC, 2009.&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50343</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50343"/>
		<updated>2011-09-22T19:48:56Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Full Reference Information */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
The original Ruby on Rails platform was extracted from the web-based project-management tool called [http://en.wikipedia.org/wiki/Basecamp_(software) Basecamp] developed by 37signals. Ruby on Rails’ creator, David Heinemeier Hansson, began work on the Rails in early 2003 and released it as open source code in 2004, but it wasn’t until 2005 that Hansson shared commit rights to the project&amp;lt;ref&amp;gt;Rails Core Team &amp;lt;/ref&amp;gt;. Hansson designed Ruby on Rails to be an out of the box development framework that includes everything a programmer needs to create database-driven web applications according to the Model-View-Control pattern of separation. Ruby on Rails is based on the two key paradigms of [http://en.wikipedia.org/wiki/Convention_over_Configuration Configuration over Convention] and [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself].&lt;br /&gt;
&lt;br /&gt;
=History of Rails Development=&lt;br /&gt;
Throughout the development of the Rails framework there have been four significant releases alongside of the usual patches, fixes, and upgrades. &lt;br /&gt;
&lt;br /&gt;
===Rails 1.0===&lt;br /&gt;
Initially, Rails 1.0 was released 15 months after the original source was made available &amp;lt;ref&amp;gt;Rails 1.0 &amp;lt;/ref&amp;gt;. This version contained the structural foundation of what would become a powerful open source motivator for the Ruby platform.  Though there were patches and code adjustments in Rails 1.2 the next major release was Rails 2.0.&lt;br /&gt;
&lt;br /&gt;
===Rails 2.0 ===&lt;br /&gt;
Rails 2.0 was a major facelift for the framework and much of the effort went into improving the resources and polishing the overall package by making it more lean. Among some of these improvements was a focus on increasing security by adding a new module to work with HTTP Basic Authentication and providing a built-in mechanism for dealing with CRSF attacks. Other improvements were in the areas of changing certain syntactical nomenclature as well as simplifying the format for a few templates. &lt;br /&gt;
&lt;br /&gt;
===Rails 2.3===&lt;br /&gt;
The next major update was Rails 2.3 at it was the most structural update prior to Rails 3.0 being released. The two primary changes in the architecture of the Rails application was the complete integration of the Rack modular web server interface and a renewed support for Rails Engines. {link 2} Alongside of these changes were updates to templates, the addition of built-in support for HTTP Digest Authentication, nested forms models, Metal, and improved caching performance among other things.&lt;br /&gt;
&lt;br /&gt;
===Rails 3.0===&lt;br /&gt;
Between Rails 2.3 and the release of Rails 3.0, Rails announced that it would merge with Merb and that Rails 3.0 would be the successor to both Rails 2 and Merb. Merb which is short for Mongrel+Erb, key feature that prompted the join Rails was its component modularity, extensible API design, and vertical scalability.&lt;br /&gt;
&lt;br /&gt;
=Rails 2 vs. Rails 3=&lt;br /&gt;
&lt;br /&gt;
As you can see a lot changed since the first release of Rails. We will explore a few of the key areas that changed between Rails 2 and Rails 3 that will affect a developer's everyday coding.&lt;br /&gt;
&lt;br /&gt;
==Rails Application Scripts==&lt;br /&gt;
&lt;br /&gt;
When you create a new Rails application, a directory named &amp;lt;tt&amp;gt;script&amp;lt;/tt&amp;gt; is generated. In Rails 2, this directory is populated with a set of scripts that can be executed by the programmer. A few of the more important ones are as follows&amp;lt;ref&amp;gt;Ruby 2009, pp 257-258 &amp;lt;/ref&amp;gt;:&lt;br /&gt;
*&amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt;: Used to generate code for controllers, mailers, models, scaffolds and other sets of code.&lt;br /&gt;
*&amp;lt;tt&amp;gt;destroy&amp;lt;/tt&amp;gt;: Used to destroy code created by the &amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
*&amp;lt;tt&amp;gt;server&amp;lt;/tt&amp;gt;: Starts up the Rails application in a self-contained web server.&lt;br /&gt;
*&amp;lt;tt&amp;gt;plugin&amp;lt;/tt&amp;gt;: Helps with the installation and administration of plug-ins to the Rails framework.&lt;br /&gt;
*&amp;lt;tt&amp;gt;performance&amp;lt;/tt&amp;gt; directory: Contains scripts used by the programmer to help understand the performance characteristics of the application being built.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with all of these scripts and compiles them all into a single script named &amp;lt;tt&amp;gt;rails&amp;lt;/tt&amp;gt;. With this script, the programmer can access all of the functionality that was available with the separate scripts. To make things even easier, the necessity to call this script outright is even unnecessary. By using the operating systems installed &amp;lt;tt&amp;gt;'''rails'''&amp;lt;/tt&amp;gt; command (ex, &amp;lt;tt&amp;gt;/usr/bin/rails&amp;lt;/tt&amp;gt; on Linux systems) in the root of any Rails 3 application, it will know to call this script.&lt;br /&gt;
&lt;br /&gt;
==Gem Dependencies==&lt;br /&gt;
&lt;br /&gt;
The ability to extend the Rails framework using a Gem is a very powerful feature. In Rails 2, the &amp;lt;tt&amp;gt;config/environment.rb&amp;lt;/tt&amp;gt; file had to be edited in order to tell the Rails application to require specific Gems using the &amp;lt;tt&amp;gt;'''config.gem'''&amp;lt;/tt&amp;gt; method. To ensure that the Gems were actually installed on the system running the Rails application, the programmer had to execute the &amp;lt;tt&amp;gt;rake gem:install&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with the need to have that manual process of installing Gems by utilizing a file named &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; in the root of the application as well as the Bundler. The Bundler looks at the contents &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; and automatically installs any Gems that are missing from the system. This allows the programmer to focus on more important issues.&lt;br /&gt;
&lt;br /&gt;
==Routing DSL==&lt;br /&gt;
&lt;br /&gt;
The routing DSL for Rails went through a major reconditioning and has actually been renamed to Action Dispatch. This rewrite took the logic out of the Action Controller and is now a standalone piece of software resulting in a much cleaner implementation. As well, the definition of routes for each application are now named within your Application module instead of the Action Controller. The difference can be seen in the beginning line of the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
  map.resources :posts&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
AppName::Application.routes do&lt;br /&gt;
  resources :posts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With all these changes, the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file now has a new syntax. Here are some of the major differences&amp;lt;ref&amp;gt;Reza 2010&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Default Route===&lt;br /&gt;
&lt;br /&gt;
The differences between the default route are minor between the two versions, but the Rails 3 version is much more explicit, as the parentheses indicate parameters that are optional. Also worth noting is that this route is commented out in Rails 3 by default. The reasoning behind this is that there is a big push for a RESTful architecture with the Rails application and this type of route isn't really recommended for that kind of application. It can be uncommented but be warned that doing so will make all actions in every controller accessible via GET requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect ':controller/:action/:id'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match '/:controller(/:action(/:id))'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Regular Routes===&lt;br /&gt;
The way in which a regular route is defined is much more streamlined and readable. In Rails 2, you had to define a key for the controller and action. With Rails 3, this definition is streamlined with a &amp;lt;tt&amp;gt;:to&amp;lt;/tt&amp;gt; key where you define in a specific format the controller and action responsible for the request. Here is an example to illustrate this point:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect 'products/:id', :controller =&amp;gt; 'products', :action =&amp;gt; 'view'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'products/:id', :to =&amp;gt; 'catalog#view'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Routes===&lt;br /&gt;
Setting up named routes has changed a little bit as well. The rules for the regular routes apply here but the way that you define the name of the route has changed. Now, you utilize the &amp;lt;tt&amp;gt;:as&amp;lt;/tt&amp;gt; key to define the name of the route as shown here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.logout '/logout', :controller =&amp;gt; 'sessions', :action =&amp;gt; 'destroy'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'logout', :to =&amp;gt; 'sessions#destroy', :as =&amp;gt; &amp;quot;logout&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Empty Route===&lt;br /&gt;
The empty route defines where to route the application when a user navigates to the root of the website (for normal HTML sites, this is the same as the index page). Rails 2 had a quick way of defining it but in true Ruby fashion, Rails 3 makes it even simpler to define this route as you can see here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.root :controller =&amp;gt; &amp;quot;welcome&amp;quot;, :action =&amp;gt; 'show'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
root :to =&amp;gt; 'welcome#show'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RESTful Resources===&lt;br /&gt;
RESTful resources are very important and have been a part of Rails since version 1.2. Utilizing the &amp;lt;tt&amp;gt;rails generate scaffold&amp;lt;/tt&amp;gt; command, Rails will generate the route, controller, views and model necessary to have the basic REST functionality for a model. As you can see from the following example, defining a RESTful route is simple in either version. Rails 3 grants you the ability to use this line for multiple resources as well, saving lines of code and making it more readable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.resources :products&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
# Single resource definition&lt;br /&gt;
resources :products&lt;br /&gt;
# Multiple resource definitions&lt;br /&gt;
resources :products, :categories, :comments&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, Rails 3 provides you with the ability to extend the routing beyond the seven basic RESTful actions.  There are multiple ways in which you can extend this functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Defining extra collection RESTful actions within a block.&lt;br /&gt;
resources :products do&lt;br /&gt;
  collection do&lt;br /&gt;
    get  :sold&lt;br /&gt;
    post :on_offer&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# An inline extension.&lt;br /&gt;
resources :products do&lt;br /&gt;
  get :sold, :on =&amp;gt; :member&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Active Record==&lt;br /&gt;
Active Record, like all of the core Rails components, got a major overhaul. &lt;br /&gt;
&lt;br /&gt;
===Arel and the Query Interface===&lt;br /&gt;
The most prominent changes made that will be apparent to developers will be the integration with [https://github.com/nkallen/arel Arel]. With this integration, all of the core methods for Active Record return relations, allowing them to be chained together to develop complex queries. A full list and their functionality can be found in the [http://guides.rubyonrails.org/active_record_querying.html Rails Guide]. This will take the place of the options hash that would normally get passed in to one of the Active Record methods. To illustrate this, say we wanted to get a list of the first 10 products ordered alphabetically:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:order =&amp;gt; &amp;quot;name DESC&amp;quot;, :limit =&amp;gt; 10)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.order(&amp;quot;name DESC&amp;quot;).limit(10)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, there are a lot of methods that are losing the options parameter in their definitions&amp;lt;ref&amp;gt;Naik 2010&amp;lt;/ref&amp;gt;, including query methods find and all as well as calculation methods count and average. Also, the &amp;lt;tt&amp;gt;:all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;:first&amp;lt;/tt&amp;gt; parameters for the find method are being deprecated in favor of the &amp;lt;tt&amp;gt;all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;first&amp;lt;/tt&amp;gt; methods. Here are some examples of how things are changing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Changes to the find method.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:all)&lt;br /&gt;
Product.find(:first)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.all&lt;br /&gt;
Product.first&lt;br /&gt;
&lt;br /&gt;
# Examples of method options being removed.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.all(:joins =&amp;gt; :categories)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.joins(&amp;quot;categories&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Scopes===&lt;br /&gt;
The &amp;lt;tt&amp;gt;named_scope&amp;lt;/tt&amp;gt; definition has been changed to just &amp;lt;tt&amp;gt;scope&amp;lt;/tt&amp;gt;. The options hash is also being deprecated and should be replaced with the new methods provided by the Arel integration. Translating existing Rails 2 named scopes is easy enough:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
class Product&lt;br /&gt;
  named_scope :food, :conditions =&amp;gt; { :type =&amp;gt; &amp;quot;food&amp;quot; }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
class Product&lt;br /&gt;
  scope :food, where(:type =&amp;gt; &amp;quot;food&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==JavaScript Support&amp;lt;ref&amp;gt;Gadbois 2010&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
There are many JavaScript libraries available to programmers to make their web applications more dynamic and smooth looking. Whenever you created a Rails 2 application, it would automatically install the Prototype and Script.aculo.us frameworks. Rails also provided helpers for these two JavaScript frameworks so that a few method calls were all that you needed to make your application more fluid.&lt;br /&gt;
&lt;br /&gt;
Rails 3 introduces the idea of Unobtrusive JavaScript (UJS). The JavaScript becomes unobtrusive by not being written in with the HTML objects producing cleaner code that is easier to debug. This is mostly done with the utilization of HTML 5 custom attributes such as data-method, data-confirm, data-remote and data-disable-with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;http://host.com&amp;quot; id=&amp;quot;create-post&amp;quot; method=&amp;quot;post&amp;quot; data-remote=&amp;quot;true&amp;quot; data-confirm=&amp;quot;Are you sure you want to submit?&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By doing this, it also frees Rails from being dependent on the Prototype framework, allowing it to become ''JavaScript framework agnostic''. With its current implementation, the framework can support Prototype, jQuery and MooTools.&lt;br /&gt;
&lt;br /&gt;
It might also be helpful to point out that as of Rails 3.1, the jQuery framework has replaced Prototype as the default JavaScript framework. Programmers that wish to still use Prototype must remember to specify this when they create the Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new myapp -j prototype&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&amp;quot;The Core Team&amp;quot;. Riding Rails. Retrieved September 2011.http://rubyonrails.org/core&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 1.0: Party like it's one oh oh!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2005/12/13/rails-1-0-party-like-its-one-oh-oh/&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.0: It's done!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2007/12/7/rails-2-0-it-s-done&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.3: Templates, Engines, Rack, Metal, much more!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2009/3/16/rails-2-3-templates-engines-rack-metal-much-more&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails Ruby on Rails 2.3 Release Notes&amp;quot; Riding Rails. Retrieved September 2011. http://guides.rubyonrails.org/2_3_release_notes.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Gadbois, John. ''Using Unobtrusive JavaScript and AJAX with Rails 3'' http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Lindsaar, Mikel. &amp;quot;Ruby on Rails 3.0 Release Notes&amp;quot; http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
&lt;br /&gt;
Naik, Pratik. &amp;quot;Active Record Query Interface 3.0&amp;quot; http://m.onkey.org/active-record-query-interface , 2010.&lt;br /&gt;
&lt;br /&gt;
Reza, Rizwan. &amp;quot;The Lowdown on Routes in Rails 3&amp;quot; http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. ''Agile Web Development with Rails: Third Edition'': The Pragmatic Programmers LLC, 2009.&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50342</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50342"/>
		<updated>2011-09-22T19:47:01Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
The original Ruby on Rails platform was extracted from the web-based project-management tool called [http://en.wikipedia.org/wiki/Basecamp_(software) Basecamp] developed by 37signals. Ruby on Rails’ creator, David Heinemeier Hansson, began work on the Rails in early 2003 and released it as open source code in 2004, but it wasn’t until 2005 that Hansson shared commit rights to the project&amp;lt;ref&amp;gt;Rails Core Team &amp;lt;/ref&amp;gt;. Hansson designed Ruby on Rails to be an out of the box development framework that includes everything a programmer needs to create database-driven web applications according to the Model-View-Control pattern of separation. Ruby on Rails is based on the two key paradigms of [http://en.wikipedia.org/wiki/Convention_over_Configuration Configuration over Convention] and [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself].&lt;br /&gt;
&lt;br /&gt;
=History of Rails Development=&lt;br /&gt;
Throughout the development of the Rails framework there have been four significant releases alongside of the usual patches, fixes, and upgrades. &lt;br /&gt;
&lt;br /&gt;
===Rails 1.0===&lt;br /&gt;
Initially, Rails 1.0 was released 15 months after the original source was made available &amp;lt;ref&amp;gt;Rails 1.0 &amp;lt;/ref&amp;gt;. This version contained the structural foundation of what would become a powerful open source motivator for the Ruby platform.  Though there were patches and code adjustments in Rails 1.2 the next major release was Rails 2.0.&lt;br /&gt;
&lt;br /&gt;
===Rails 2.0 ===&lt;br /&gt;
Rails 2.0 was a major facelift for the framework and much of the effort went into improving the resources and polishing the overall package by making it more lean. Among some of these improvements was a focus on increasing security by adding a new module to work with HTTP Basic Authentication and providing a built-in mechanism for dealing with CRSF attacks. Other improvements were in the areas of changing certain syntactical nomenclature as well as simplifying the format for a few templates. &lt;br /&gt;
&lt;br /&gt;
===Rails 2.3===&lt;br /&gt;
The next major update was Rails 2.3 at it was the most structural update prior to Rails 3.0 being released. The two primary changes in the architecture of the Rails application was the complete integration of the Rack modular web server interface and a renewed support for Rails Engines. {link 2} Alongside of these changes were updates to templates, the addition of built-in support for HTTP Digest Authentication, nested forms models, Metal, and improved caching performance among other things.&lt;br /&gt;
&lt;br /&gt;
===Rails 3.0===&lt;br /&gt;
Between Rails 2.3 and the release of Rails 3.0, Rails announced that it would merge with Merb and that Rails 3.0 would be the successor to both Rails 2 and Merb. Merb which is short for Mongrel+Erb, key feature that prompted the join Rails was its component modularity, extensible API design, and vertical scalability.&lt;br /&gt;
&lt;br /&gt;
=Rails 2 vs. Rails 3=&lt;br /&gt;
&lt;br /&gt;
As you can see a lot changed since the first release of Rails. We will explore a few of the key areas that changed between Rails 2 and Rails 3 that will affect a developer's everyday coding.&lt;br /&gt;
&lt;br /&gt;
==Rails Application Scripts==&lt;br /&gt;
&lt;br /&gt;
When you create a new Rails application, a directory named &amp;lt;tt&amp;gt;script&amp;lt;/tt&amp;gt; is generated. In Rails 2, this directory is populated with a set of scripts that can be executed by the programmer. A few of the more important ones are as follows&amp;lt;ref&amp;gt;Ruby 2009, pp 257-258 &amp;lt;/ref&amp;gt;:&lt;br /&gt;
*&amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt;: Used to generate code for controllers, mailers, models, scaffolds and other sets of code.&lt;br /&gt;
*&amp;lt;tt&amp;gt;destroy&amp;lt;/tt&amp;gt;: Used to destroy code created by the &amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
*&amp;lt;tt&amp;gt;server&amp;lt;/tt&amp;gt;: Starts up the Rails application in a self-contained web server.&lt;br /&gt;
*&amp;lt;tt&amp;gt;plugin&amp;lt;/tt&amp;gt;: Helps with the installation and administration of plug-ins to the Rails framework.&lt;br /&gt;
*&amp;lt;tt&amp;gt;performance&amp;lt;/tt&amp;gt; directory: Contains scripts used by the programmer to help understand the performance characteristics of the application being built.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with all of these scripts and compiles them all into a single script named &amp;lt;tt&amp;gt;rails&amp;lt;/tt&amp;gt;. With this script, the programmer can access all of the functionality that was available with the separate scripts. To make things even easier, the necessity to call this script outright is even unnecessary. By using the operating systems installed &amp;lt;tt&amp;gt;'''rails'''&amp;lt;/tt&amp;gt; command (ex, &amp;lt;tt&amp;gt;/usr/bin/rails&amp;lt;/tt&amp;gt; on Linux systems) in the root of any Rails 3 application, it will know to call this script.&lt;br /&gt;
&lt;br /&gt;
==Gem Dependencies==&lt;br /&gt;
&lt;br /&gt;
The ability to extend the Rails framework using a Gem is a very powerful feature. In Rails 2, the &amp;lt;tt&amp;gt;config/environment.rb&amp;lt;/tt&amp;gt; file had to be edited in order to tell the Rails application to require specific Gems using the &amp;lt;tt&amp;gt;'''config.gem'''&amp;lt;/tt&amp;gt; method. To ensure that the Gems were actually installed on the system running the Rails application, the programmer had to execute the &amp;lt;tt&amp;gt;rake gem:install&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with the need to have that manual process of installing Gems by utilizing a file named &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; in the root of the application as well as the Bundler. The Bundler looks at the contents &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; and automatically installs any Gems that are missing from the system. This allows the programmer to focus on more important issues.&lt;br /&gt;
&lt;br /&gt;
==Routing DSL==&lt;br /&gt;
&lt;br /&gt;
The routing DSL for Rails went through a major reconditioning and has actually been renamed to Action Dispatch. This rewrite took the logic out of the Action Controller and is now a standalone piece of software resulting in a much cleaner implementation. As well, the definition of routes for each application are now named within your Application module instead of the Action Controller. The difference can be seen in the beginning line of the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
  map.resources :posts&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
AppName::Application.routes do&lt;br /&gt;
  resources :posts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With all these changes, the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file now has a new syntax. Here are some of the major differences&amp;lt;ref&amp;gt;Reza 2010&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Default Route===&lt;br /&gt;
&lt;br /&gt;
The differences between the default route are minor between the two versions, but the Rails 3 version is much more explicit, as the parentheses indicate parameters that are optional. Also worth noting is that this route is commented out in Rails 3 by default. The reasoning behind this is that there is a big push for a RESTful architecture with the Rails application and this type of route isn't really recommended for that kind of application. It can be uncommented but be warned that doing so will make all actions in every controller accessible via GET requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect ':controller/:action/:id'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match '/:controller(/:action(/:id))'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Regular Routes===&lt;br /&gt;
The way in which a regular route is defined is much more streamlined and readable. In Rails 2, you had to define a key for the controller and action. With Rails 3, this definition is streamlined with a &amp;lt;tt&amp;gt;:to&amp;lt;/tt&amp;gt; key where you define in a specific format the controller and action responsible for the request. Here is an example to illustrate this point:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect 'products/:id', :controller =&amp;gt; 'products', :action =&amp;gt; 'view'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'products/:id', :to =&amp;gt; 'catalog#view'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Routes===&lt;br /&gt;
Setting up named routes has changed a little bit as well. The rules for the regular routes apply here but the way that you define the name of the route has changed. Now, you utilize the &amp;lt;tt&amp;gt;:as&amp;lt;/tt&amp;gt; key to define the name of the route as shown here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.logout '/logout', :controller =&amp;gt; 'sessions', :action =&amp;gt; 'destroy'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'logout', :to =&amp;gt; 'sessions#destroy', :as =&amp;gt; &amp;quot;logout&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Empty Route===&lt;br /&gt;
The empty route defines where to route the application when a user navigates to the root of the website (for normal HTML sites, this is the same as the index page). Rails 2 had a quick way of defining it but in true Ruby fashion, Rails 3 makes it even simpler to define this route as you can see here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.root :controller =&amp;gt; &amp;quot;welcome&amp;quot;, :action =&amp;gt; 'show'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
root :to =&amp;gt; 'welcome#show'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RESTful Resources===&lt;br /&gt;
RESTful resources are very important and have been a part of Rails since version 1.2. Utilizing the &amp;lt;tt&amp;gt;rails generate scaffold&amp;lt;/tt&amp;gt; command, Rails will generate the route, controller, views and model necessary to have the basic REST functionality for a model. As you can see from the following example, defining a RESTful route is simple in either version. Rails 3 grants you the ability to use this line for multiple resources as well, saving lines of code and making it more readable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.resources :products&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
# Single resource definition&lt;br /&gt;
resources :products&lt;br /&gt;
# Multiple resource definitions&lt;br /&gt;
resources :products, :categories, :comments&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, Rails 3 provides you with the ability to extend the routing beyond the seven basic RESTful actions.  There are multiple ways in which you can extend this functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Defining extra collection RESTful actions within a block.&lt;br /&gt;
resources :products do&lt;br /&gt;
  collection do&lt;br /&gt;
    get  :sold&lt;br /&gt;
    post :on_offer&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# An inline extension.&lt;br /&gt;
resources :products do&lt;br /&gt;
  get :sold, :on =&amp;gt; :member&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Active Record==&lt;br /&gt;
Active Record, like all of the core Rails components, got a major overhaul. &lt;br /&gt;
&lt;br /&gt;
===Arel and the Query Interface===&lt;br /&gt;
The most prominent changes made that will be apparent to developers will be the integration with [https://github.com/nkallen/arel Arel]. With this integration, all of the core methods for Active Record return relations, allowing them to be chained together to develop complex queries. A full list and their functionality can be found in the [http://guides.rubyonrails.org/active_record_querying.html Rails Guide]. This will take the place of the options hash that would normally get passed in to one of the Active Record methods. To illustrate this, say we wanted to get a list of the first 10 products ordered alphabetically:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:order =&amp;gt; &amp;quot;name DESC&amp;quot;, :limit =&amp;gt; 10)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.order(&amp;quot;name DESC&amp;quot;).limit(10)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, there are a lot of methods that are losing the options parameter in their definitions&amp;lt;ref&amp;gt;Naik 2010&amp;lt;/ref&amp;gt;, including query methods find and all as well as calculation methods count and average. Also, the &amp;lt;tt&amp;gt;:all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;:first&amp;lt;/tt&amp;gt; parameters for the find method are being deprecated in favor of the &amp;lt;tt&amp;gt;all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;first&amp;lt;/tt&amp;gt; methods. Here are some examples of how things are changing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Changes to the find method.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:all)&lt;br /&gt;
Product.find(:first)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.all&lt;br /&gt;
Product.first&lt;br /&gt;
&lt;br /&gt;
# Examples of method options being removed.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.all(:joins =&amp;gt; :categories)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.joins(&amp;quot;categories&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Scopes===&lt;br /&gt;
The &amp;lt;tt&amp;gt;named_scope&amp;lt;/tt&amp;gt; definition has been changed to just &amp;lt;tt&amp;gt;scope&amp;lt;/tt&amp;gt;. The options hash is also being deprecated and should be replaced with the new methods provided by the Arel integration. Translating existing Rails 2 named scopes is easy enough:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
class Product&lt;br /&gt;
  named_scope :food, :conditions =&amp;gt; { :type =&amp;gt; &amp;quot;food&amp;quot; }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
class Product&lt;br /&gt;
  scope :food, where(:type =&amp;gt; &amp;quot;food&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==JavaScript Support&amp;lt;ref&amp;gt;Gadbois 2010&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
There are many JavaScript libraries available to programmers to make their web applications more dynamic and smooth looking. Whenever you created a Rails 2 application, it would automatically install the Prototype and Script.aculo.us frameworks. Rails also provided helpers for these two JavaScript frameworks so that a few method calls were all that you needed to make your application more fluid.&lt;br /&gt;
&lt;br /&gt;
Rails 3 introduces the idea of Unobtrusive JavaScript (UJS). The JavaScript becomes unobtrusive by not being written in with the HTML objects producing cleaner code that is easier to debug. This is mostly done with the utilization of HTML 5 custom attributes such as data-method, data-confirm, data-remote and data-disable-with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;http://host.com&amp;quot; id=&amp;quot;create-post&amp;quot; method=&amp;quot;post&amp;quot; data-remote=&amp;quot;true&amp;quot; data-confirm=&amp;quot;Are you sure you want to submit?&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By doing this, it also frees Rails from being dependent on the Prototype framework, allowing it to become ''JavaScript framework agnostic''. With its current implementation, the framework can support Prototype, jQuery and MooTools.&lt;br /&gt;
&lt;br /&gt;
It might also be helpful to point out that as of Rails 3.1, the jQuery framework has replaced Prototype as the default JavaScript framework. Programmers that wish to still use Prototype must remember to specify this when they create the Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new myapp -j prototype&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 1.0: Party like it's one oh oh!&amp;quot;. Riding Rails. Retrieved September 2011.&amp;quot; http://weblog.rubyonrails.org/2005/12/13/rails-1-0-party-like-its-one-oh-oh/&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.0: It's done!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2007/12/7/rails-2-0-it-s-done&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.3: Templates, Engines, Rack, Metal, much more!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2009/3/16/rails-2-3-templates-engines-rack-metal-much-more&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails Ruby on Rails 2.3 Release Notes&amp;quot; Riding Rails. Retrieved September 2011. http://guides.rubyonrails.org/2_3_release_notes.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Gadbois, John. ''Using Unobtrusive JavaScript and AJAX with Rails 3'' http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Lindsaar, Mikel. &amp;quot;Ruby on Rails 3.0 Release Notes&amp;quot; http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
&lt;br /&gt;
Naik, Pratik. &amp;quot;Active Record Query Interface 3.0&amp;quot; http://m.onkey.org/active-record-query-interface , 2010.&lt;br /&gt;
&lt;br /&gt;
Reza, Rizwan. &amp;quot;The Lowdown on Routes in Rails 3&amp;quot; http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. ''Agile Web Development with Rails: Third Edition'': The Pragmatic Programmers LLC, 2009.&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50340</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50340"/>
		<updated>2011-09-22T19:44:53Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
The original Ruby on Rails platform was extracted from the web-based project-management tool called [http://en.wikipedia.org/wiki/Basecamp_(software) Basecamp] developed by 37signals. Ruby on Rails’ creator, David Heinemeier Hansson, began work on the Rails in early 2003 and released it as open source code in 2004, but it wasn’t until 2005 that Hansson shared commit rights to the project. Hansson designed Ruby on Rails to be an out of the box development framework that includes everything a programmer needs to create database-driven web applications according to the Model-View-Control pattern of separation. Ruby on Rails is based on the two key paradigms of [http://en.wikipedia.org/wiki/Convention_over_Configuration Configuration over Convention] and [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself].&lt;br /&gt;
&lt;br /&gt;
=History of Rails Development=&lt;br /&gt;
Throughout the development of the Rails framework there have been four significant releases alongside of the usual patches, fixes, and upgrades. &lt;br /&gt;
&lt;br /&gt;
===Rails 1.0===&lt;br /&gt;
Initially, Rails 1.0 was released 15 months after the original source was made available &amp;lt;ref&amp;gt;Rails 1.0 &amp;lt;/ref&amp;gt;. This version contained the structural foundation of what would become a powerful open source motivator for the Ruby platform.  Though there were patches and code adjustments in Rails 1.2 the next major release was Rails 2.0.&lt;br /&gt;
&lt;br /&gt;
===Rails 2.0 ===&lt;br /&gt;
Rails 2.0 was a major facelift for the framework and much of the effort went into improving the resources and polishing the overall package by making it more lean. Among some of these improvements was a focus on increasing security by adding a new module to work with HTTP Basic Authentication and providing a built-in mechanism for dealing with CRSF attacks. Other improvements were in the areas of changing certain syntactical nomenclature as well as simplifying the format for a few templates. &lt;br /&gt;
&lt;br /&gt;
===Rails 2.3===&lt;br /&gt;
The next major update was Rails 2.3 at it was the most structural update prior to Rails 3.0 being released. The two primary changes in the architecture of the Rails application was the complete integration of the Rack modular web server interface and a renewed support for Rails Engines. {link 2} Alongside of these changes were updates to templates, the addition of built-in support for HTTP Digest Authentication, nested forms models, Metal, and improved caching performance among other things.&lt;br /&gt;
&lt;br /&gt;
===Rails 3.0===&lt;br /&gt;
Between Rails 2.3 and the release of Rails 3.0, Rails announced that it would merge with Merb and that Rails 3.0 would be the successor to both Rails 2 and Merb. Merb which is short for Mongrel+Erb, key feature that prompted the join Rails was its component modularity, extensible API design, and vertical scalability.&lt;br /&gt;
&lt;br /&gt;
=Rails 2 vs. Rails 3=&lt;br /&gt;
&lt;br /&gt;
As you can see a lot changed since the first release of Rails. We will explore a few of the key areas that changed between Rails 2 and Rails 3 that will affect a developer's everyday coding.&lt;br /&gt;
&lt;br /&gt;
==Rails Application Scripts==&lt;br /&gt;
&lt;br /&gt;
When you create a new Rails application, a directory named &amp;lt;tt&amp;gt;script&amp;lt;/tt&amp;gt; is generated. In Rails 2, this directory is populated with a set of scripts that can be executed by the programmer. A few of the more important ones are as follows&amp;lt;ref&amp;gt;Ruby 2009, pp 257-258 &amp;lt;/ref&amp;gt;:&lt;br /&gt;
*&amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt;: Used to generate code for controllers, mailers, models, scaffolds and other sets of code.&lt;br /&gt;
*&amp;lt;tt&amp;gt;destroy&amp;lt;/tt&amp;gt;: Used to destroy code created by the &amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
*&amp;lt;tt&amp;gt;server&amp;lt;/tt&amp;gt;: Starts up the Rails application in a self-contained web server.&lt;br /&gt;
*&amp;lt;tt&amp;gt;plugin&amp;lt;/tt&amp;gt;: Helps with the installation and administration of plug-ins to the Rails framework.&lt;br /&gt;
*&amp;lt;tt&amp;gt;performance&amp;lt;/tt&amp;gt; directory: Contains scripts used by the programmer to help understand the performance characteristics of the application being built.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with all of these scripts and compiles them all into a single script named &amp;lt;tt&amp;gt;rails&amp;lt;/tt&amp;gt;. With this script, the programmer can access all of the functionality that was available with the separate scripts. To make things even easier, the necessity to call this script outright is even unnecessary. By using the operating systems installed &amp;lt;tt&amp;gt;'''rails'''&amp;lt;/tt&amp;gt; command (ex, &amp;lt;tt&amp;gt;/usr/bin/rails&amp;lt;/tt&amp;gt; on Linux systems) in the root of any Rails 3 application, it will know to call this script.&lt;br /&gt;
&lt;br /&gt;
==Gem Dependencies==&lt;br /&gt;
&lt;br /&gt;
The ability to extend the Rails framework using a Gem is a very powerful feature. In Rails 2, the &amp;lt;tt&amp;gt;config/environment.rb&amp;lt;/tt&amp;gt; file had to be edited in order to tell the Rails application to require specific Gems using the &amp;lt;tt&amp;gt;'''config.gem'''&amp;lt;/tt&amp;gt; method. To ensure that the Gems were actually installed on the system running the Rails application, the programmer had to execute the &amp;lt;tt&amp;gt;rake gem:install&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with the need to have that manual process of installing Gems by utilizing a file named &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; in the root of the application as well as the Bundler. The Bundler looks at the contents &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; and automatically installs any Gems that are missing from the system. This allows the programmer to focus on more important issues.&lt;br /&gt;
&lt;br /&gt;
==Routing DSL==&lt;br /&gt;
&lt;br /&gt;
The routing DSL for Rails went through a major reconditioning and has actually been renamed to Action Dispatch. This rewrite took the logic out of the Action Controller and is now a standalone piece of software resulting in a much cleaner implementation. As well, the definition of routes for each application are now named within your Application module instead of the Action Controller. The difference can be seen in the beginning line of the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
  map.resources :posts&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
AppName::Application.routes do&lt;br /&gt;
  resources :posts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With all these changes, the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file now has a new syntax. Here are some of the major differences&amp;lt;ref&amp;gt;Reza 2010&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Default Route===&lt;br /&gt;
&lt;br /&gt;
The differences between the default route are minor between the two versions, but the Rails 3 version is much more explicit, as the parentheses indicate parameters that are optional. Also worth noting is that this route is commented out in Rails 3 by default. The reasoning behind this is that there is a big push for a RESTful architecture with the Rails application and this type of route isn't really recommended for that kind of application. It can be uncommented but be warned that doing so will make all actions in every controller accessible via GET requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect ':controller/:action/:id'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match '/:controller(/:action(/:id))'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Regular Routes===&lt;br /&gt;
The way in which a regular route is defined is much more streamlined and readable. In Rails 2, you had to define a key for the controller and action. With Rails 3, this definition is streamlined with a &amp;lt;tt&amp;gt;:to&amp;lt;/tt&amp;gt; key where you define in a specific format the controller and action responsible for the request. Here is an example to illustrate this point:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect 'products/:id', :controller =&amp;gt; 'products', :action =&amp;gt; 'view'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'products/:id', :to =&amp;gt; 'catalog#view'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Routes===&lt;br /&gt;
Setting up named routes has changed a little bit as well. The rules for the regular routes apply here but the way that you define the name of the route has changed. Now, you utilize the &amp;lt;tt&amp;gt;:as&amp;lt;/tt&amp;gt; key to define the name of the route as shown here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.logout '/logout', :controller =&amp;gt; 'sessions', :action =&amp;gt; 'destroy'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'logout', :to =&amp;gt; 'sessions#destroy', :as =&amp;gt; &amp;quot;logout&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Empty Route===&lt;br /&gt;
The empty route defines where to route the application when a user navigates to the root of the website (for normal HTML sites, this is the same as the index page). Rails 2 had a quick way of defining it but in true Ruby fashion, Rails 3 makes it even simpler to define this route as you can see here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.root :controller =&amp;gt; &amp;quot;welcome&amp;quot;, :action =&amp;gt; 'show'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
root :to =&amp;gt; 'welcome#show'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RESTful Resources===&lt;br /&gt;
RESTful resources are very important and have been a part of Rails since version 1.2. Utilizing the &amp;lt;tt&amp;gt;rails generate scaffold&amp;lt;/tt&amp;gt; command, Rails will generate the route, controller, views and model necessary to have the basic REST functionality for a model. As you can see from the following example, defining a RESTful route is simple in either version. Rails 3 grants you the ability to use this line for multiple resources as well, saving lines of code and making it more readable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.resources :products&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
# Single resource definition&lt;br /&gt;
resources :products&lt;br /&gt;
# Multiple resource definitions&lt;br /&gt;
resources :products, :categories, :comments&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, Rails 3 provides you with the ability to extend the routing beyond the seven basic RESTful actions.  There are multiple ways in which you can extend this functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Defining extra collection RESTful actions within a block.&lt;br /&gt;
resources :products do&lt;br /&gt;
  collection do&lt;br /&gt;
    get  :sold&lt;br /&gt;
    post :on_offer&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# An inline extension.&lt;br /&gt;
resources :products do&lt;br /&gt;
  get :sold, :on =&amp;gt; :member&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Active Record==&lt;br /&gt;
Active Record, like all of the core Rails components, got a major overhaul. &lt;br /&gt;
&lt;br /&gt;
===Arel and the Query Interface===&lt;br /&gt;
The most prominent changes made that will be apparent to developers will be the integration with [https://github.com/nkallen/arel Arel]. With this integration, all of the core methods for Active Record return relations, allowing them to be chained together to develop complex queries. A full list and their functionality can be found in the [http://guides.rubyonrails.org/active_record_querying.html Rails Guide]. This will take the place of the options hash that would normally get passed in to one of the Active Record methods. To illustrate this, say we wanted to get a list of the first 10 products ordered alphabetically:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:order =&amp;gt; &amp;quot;name DESC&amp;quot;, :limit =&amp;gt; 10)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.order(&amp;quot;name DESC&amp;quot;).limit(10)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, there are a lot of methods that are losing the options parameter in their definitions&amp;lt;ref&amp;gt;Naik 2010&amp;lt;/ref&amp;gt;, including query methods find and all as well as calculation methods count and average. Also, the &amp;lt;tt&amp;gt;:all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;:first&amp;lt;/tt&amp;gt; parameters for the find method are being deprecated in favor of the &amp;lt;tt&amp;gt;all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;first&amp;lt;/tt&amp;gt; methods. Here are some examples of how things are changing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Changes to the find method.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:all)&lt;br /&gt;
Product.find(:first)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.all&lt;br /&gt;
Product.first&lt;br /&gt;
&lt;br /&gt;
# Examples of method options being removed.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.all(:joins =&amp;gt; :categories)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.joins(&amp;quot;categories&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Scopes===&lt;br /&gt;
The &amp;lt;tt&amp;gt;named_scope&amp;lt;/tt&amp;gt; definition has been changed to just &amp;lt;tt&amp;gt;scope&amp;lt;/tt&amp;gt;. The options hash is also being deprecated and should be replaced with the new methods provided by the Arel integration. Translating existing Rails 2 named scopes is easy enough:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
class Product&lt;br /&gt;
  named_scope :food, :conditions =&amp;gt; { :type =&amp;gt; &amp;quot;food&amp;quot; }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
class Product&lt;br /&gt;
  scope :food, where(:type =&amp;gt; &amp;quot;food&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==JavaScript Support&amp;lt;ref&amp;gt;Gadbois 2010&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
There are many JavaScript libraries available to programmers to make their web applications more dynamic and smooth looking. Whenever you created a Rails 2 application, it would automatically install the Prototype and Script.aculo.us frameworks. Rails also provided helpers for these two JavaScript frameworks so that a few method calls were all that you needed to make your application more fluid.&lt;br /&gt;
&lt;br /&gt;
Rails 3 introduces the idea of Unobtrusive JavaScript (UJS). The JavaScript becomes unobtrusive by not being written in with the HTML objects producing cleaner code that is easier to debug. This is mostly done with the utilization of HTML 5 custom attributes such as data-method, data-confirm, data-remote and data-disable-with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;http://host.com&amp;quot; id=&amp;quot;create-post&amp;quot; method=&amp;quot;post&amp;quot; data-remote=&amp;quot;true&amp;quot; data-confirm=&amp;quot;Are you sure you want to submit?&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By doing this, it also frees Rails from being dependent on the Prototype framework, allowing it to become ''JavaScript framework agnostic''. With its current implementation, the framework can support Prototype, jQuery and MooTools.&lt;br /&gt;
&lt;br /&gt;
It might also be helpful to point out that as of Rails 3.1, the jQuery framework has replaced Prototype as the default JavaScript framework. Programmers that wish to still use Prototype must remember to specify this when they create the Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new myapp -j prototype&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 1.0: Party like it's one oh oh!&amp;quot;. Riding Rails. Retrieved September 2011.&amp;quot; http://weblog.rubyonrails.org/2005/12/13/rails-1-0-party-like-its-one-oh-oh/&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.0: It's done!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2007/12/7/rails-2-0-it-s-done&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.3: Templates, Engines, Rack, Metal, much more!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2009/3/16/rails-2-3-templates-engines-rack-metal-much-more&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails Ruby on Rails 2.3 Release Notes&amp;quot; Riding Rails. Retrieved September 2011. http://guides.rubyonrails.org/2_3_release_notes.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Gadbois, John. ''Using Unobtrusive JavaScript and AJAX with Rails 3'' http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Lindsaar, Mikel. &amp;quot;Ruby on Rails 3.0 Release Notes&amp;quot; http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
&lt;br /&gt;
Naik, Pratik. &amp;quot;Active Record Query Interface 3.0&amp;quot; http://m.onkey.org/active-record-query-interface , 2010.&lt;br /&gt;
&lt;br /&gt;
Reza, Rizwan. &amp;quot;The Lowdown on Routes in Rails 3&amp;quot; http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. ''Agile Web Development with Rails: Third Edition'': The Pragmatic Programmers LLC, 2009.&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50339</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50339"/>
		<updated>2011-09-22T19:42:39Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
The original Ruby on Rails platform was extracted from the web-based project-management tool called Basecamp developed by 37signals. Ruby on Rails’ creator, David Heinemeier Hansson, began work on the Rails in early 2003 and released it as open source code in 2004, but it wasn’t until 2005 that Hansson shared commit rights to the project. Hansson designed Ruby on Rails to be an out of the box development framework that includes everything a programmer needs to create database-driven web applications according to the Model-View-Control pattern of separation. Ruby on Rails is based on the two key paradigms of [http://en.wikipedia.org/wiki/Convention_over_Configuration Configuration over Convention] and [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself].&lt;br /&gt;
&lt;br /&gt;
=History of Rails Development=&lt;br /&gt;
Throughout the development of the Rails framework there have been four significant releases alongside of the usual patches, fixes, and upgrades. &lt;br /&gt;
&lt;br /&gt;
===Rails 1.0===&lt;br /&gt;
Initially, Rails 1.0 was released 15 months after the original source was made available &amp;lt;ref&amp;gt;Rails 1.0 &amp;lt;/ref&amp;gt;. This version contained the structural foundation of what would become a powerful open source motivator for the Ruby platform.  Though there were patches and code adjustments in Rails 1.2 the next major release was Rails 2.0.&lt;br /&gt;
&lt;br /&gt;
===Rails 2.0 ===&lt;br /&gt;
Rails 2.0 was a major facelift for the framework and much of the effort went into improving the resources and polishing the overall package by making it more lean. Among some of these improvements was a focus on increasing security by adding a new module to work with HTTP Basic Authentication and providing a built-in mechanism for dealing with CRSF attacks. Other improvements were in the areas of changing certain syntactical nomenclature as well as simplifying the format for a few templates. &lt;br /&gt;
&lt;br /&gt;
===Rails 2.3===&lt;br /&gt;
The next major update was Rails 2.3 at it was the most structural update prior to Rails 3.0 being released. The two primary changes in the architecture of the Rails application was the complete integration of the Rack modular web server interface and a renewed support for Rails Engines. {link 2} Alongside of these changes were updates to templates, the addition of built-in support for HTTP Digest Authentication, nested forms models, Metal, and improved caching performance among other things.&lt;br /&gt;
&lt;br /&gt;
===Rails 3.0===&lt;br /&gt;
Between Rails 2.3 and the release of Rails 3.0, Rails announced that it would merge with Merb and that Rails 3.0 would be the successor to both Rails 2 and Merb. Merb which is short for Mongrel+Erb, key feature that prompted the join Rails was its component modularity, extensible API design, and vertical scalability.&lt;br /&gt;
&lt;br /&gt;
=Rails 2 vs. Rails 3=&lt;br /&gt;
&lt;br /&gt;
As you can see a lot changed since the first release of Rails. We will explore a few of the key areas that changed between Rails 2 and Rails 3 that will affect a developer's everyday coding.&lt;br /&gt;
&lt;br /&gt;
==Rails Application Scripts==&lt;br /&gt;
&lt;br /&gt;
When you create a new Rails application, a directory named &amp;lt;tt&amp;gt;script&amp;lt;/tt&amp;gt; is generated. In Rails 2, this directory is populated with a set of scripts that can be executed by the programmer. A few of the more important ones are as follows&amp;lt;ref&amp;gt;Ruby 2009, pp 257-258 &amp;lt;/ref&amp;gt;:&lt;br /&gt;
*&amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt;: Used to generate code for controllers, mailers, models, scaffolds and other sets of code.&lt;br /&gt;
*&amp;lt;tt&amp;gt;destroy&amp;lt;/tt&amp;gt;: Used to destroy code created by the &amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
*&amp;lt;tt&amp;gt;server&amp;lt;/tt&amp;gt;: Starts up the Rails application in a self-contained web server.&lt;br /&gt;
*&amp;lt;tt&amp;gt;plugin&amp;lt;/tt&amp;gt;: Helps with the installation and administration of plug-ins to the Rails framework.&lt;br /&gt;
*&amp;lt;tt&amp;gt;performance&amp;lt;/tt&amp;gt; directory: Contains scripts used by the programmer to help understand the performance characteristics of the application being built.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with all of these scripts and compiles them all into a single script named &amp;lt;tt&amp;gt;rails&amp;lt;/tt&amp;gt;. With this script, the programmer can access all of the functionality that was available with the separate scripts. To make things even easier, the necessity to call this script outright is even unnecessary. By using the operating systems installed &amp;lt;tt&amp;gt;'''rails'''&amp;lt;/tt&amp;gt; command (ex, &amp;lt;tt&amp;gt;/usr/bin/rails&amp;lt;/tt&amp;gt; on Linux systems) in the root of any Rails 3 application, it will know to call this script.&lt;br /&gt;
&lt;br /&gt;
==Gem Dependencies==&lt;br /&gt;
&lt;br /&gt;
The ability to extend the Rails framework using a Gem is a very powerful feature. In Rails 2, the &amp;lt;tt&amp;gt;config/environment.rb&amp;lt;/tt&amp;gt; file had to be edited in order to tell the Rails application to require specific Gems using the &amp;lt;tt&amp;gt;'''config.gem'''&amp;lt;/tt&amp;gt; method. To ensure that the Gems were actually installed on the system running the Rails application, the programmer had to execute the &amp;lt;tt&amp;gt;rake gem:install&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with the need to have that manual process of installing Gems by utilizing a file named &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; in the root of the application as well as the Bundler. The Bundler looks at the contents &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; and automatically installs any Gems that are missing from the system. This allows the programmer to focus on more important issues.&lt;br /&gt;
&lt;br /&gt;
==Routing DSL==&lt;br /&gt;
&lt;br /&gt;
The routing DSL for Rails went through a major reconditioning and has actually been renamed to Action Dispatch. This rewrite took the logic out of the Action Controller and is now a standalone piece of software resulting in a much cleaner implementation. As well, the definition of routes for each application are now named within your Application module instead of the Action Controller. The difference can be seen in the beginning line of the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
  map.resources :posts&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
AppName::Application.routes do&lt;br /&gt;
  resources :posts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With all these changes, the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file now has a new syntax. Here are some of the major differences&amp;lt;ref&amp;gt;Reza 2010&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Default Route===&lt;br /&gt;
&lt;br /&gt;
The differences between the default route are minor between the two versions, but the Rails 3 version is much more explicit, as the parentheses indicate parameters that are optional. Also worth noting is that this route is commented out in Rails 3 by default. The reasoning behind this is that there is a big push for a RESTful architecture with the Rails application and this type of route isn't really recommended for that kind of application. It can be uncommented but be warned that doing so will make all actions in every controller accessible via GET requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect ':controller/:action/:id'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match '/:controller(/:action(/:id))'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Regular Routes===&lt;br /&gt;
The way in which a regular route is defined is much more streamlined and readable. In Rails 2, you had to define a key for the controller and action. With Rails 3, this definition is streamlined with a &amp;lt;tt&amp;gt;:to&amp;lt;/tt&amp;gt; key where you define in a specific format the controller and action responsible for the request. Here is an example to illustrate this point:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect 'products/:id', :controller =&amp;gt; 'products', :action =&amp;gt; 'view'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'products/:id', :to =&amp;gt; 'catalog#view'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Routes===&lt;br /&gt;
Setting up named routes has changed a little bit as well. The rules for the regular routes apply here but the way that you define the name of the route has changed. Now, you utilize the &amp;lt;tt&amp;gt;:as&amp;lt;/tt&amp;gt; key to define the name of the route as shown here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.logout '/logout', :controller =&amp;gt; 'sessions', :action =&amp;gt; 'destroy'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'logout', :to =&amp;gt; 'sessions#destroy', :as =&amp;gt; &amp;quot;logout&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Empty Route===&lt;br /&gt;
The empty route defines where to route the application when a user navigates to the root of the website (for normal HTML sites, this is the same as the index page). Rails 2 had a quick way of defining it but in true Ruby fashion, Rails 3 makes it even simpler to define this route as you can see here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.root :controller =&amp;gt; &amp;quot;welcome&amp;quot;, :action =&amp;gt; 'show'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
root :to =&amp;gt; 'welcome#show'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RESTful Resources===&lt;br /&gt;
RESTful resources are very important and have been a part of Rails since version 1.2. Utilizing the &amp;lt;tt&amp;gt;rails generate scaffold&amp;lt;/tt&amp;gt; command, Rails will generate the route, controller, views and model necessary to have the basic REST functionality for a model. As you can see from the following example, defining a RESTful route is simple in either version. Rails 3 grants you the ability to use this line for multiple resources as well, saving lines of code and making it more readable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.resources :products&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
# Single resource definition&lt;br /&gt;
resources :products&lt;br /&gt;
# Multiple resource definitions&lt;br /&gt;
resources :products, :categories, :comments&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, Rails 3 provides you with the ability to extend the routing beyond the seven basic RESTful actions.  There are multiple ways in which you can extend this functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Defining extra collection RESTful actions within a block.&lt;br /&gt;
resources :products do&lt;br /&gt;
  collection do&lt;br /&gt;
    get  :sold&lt;br /&gt;
    post :on_offer&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# An inline extension.&lt;br /&gt;
resources :products do&lt;br /&gt;
  get :sold, :on =&amp;gt; :member&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Active Record==&lt;br /&gt;
Active Record, like all of the core Rails components, got a major overhaul. &lt;br /&gt;
&lt;br /&gt;
===Arel and the Query Interface===&lt;br /&gt;
The most prominent changes made that will be apparent to developers will be the integration with [https://github.com/nkallen/arel Arel]. With this integration, all of the core methods for Active Record return relations, allowing them to be chained together to develop complex queries. A full list and their functionality can be found in the [http://guides.rubyonrails.org/active_record_querying.html Rails Guide]. This will take the place of the options hash that would normally get passed in to one of the Active Record methods. To illustrate this, say we wanted to get a list of the first 10 products ordered alphabetically:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:order =&amp;gt; &amp;quot;name DESC&amp;quot;, :limit =&amp;gt; 10)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.order(&amp;quot;name DESC&amp;quot;).limit(10)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, there are a lot of methods that are losing the options parameter in their definitions&amp;lt;ref&amp;gt;Naik 2010&amp;lt;/ref&amp;gt;, including query methods find and all as well as calculation methods count and average. Also, the &amp;lt;tt&amp;gt;:all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;:first&amp;lt;/tt&amp;gt; parameters for the find method are being deprecated in favor of the &amp;lt;tt&amp;gt;all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;first&amp;lt;/tt&amp;gt; methods. Here are some examples of how things are changing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Changes to the find method.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:all)&lt;br /&gt;
Product.find(:first)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.all&lt;br /&gt;
Product.first&lt;br /&gt;
&lt;br /&gt;
# Examples of method options being removed.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.all(:joins =&amp;gt; :categories)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.joins(&amp;quot;categories&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Scopes===&lt;br /&gt;
The &amp;lt;tt&amp;gt;named_scope&amp;lt;/tt&amp;gt; definition has been changed to just &amp;lt;tt&amp;gt;scope&amp;lt;/tt&amp;gt;. The options hash is also being deprecated and should be replaced with the new methods provided by the Arel integration. Translating existing Rails 2 named scopes is easy enough:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
class Product&lt;br /&gt;
  named_scope :food, :conditions =&amp;gt; { :type =&amp;gt; &amp;quot;food&amp;quot; }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
class Product&lt;br /&gt;
  scope :food, where(:type =&amp;gt; &amp;quot;food&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==JavaScript Support&amp;lt;ref&amp;gt;Gadbois 2010&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
There are many JavaScript libraries available to programmers to make their web applications more dynamic and smooth looking. Whenever you created a Rails 2 application, it would automatically install the Prototype and Script.aculo.us frameworks. Rails also provided helpers for these two JavaScript frameworks so that a few method calls were all that you needed to make your application more fluid.&lt;br /&gt;
&lt;br /&gt;
Rails 3 introduces the idea of Unobtrusive JavaScript (UJS). The JavaScript becomes unobtrusive by not being written in with the HTML objects producing cleaner code that is easier to debug. This is mostly done with the utilization of HTML 5 custom attributes such as data-method, data-confirm, data-remote and data-disable-with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;http://host.com&amp;quot; id=&amp;quot;create-post&amp;quot; method=&amp;quot;post&amp;quot; data-remote=&amp;quot;true&amp;quot; data-confirm=&amp;quot;Are you sure you want to submit?&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By doing this, it also frees Rails from being dependent on the Prototype framework, allowing it to become ''JavaScript framework agnostic''. With its current implementation, the framework can support Prototype, jQuery and MooTools.&lt;br /&gt;
&lt;br /&gt;
It might also be helpful to point out that as of Rails 3.1, the jQuery framework has replaced Prototype as the default JavaScript framework. Programmers that wish to still use Prototype must remember to specify this when they create the Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new myapp -j prototype&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 1.0: Party like it's one oh oh!&amp;quot;. Riding Rails. Retrieved September 2011.&amp;quot; http://weblog.rubyonrails.org/2005/12/13/rails-1-0-party-like-its-one-oh-oh/&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.0: It's done!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2007/12/7/rails-2-0-it-s-done&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.3: Templates, Engines, Rack, Metal, much more!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2009/3/16/rails-2-3-templates-engines-rack-metal-much-more&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails Ruby on Rails 2.3 Release Notes&amp;quot; Riding Rails. Retrieved September 2011. http://guides.rubyonrails.org/2_3_release_notes.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Gadbois, John. ''Using Unobtrusive JavaScript and AJAX with Rails 3'' http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Lindsaar, Mikel. &amp;quot;Ruby on Rails 3.0 Release Notes&amp;quot; http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
&lt;br /&gt;
Naik, Pratik. &amp;quot;Active Record Query Interface 3.0&amp;quot; http://m.onkey.org/active-record-query-interface , 2010.&lt;br /&gt;
&lt;br /&gt;
Reza, Rizwan. &amp;quot;The Lowdown on Routes in Rails 3&amp;quot; http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. ''Agile Web Development with Rails: Third Edition'': The Pragmatic Programmers LLC, 2009.&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50338</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50338"/>
		<updated>2011-09-22T19:41:51Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
The original Ruby on Rails platform was extracted from the web-based project-management tool called Basecamp developed by 37signals. Ruby on Rails’ creator, David Heinemeier Hansson, began work on the Rails in early 2003 and released it as open source code in 2004, but it wasn’t until 2005 that Hansson shared commit rights to the project. [REFERENCE] Hansson designed Ruby on Rails to be an out of the box development framework that includes everything a programmer needs to create database-driven web applications according to the Model-View-Control pattern of separation. Ruby on Rails is based on the two key paradigms of [http://en.wikipedia.org/wiki/Convention_over_Configuration Configuration over Convention] and [http://en.wikipedia.org/wiki/Don't_Repeat_Yourself Don't Repeat Yourself].&lt;br /&gt;
&lt;br /&gt;
=History of Rails Development=&lt;br /&gt;
Throughout the development of the Rails framework there have been four significant releases alongside of the usual patches, fixes, and upgrades. &lt;br /&gt;
&lt;br /&gt;
===Rails 1.0===&lt;br /&gt;
Initially, Rails 1.0 was released 15 months after the original source was made available &amp;lt;ref&amp;gt;Rails 1.0 &amp;lt;/ref&amp;gt;. This version contained the structural foundation of what would become a powerful open source motivator for the Ruby platform.  Though there were patches and code adjustments in Rails 1.2 the next major release was Rails 2.0.&lt;br /&gt;
&lt;br /&gt;
===Rails 2.0 ===&lt;br /&gt;
Rails 2.0 was a major facelift for the framework and much of the effort went into improving the resources and polishing the overall package by making it more lean. Among some of these improvements was a focus on increasing security by adding a new module to work with HTTP Basic Authentication and providing a built-in mechanism for dealing with CRSF attacks. Other improvements were in the areas of changing certain syntactical nomenclature as well as simplifying the format for a few templates. &lt;br /&gt;
&lt;br /&gt;
===Rails 2.3===&lt;br /&gt;
The next major update was Rails 2.3 at it was the most structural update prior to Rails 3.0 being released. The two primary changes in the architecture of the Rails application was the complete integration of the Rack modular web server interface and a renewed support for Rails Engines. {link 2} Alongside of these changes were updates to templates, the addition of built-in support for HTTP Digest Authentication, nested forms models, Metal, and improved caching performance among other things.&lt;br /&gt;
&lt;br /&gt;
===Rails 3.0===&lt;br /&gt;
Between Rails 2.3 and the release of Rails 3.0, Rails announced that it would merge with Merb and that Rails 3.0 would be the successor to both Rails 2 and Merb. Merb which is short for Mongrel+Erb, key feature that prompted the join Rails was its component modularity, extensible API design, and vertical scalability.&lt;br /&gt;
&lt;br /&gt;
=Rails 2 vs. Rails 3=&lt;br /&gt;
&lt;br /&gt;
As you can see a lot changed since the first release of Rails. We will explore a few of the key areas that changed between Rails 2 and Rails 3 that will affect a developer's everyday coding.&lt;br /&gt;
&lt;br /&gt;
==Rails Application Scripts==&lt;br /&gt;
&lt;br /&gt;
When you create a new Rails application, a directory named &amp;lt;tt&amp;gt;script&amp;lt;/tt&amp;gt; is generated. In Rails 2, this directory is populated with a set of scripts that can be executed by the programmer. A few of the more important ones are as follows&amp;lt;ref&amp;gt;Ruby 2009, pp 257-258 &amp;lt;/ref&amp;gt;:&lt;br /&gt;
*&amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt;: Used to generate code for controllers, mailers, models, scaffolds and other sets of code.&lt;br /&gt;
*&amp;lt;tt&amp;gt;destroy&amp;lt;/tt&amp;gt;: Used to destroy code created by the &amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
*&amp;lt;tt&amp;gt;server&amp;lt;/tt&amp;gt;: Starts up the Rails application in a self-contained web server.&lt;br /&gt;
*&amp;lt;tt&amp;gt;plugin&amp;lt;/tt&amp;gt;: Helps with the installation and administration of plug-ins to the Rails framework.&lt;br /&gt;
*&amp;lt;tt&amp;gt;performance&amp;lt;/tt&amp;gt; directory: Contains scripts used by the programmer to help understand the performance characteristics of the application being built.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with all of these scripts and compiles them all into a single script named &amp;lt;tt&amp;gt;rails&amp;lt;/tt&amp;gt;. With this script, the programmer can access all of the functionality that was available with the separate scripts. To make things even easier, the necessity to call this script outright is even unnecessary. By using the operating systems installed &amp;lt;tt&amp;gt;'''rails'''&amp;lt;/tt&amp;gt; command (ex, &amp;lt;tt&amp;gt;/usr/bin/rails&amp;lt;/tt&amp;gt; on Linux systems) in the root of any Rails 3 application, it will know to call this script.&lt;br /&gt;
&lt;br /&gt;
==Gem Dependencies==&lt;br /&gt;
&lt;br /&gt;
The ability to extend the Rails framework using a Gem is a very powerful feature. In Rails 2, the &amp;lt;tt&amp;gt;config/environment.rb&amp;lt;/tt&amp;gt; file had to be edited in order to tell the Rails application to require specific Gems using the &amp;lt;tt&amp;gt;'''config.gem'''&amp;lt;/tt&amp;gt; method. To ensure that the Gems were actually installed on the system running the Rails application, the programmer had to execute the &amp;lt;tt&amp;gt;rake gem:install&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with the need to have that manual process of installing Gems by utilizing a file named &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; in the root of the application as well as the Bundler. The Bundler looks at the contents &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; and automatically installs any Gems that are missing from the system. This allows the programmer to focus on more important issues.&lt;br /&gt;
&lt;br /&gt;
==Routing DSL==&lt;br /&gt;
&lt;br /&gt;
The routing DSL for Rails went through a major reconditioning and has actually been renamed to Action Dispatch. This rewrite took the logic out of the Action Controller and is now a standalone piece of software resulting in a much cleaner implementation. As well, the definition of routes for each application are now named within your Application module instead of the Action Controller. The difference can be seen in the beginning line of the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
  map.resources :posts&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
AppName::Application.routes do&lt;br /&gt;
  resources :posts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With all these changes, the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file now has a new syntax. Here are some of the major differences&amp;lt;ref&amp;gt;Reza 2010&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Default Route===&lt;br /&gt;
&lt;br /&gt;
The differences between the default route are minor between the two versions, but the Rails 3 version is much more explicit, as the parentheses indicate parameters that are optional. Also worth noting is that this route is commented out in Rails 3 by default. The reasoning behind this is that there is a big push for a RESTful architecture with the Rails application and this type of route isn't really recommended for that kind of application. It can be uncommented but be warned that doing so will make all actions in every controller accessible via GET requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect ':controller/:action/:id'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match '/:controller(/:action(/:id))'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Regular Routes===&lt;br /&gt;
The way in which a regular route is defined is much more streamlined and readable. In Rails 2, you had to define a key for the controller and action. With Rails 3, this definition is streamlined with a &amp;lt;tt&amp;gt;:to&amp;lt;/tt&amp;gt; key where you define in a specific format the controller and action responsible for the request. Here is an example to illustrate this point:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect 'products/:id', :controller =&amp;gt; 'products', :action =&amp;gt; 'view'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'products/:id', :to =&amp;gt; 'catalog#view'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Routes===&lt;br /&gt;
Setting up named routes has changed a little bit as well. The rules for the regular routes apply here but the way that you define the name of the route has changed. Now, you utilize the &amp;lt;tt&amp;gt;:as&amp;lt;/tt&amp;gt; key to define the name of the route as shown here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.logout '/logout', :controller =&amp;gt; 'sessions', :action =&amp;gt; 'destroy'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'logout', :to =&amp;gt; 'sessions#destroy', :as =&amp;gt; &amp;quot;logout&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Empty Route===&lt;br /&gt;
The empty route defines where to route the application when a user navigates to the root of the website (for normal HTML sites, this is the same as the index page). Rails 2 had a quick way of defining it but in true Ruby fashion, Rails 3 makes it even simpler to define this route as you can see here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.root :controller =&amp;gt; &amp;quot;welcome&amp;quot;, :action =&amp;gt; 'show'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
root :to =&amp;gt; 'welcome#show'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RESTful Resources===&lt;br /&gt;
RESTful resources are very important and have been a part of Rails since version 1.2. Utilizing the &amp;lt;tt&amp;gt;rails generate scaffold&amp;lt;/tt&amp;gt; command, Rails will generate the route, controller, views and model necessary to have the basic REST functionality for a model. As you can see from the following example, defining a RESTful route is simple in either version. Rails 3 grants you the ability to use this line for multiple resources as well, saving lines of code and making it more readable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.resources :products&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
# Single resource definition&lt;br /&gt;
resources :products&lt;br /&gt;
# Multiple resource definitions&lt;br /&gt;
resources :products, :categories, :comments&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, Rails 3 provides you with the ability to extend the routing beyond the seven basic RESTful actions.  There are multiple ways in which you can extend this functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Defining extra collection RESTful actions within a block.&lt;br /&gt;
resources :products do&lt;br /&gt;
  collection do&lt;br /&gt;
    get  :sold&lt;br /&gt;
    post :on_offer&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# An inline extension.&lt;br /&gt;
resources :products do&lt;br /&gt;
  get :sold, :on =&amp;gt; :member&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Active Record==&lt;br /&gt;
Active Record, like all of the core Rails components, got a major overhaul. &lt;br /&gt;
&lt;br /&gt;
===Arel and the Query Interface===&lt;br /&gt;
The most prominent changes made that will be apparent to developers will be the integration with [https://github.com/nkallen/arel Arel]. With this integration, all of the core methods for Active Record return relations, allowing them to be chained together to develop complex queries. A full list and their functionality can be found in the [http://guides.rubyonrails.org/active_record_querying.html Rails Guide]. This will take the place of the options hash that would normally get passed in to one of the Active Record methods. To illustrate this, say we wanted to get a list of the first 10 products ordered alphabetically:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:order =&amp;gt; &amp;quot;name DESC&amp;quot;, :limit =&amp;gt; 10)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.order(&amp;quot;name DESC&amp;quot;).limit(10)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, there are a lot of methods that are losing the options parameter in their definitions&amp;lt;ref&amp;gt;Naik 2010&amp;lt;/ref&amp;gt;, including query methods find and all as well as calculation methods count and average. Also, the &amp;lt;tt&amp;gt;:all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;:first&amp;lt;/tt&amp;gt; parameters for the find method are being deprecated in favor of the &amp;lt;tt&amp;gt;all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;first&amp;lt;/tt&amp;gt; methods. Here are some examples of how things are changing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Changes to the find method.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:all)&lt;br /&gt;
Product.find(:first)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.all&lt;br /&gt;
Product.first&lt;br /&gt;
&lt;br /&gt;
# Examples of method options being removed.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.all(:joins =&amp;gt; :categories)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.joins(&amp;quot;categories&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Scopes===&lt;br /&gt;
The &amp;lt;tt&amp;gt;named_scope&amp;lt;/tt&amp;gt; definition has been changed to just &amp;lt;tt&amp;gt;scope&amp;lt;/tt&amp;gt;. The options hash is also being deprecated and should be replaced with the new methods provided by the Arel integration. Translating existing Rails 2 named scopes is easy enough:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
class Product&lt;br /&gt;
  named_scope :food, :conditions =&amp;gt; { :type =&amp;gt; &amp;quot;food&amp;quot; }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
class Product&lt;br /&gt;
  scope :food, where(:type =&amp;gt; &amp;quot;food&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==JavaScript Support&amp;lt;ref&amp;gt;Gadbois 2010&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
There are many JavaScript libraries available to programmers to make their web applications more dynamic and smooth looking. Whenever you created a Rails 2 application, it would automatically install the Prototype and Script.aculo.us frameworks. Rails also provided helpers for these two JavaScript frameworks so that a few method calls were all that you needed to make your application more fluid.&lt;br /&gt;
&lt;br /&gt;
Rails 3 introduces the idea of Unobtrusive JavaScript (UJS). The JavaScript becomes unobtrusive by not being written in with the HTML objects producing cleaner code that is easier to debug. This is mostly done with the utilization of HTML 5 custom attributes such as data-method, data-confirm, data-remote and data-disable-with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;http://host.com&amp;quot; id=&amp;quot;create-post&amp;quot; method=&amp;quot;post&amp;quot; data-remote=&amp;quot;true&amp;quot; data-confirm=&amp;quot;Are you sure you want to submit?&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By doing this, it also frees Rails from being dependent on the Prototype framework, allowing it to become ''JavaScript framework agnostic''. With its current implementation, the framework can support Prototype, jQuery and MooTools.&lt;br /&gt;
&lt;br /&gt;
It might also be helpful to point out that as of Rails 3.1, the jQuery framework has replaced Prototype as the default JavaScript framework. Programmers that wish to still use Prototype must remember to specify this when they create the Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new myapp -j prototype&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 1.0: Party like it's one oh oh!&amp;quot;. Riding Rails. Retrieved September 2011.&amp;quot; http://weblog.rubyonrails.org/2005/12/13/rails-1-0-party-like-its-one-oh-oh/&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.0: It's done!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2007/12/7/rails-2-0-it-s-done&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.3: Templates, Engines, Rack, Metal, much more!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2009/3/16/rails-2-3-templates-engines-rack-metal-much-more&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails Ruby on Rails 2.3 Release Notes&amp;quot; Riding Rails. Retrieved September 2011. http://guides.rubyonrails.org/2_3_release_notes.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Gadbois, John. ''Using Unobtrusive JavaScript and AJAX with Rails 3'' http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Lindsaar, Mikel. &amp;quot;Ruby on Rails 3.0 Release Notes&amp;quot; http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
&lt;br /&gt;
Naik, Pratik. &amp;quot;Active Record Query Interface 3.0&amp;quot; http://m.onkey.org/active-record-query-interface , 2010.&lt;br /&gt;
&lt;br /&gt;
Reza, Rizwan. &amp;quot;The Lowdown on Routes in Rails 3&amp;quot; http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. ''Agile Web Development with Rails: Third Edition'': The Pragmatic Programmers LLC, 2009.&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50337</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50337"/>
		<updated>2011-09-22T19:39:30Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Rails 1.0 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
The original Ruby on Rails platform was extracted from the web-based project-management tool called Basecamp developed by 37signals. Ruby on Rails’ creator, David Heinemeier Hansson, began work on the Rails in early 2003 and released it as open source code in 2004, but it wasn’t until 2005 that Hansson shared commit rights to the project. [REFERENCE] Hansson designed Ruby on Rails to be an out of the box development framework that includes everything a programmer needs to create database-driven web applications according to the Model-View-Control pattern of separation. Ruby on Rails is based on the two key paradigms of Convention over Configuration [WIKI LINK] and Don’t Repeat Yourself (DRY) [WIKI LINK].&lt;br /&gt;
&lt;br /&gt;
=History of Rails Development=&lt;br /&gt;
Throughout the development of the Rails framework there have been four significant releases alongside of the usual patches, fixes, and upgrades. &lt;br /&gt;
&lt;br /&gt;
===Rails 1.0===&lt;br /&gt;
Initially, Rails 1.0 was released 15 months after the original source was made available &amp;lt;ref&amp;gt;Rails 1.0 &amp;lt;/ref&amp;gt;. This version contained the structural foundation of what would become a powerful open source motivator for the Ruby platform.  Though there were patches and code adjustments in Rails 1.2 the next major release was Rails 2.0.&lt;br /&gt;
&lt;br /&gt;
===Rails 2.0 ===&lt;br /&gt;
Rails 2.0 was a major facelift for the framework and much of the effort went into improving the resources and polishing the overall package by making it more lean. Among some of these improvements was a focus on increasing security by adding a new module to work with HTTP Basic Authentication and providing a built-in mechanism for dealing with CRSF attacks. Other improvements were in the areas of changing certain syntactical nomenclature as well as simplifying the format for a few templates. &lt;br /&gt;
&lt;br /&gt;
===Rails 2.3===&lt;br /&gt;
The next major update was Rails 2.3 at it was the most structural update prior to Rails 3.0 being released. The two primary changes in the architecture of the Rails application was the complete integration of the Rack modular web server interface and a renewed support for Rails Engines. {link 2} Alongside of these changes were updates to templates, the addition of built-in support for HTTP Digest Authentication, nested forms models, Metal, and improved caching performance among other things.&lt;br /&gt;
&lt;br /&gt;
===Rails 3.0===&lt;br /&gt;
Between Rails 2.3 and the release of Rails 3.0, Rails announced that it would merge with Merb and that Rails 3.0 would be the successor to both Rails 2 and Merb. Merb which is short for Mongrel+Erb, key feature that prompted the join Rails was its component modularity, extensible API design, and vertical scalability.&lt;br /&gt;
&lt;br /&gt;
=Rails 2 vs. Rails 3=&lt;br /&gt;
&lt;br /&gt;
As you can see a lot changed since the first release of Rails. We will explore a few of the key areas that changed between Rails 2 and Rails 3 that will affect a developer's everyday coding.&lt;br /&gt;
&lt;br /&gt;
==Rails Application Scripts==&lt;br /&gt;
&lt;br /&gt;
When you create a new Rails application, a directory named &amp;lt;tt&amp;gt;script&amp;lt;/tt&amp;gt; is generated. In Rails 2, this directory is populated with a set of scripts that can be executed by the programmer. A few of the more important ones are as follows&amp;lt;ref&amp;gt;Ruby 2009, pp 257-258 &amp;lt;/ref&amp;gt;:&lt;br /&gt;
*&amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt;: Used to generate code for controllers, mailers, models, scaffolds and other sets of code.&lt;br /&gt;
*&amp;lt;tt&amp;gt;destroy&amp;lt;/tt&amp;gt;: Used to destroy code created by the &amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
*&amp;lt;tt&amp;gt;server&amp;lt;/tt&amp;gt;: Starts up the Rails application in a self-contained web server.&lt;br /&gt;
*&amp;lt;tt&amp;gt;plugin&amp;lt;/tt&amp;gt;: Helps with the installation and administration of plug-ins to the Rails framework.&lt;br /&gt;
*&amp;lt;tt&amp;gt;performance&amp;lt;/tt&amp;gt; directory: Contains scripts used by the programmer to help understand the performance characteristics of the application being built.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with all of these scripts and compiles them all into a single script named &amp;lt;tt&amp;gt;rails&amp;lt;/tt&amp;gt;. With this script, the programmer can access all of the functionality that was available with the separate scripts. To make things even easier, the necessity to call this script outright is even unnecessary. By using the operating systems installed &amp;lt;tt&amp;gt;'''rails'''&amp;lt;/tt&amp;gt; command (ex, &amp;lt;tt&amp;gt;/usr/bin/rails&amp;lt;/tt&amp;gt; on Linux systems) in the root of any Rails 3 application, it will know to call this script.&lt;br /&gt;
&lt;br /&gt;
==Gem Dependencies==&lt;br /&gt;
&lt;br /&gt;
The ability to extend the Rails framework using a Gem is a very powerful feature. In Rails 2, the &amp;lt;tt&amp;gt;config/environment.rb&amp;lt;/tt&amp;gt; file had to be edited in order to tell the Rails application to require specific Gems using the &amp;lt;tt&amp;gt;'''config.gem'''&amp;lt;/tt&amp;gt; method. To ensure that the Gems were actually installed on the system running the Rails application, the programmer had to execute the &amp;lt;tt&amp;gt;rake gem:install&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with the need to have that manual process of installing Gems by utilizing a file named &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; in the root of the application as well as the Bundler. The Bundler looks at the contents &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; and automatically installs any Gems that are missing from the system. This allows the programmer to focus on more important issues.&lt;br /&gt;
&lt;br /&gt;
==Routing DSL==&lt;br /&gt;
&lt;br /&gt;
The routing DSL for Rails went through a major reconditioning and has actually been renamed to Action Dispatch. This rewrite took the logic out of the Action Controller and is now a standalone piece of software resulting in a much cleaner implementation. As well, the definition of routes for each application are now named within your Application module instead of the Action Controller. The difference can be seen in the beginning line of the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
  map.resources :posts&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
AppName::Application.routes do&lt;br /&gt;
  resources :posts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With all these changes, the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file now has a new syntax. Here are some of the major differences&amp;lt;ref&amp;gt;Reza 2010&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Default Route===&lt;br /&gt;
&lt;br /&gt;
The differences between the default route are minor between the two versions, but the Rails 3 version is much more explicit, as the parentheses indicate parameters that are optional. Also worth noting is that this route is commented out in Rails 3 by default. The reasoning behind this is that there is a big push for a RESTful architecture with the Rails application and this type of route isn't really recommended for that kind of application. It can be uncommented but be warned that doing so will make all actions in every controller accessible via GET requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect ':controller/:action/:id'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match '/:controller(/:action(/:id))'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Regular Routes===&lt;br /&gt;
The way in which a regular route is defined is much more streamlined and readable. In Rails 2, you had to define a key for the controller and action. With Rails 3, this definition is streamlined with a &amp;lt;tt&amp;gt;:to&amp;lt;/tt&amp;gt; key where you define in a specific format the controller and action responsible for the request. Here is an example to illustrate this point:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect 'products/:id', :controller =&amp;gt; 'products', :action =&amp;gt; 'view'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'products/:id', :to =&amp;gt; 'catalog#view'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Routes===&lt;br /&gt;
Setting up named routes has changed a little bit as well. The rules for the regular routes apply here but the way that you define the name of the route has changed. Now, you utilize the &amp;lt;tt&amp;gt;:as&amp;lt;/tt&amp;gt; key to define the name of the route as shown here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.logout '/logout', :controller =&amp;gt; 'sessions', :action =&amp;gt; 'destroy'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'logout', :to =&amp;gt; 'sessions#destroy', :as =&amp;gt; &amp;quot;logout&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Empty Route===&lt;br /&gt;
The empty route defines where to route the application when a user navigates to the root of the website (for normal HTML sites, this is the same as the index page). Rails 2 had a quick way of defining it but in true Ruby fashion, Rails 3 makes it even simpler to define this route as you can see here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.root :controller =&amp;gt; &amp;quot;welcome&amp;quot;, :action =&amp;gt; 'show'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
root :to =&amp;gt; 'welcome#show'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RESTful Resources===&lt;br /&gt;
RESTful resources are very important and have been a part of Rails since version 1.2. Utilizing the &amp;lt;tt&amp;gt;rails generate scaffold&amp;lt;/tt&amp;gt; command, Rails will generate the route, controller, views and model necessary to have the basic REST functionality for a model. As you can see from the following example, defining a RESTful route is simple in either version. Rails 3 grants you the ability to use this line for multiple resources as well, saving lines of code and making it more readable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.resources :products&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
# Single resource definition&lt;br /&gt;
resources :products&lt;br /&gt;
# Multiple resource definitions&lt;br /&gt;
resources :products, :categories, :comments&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, Rails 3 provides you with the ability to extend the routing beyond the seven basic RESTful actions.  There are multiple ways in which you can extend this functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Defining extra collection RESTful actions within a block.&lt;br /&gt;
resources :products do&lt;br /&gt;
  collection do&lt;br /&gt;
    get  :sold&lt;br /&gt;
    post :on_offer&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# An inline extension.&lt;br /&gt;
resources :products do&lt;br /&gt;
  get :sold, :on =&amp;gt; :member&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Active Record==&lt;br /&gt;
Active Record, like all of the core Rails components, got a major overhaul. &lt;br /&gt;
&lt;br /&gt;
===Arel and the Query Interface===&lt;br /&gt;
The most prominent changes made that will be apparent to developers will be the integration with [https://github.com/nkallen/arel Arel]. With this integration, all of the core methods for Active Record return relations, allowing them to be chained together to develop complex queries. A full list and their functionality can be found in the [http://guides.rubyonrails.org/active_record_querying.html Rails Guide]. This will take the place of the options hash that would normally get passed in to one of the Active Record methods. To illustrate this, say we wanted to get a list of the first 10 products ordered alphabetically:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:order =&amp;gt; &amp;quot;name DESC&amp;quot;, :limit =&amp;gt; 10)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.order(&amp;quot;name DESC&amp;quot;).limit(10)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, there are a lot of methods that are losing the options parameter in their definitions&amp;lt;ref&amp;gt;Naik 2010&amp;lt;/ref&amp;gt;, including query methods find and all as well as calculation methods count and average. Also, the &amp;lt;tt&amp;gt;:all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;:first&amp;lt;/tt&amp;gt; parameters for the find method are being deprecated in favor of the &amp;lt;tt&amp;gt;all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;first&amp;lt;/tt&amp;gt; methods. Here are some examples of how things are changing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Changes to the find method.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:all)&lt;br /&gt;
Product.find(:first)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.all&lt;br /&gt;
Product.first&lt;br /&gt;
&lt;br /&gt;
# Examples of method options being removed.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.all(:joins =&amp;gt; :categories)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.joins(&amp;quot;categories&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Scopes===&lt;br /&gt;
The &amp;lt;tt&amp;gt;named_scope&amp;lt;/tt&amp;gt; definition has been changed to just &amp;lt;tt&amp;gt;scope&amp;lt;/tt&amp;gt;. The options hash is also being deprecated and should be replaced with the new methods provided by the Arel integration. Translating existing Rails 2 named scopes is easy enough:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
class Product&lt;br /&gt;
  named_scope :food, :conditions =&amp;gt; { :type =&amp;gt; &amp;quot;food&amp;quot; }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
class Product&lt;br /&gt;
  scope :food, where(:type =&amp;gt; &amp;quot;food&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==JavaScript Support&amp;lt;ref&amp;gt;Gadbois 2010&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
There are many JavaScript libraries available to programmers to make their web applications more dynamic and smooth looking. Whenever you created a Rails 2 application, it would automatically install the Prototype and Script.aculo.us frameworks. Rails also provided helpers for these two JavaScript frameworks so that a few method calls were all that you needed to make your application more fluid.&lt;br /&gt;
&lt;br /&gt;
Rails 3 introduces the idea of Unobtrusive JavaScript (UJS). The JavaScript becomes unobtrusive by not being written in with the HTML objects producing cleaner code that is easier to debug. This is mostly done with the utilization of HTML 5 custom attributes such as data-method, data-confirm, data-remote and data-disable-with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;http://host.com&amp;quot; id=&amp;quot;create-post&amp;quot; method=&amp;quot;post&amp;quot; data-remote=&amp;quot;true&amp;quot; data-confirm=&amp;quot;Are you sure you want to submit?&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By doing this, it also frees Rails from being dependent on the Prototype framework, allowing it to become ''JavaScript framework agnostic''. With its current implementation, the framework can support Prototype, jQuery and MooTools.&lt;br /&gt;
&lt;br /&gt;
It might also be helpful to point out that as of Rails 3.1, the jQuery framework has replaced Prototype as the default JavaScript framework. Programmers that wish to still use Prototype must remember to specify this when they create the Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new myapp -j prototype&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 1.0: Party like it's one oh oh!&amp;quot;. Riding Rails. Retrieved September 2011.&amp;quot; http://weblog.rubyonrails.org/2005/12/13/rails-1-0-party-like-its-one-oh-oh/&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.0: It's done!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2007/12/7/rails-2-0-it-s-done&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.3: Templates, Engines, Rack, Metal, much more!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2009/3/16/rails-2-3-templates-engines-rack-metal-much-more&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails Ruby on Rails 2.3 Release Notes&amp;quot; Riding Rails. Retrieved September 2011. http://guides.rubyonrails.org/2_3_release_notes.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Gadbois, John. ''Using Unobtrusive JavaScript and AJAX with Rails 3'' http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Lindsaar, Mikel. &amp;quot;Ruby on Rails 3.0 Release Notes&amp;quot; http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
&lt;br /&gt;
Naik, Pratik. &amp;quot;Active Record Query Interface 3.0&amp;quot; http://m.onkey.org/active-record-query-interface , 2010.&lt;br /&gt;
&lt;br /&gt;
Reza, Rizwan. &amp;quot;The Lowdown on Routes in Rails 3&amp;quot; http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. ''Agile Web Development with Rails: Third Edition'': The Pragmatic Programmers LLC, 2009.&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50336</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50336"/>
		<updated>2011-09-22T19:36:23Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
The original Ruby on Rails platform was extracted from the web-based project-management tool called Basecamp developed by 37signals. Ruby on Rails’ creator, David Heinemeier Hansson, began work on the Rails in early 2003 and released it as open source code in 2004, but it wasn’t until 2005 that Hansson shared commit rights to the project. [REFERENCE] Hansson designed Ruby on Rails to be an out of the box development framework that includes everything a programmer needs to create database-driven web applications according to the Model-View-Control pattern of separation. Ruby on Rails is based on the two key paradigms of Convention over Configuration [WIKI LINK] and Don’t Repeat Yourself (DRY) [WIKI LINK].&lt;br /&gt;
&lt;br /&gt;
=History of Rails Development=&lt;br /&gt;
Throughout the development of the Rails framework there have been four significant releases alongside of the usual patches, fixes, and upgrades. &lt;br /&gt;
&lt;br /&gt;
===Rails 1.0===&lt;br /&gt;
Initially, Rails 1.0 was released 15 months after the original source was made available. [REFERENCE] This version contained the structural foundation of what would become a powerful open source motivator for the Ruby platform.  Though there were patches and code adjustments in Rails 1.2 the next major release was Rails 2.0. &lt;br /&gt;
&lt;br /&gt;
===Rails 2.0 ===&lt;br /&gt;
Rails 2.0 was a major facelift for the framework and much of the effort went into improving the resources and polishing the overall package by making it more lean. Among some of these improvements was a focus on increasing security by adding a new module to work with HTTP Basic Authentication and providing a built-in mechanism for dealing with CRSF attacks. Other improvements were in the areas of changing certain syntactical nomenclature as well as simplifying the format for a few templates. &lt;br /&gt;
&lt;br /&gt;
===Rails 2.3===&lt;br /&gt;
The next major update was Rails 2.3 at it was the most structural update prior to Rails 3.0 being released. The two primary changes in the architecture of the Rails application was the complete integration of the Rack modular web server interface and a renewed support for Rails Engines. {link 2} Alongside of these changes were updates to templates, the addition of built-in support for HTTP Digest Authentication, nested forms models, Metal, and improved caching performance among other things.&lt;br /&gt;
&lt;br /&gt;
===Rails 3.0===&lt;br /&gt;
Between Rails 2.3 and the release of Rails 3.0, Rails announced that it would merge with Merb and that Rails 3.0 would be the successor to both Rails 2 and Merb. Merb which is short for Mongrel+Erb, key feature that prompted the join Rails was its component modularity, extensible API design, and vertical scalability.&lt;br /&gt;
&lt;br /&gt;
=Rails 2 vs. Rails 3=&lt;br /&gt;
&lt;br /&gt;
As you can see a lot changed since the first release of Rails. We will explore a few of the key areas that changed between Rails 2 and Rails 3 that will affect a developer's everyday coding.&lt;br /&gt;
&lt;br /&gt;
==Rails Application Scripts==&lt;br /&gt;
&lt;br /&gt;
When you create a new Rails application, a directory named &amp;lt;tt&amp;gt;script&amp;lt;/tt&amp;gt; is generated. In Rails 2, this directory is populated with a set of scripts that can be executed by the programmer. A few of the more important ones are as follows&amp;lt;ref&amp;gt;Ruby 2009, pp 257-258 &amp;lt;/ref&amp;gt;:&lt;br /&gt;
*&amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt;: Used to generate code for controllers, mailers, models, scaffolds and other sets of code.&lt;br /&gt;
*&amp;lt;tt&amp;gt;destroy&amp;lt;/tt&amp;gt;: Used to destroy code created by the &amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
*&amp;lt;tt&amp;gt;server&amp;lt;/tt&amp;gt;: Starts up the Rails application in a self-contained web server.&lt;br /&gt;
*&amp;lt;tt&amp;gt;plugin&amp;lt;/tt&amp;gt;: Helps with the installation and administration of plug-ins to the Rails framework.&lt;br /&gt;
*&amp;lt;tt&amp;gt;performance&amp;lt;/tt&amp;gt; directory: Contains scripts used by the programmer to help understand the performance characteristics of the application being built.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with all of these scripts and compiles them all into a single script named &amp;lt;tt&amp;gt;rails&amp;lt;/tt&amp;gt;. With this script, the programmer can access all of the functionality that was available with the separate scripts. To make things even easier, the necessity to call this script outright is even unnecessary. By using the operating systems installed &amp;lt;tt&amp;gt;'''rails'''&amp;lt;/tt&amp;gt; command (ex, &amp;lt;tt&amp;gt;/usr/bin/rails&amp;lt;/tt&amp;gt; on Linux systems) in the root of any Rails 3 application, it will know to call this script.&lt;br /&gt;
&lt;br /&gt;
==Gem Dependencies==&lt;br /&gt;
&lt;br /&gt;
The ability to extend the Rails framework using a Gem is a very powerful feature. In Rails 2, the &amp;lt;tt&amp;gt;config/environment.rb&amp;lt;/tt&amp;gt; file had to be edited in order to tell the Rails application to require specific Gems using the &amp;lt;tt&amp;gt;'''config.gem'''&amp;lt;/tt&amp;gt; method. To ensure that the Gems were actually installed on the system running the Rails application, the programmer had to execute the &amp;lt;tt&amp;gt;rake gem:install&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with the need to have that manual process of installing Gems by utilizing a file named &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; in the root of the application as well as the Bundler. The Bundler looks at the contents &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; and automatically installs any Gems that are missing from the system. This allows the programmer to focus on more important issues.&lt;br /&gt;
&lt;br /&gt;
==Routing DSL==&lt;br /&gt;
&lt;br /&gt;
The routing DSL for Rails went through a major reconditioning and has actually been renamed to Action Dispatch. This rewrite took the logic out of the Action Controller and is now a standalone piece of software resulting in a much cleaner implementation. As well, the definition of routes for each application are now named within your Application module instead of the Action Controller. The difference can be seen in the beginning line of the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
  map.resources :posts&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
AppName::Application.routes do&lt;br /&gt;
  resources :posts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With all these changes, the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file now has a new syntax. Here are some of the major differences&amp;lt;ref&amp;gt;Reza 2010&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Default Route===&lt;br /&gt;
&lt;br /&gt;
The differences between the default route are minor between the two versions, but the Rails 3 version is much more explicit, as the parentheses indicate parameters that are optional. Also worth noting is that this route is commented out in Rails 3 by default. The reasoning behind this is that there is a big push for a RESTful architecture with the Rails application and this type of route isn't really recommended for that kind of application. It can be uncommented but be warned that doing so will make all actions in every controller accessible via GET requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect ':controller/:action/:id'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match '/:controller(/:action(/:id))'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Regular Routes===&lt;br /&gt;
The way in which a regular route is defined is much more streamlined and readable. In Rails 2, you had to define a key for the controller and action. With Rails 3, this definition is streamlined with a &amp;lt;tt&amp;gt;:to&amp;lt;/tt&amp;gt; key where you define in a specific format the controller and action responsible for the request. Here is an example to illustrate this point:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect 'products/:id', :controller =&amp;gt; 'products', :action =&amp;gt; 'view'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'products/:id', :to =&amp;gt; 'catalog#view'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Routes===&lt;br /&gt;
Setting up named routes has changed a little bit as well. The rules for the regular routes apply here but the way that you define the name of the route has changed. Now, you utilize the &amp;lt;tt&amp;gt;:as&amp;lt;/tt&amp;gt; key to define the name of the route as shown here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.logout '/logout', :controller =&amp;gt; 'sessions', :action =&amp;gt; 'destroy'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'logout', :to =&amp;gt; 'sessions#destroy', :as =&amp;gt; &amp;quot;logout&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Empty Route===&lt;br /&gt;
The empty route defines where to route the application when a user navigates to the root of the website (for normal HTML sites, this is the same as the index page). Rails 2 had a quick way of defining it but in true Ruby fashion, Rails 3 makes it even simpler to define this route as you can see here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.root :controller =&amp;gt; &amp;quot;welcome&amp;quot;, :action =&amp;gt; 'show'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
root :to =&amp;gt; 'welcome#show'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RESTful Resources===&lt;br /&gt;
RESTful resources are very important and have been a part of Rails since version 1.2. Utilizing the &amp;lt;tt&amp;gt;rails generate scaffold&amp;lt;/tt&amp;gt; command, Rails will generate the route, controller, views and model necessary to have the basic REST functionality for a model. As you can see from the following example, defining a RESTful route is simple in either version. Rails 3 grants you the ability to use this line for multiple resources as well, saving lines of code and making it more readable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.resources :products&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
# Single resource definition&lt;br /&gt;
resources :products&lt;br /&gt;
# Multiple resource definitions&lt;br /&gt;
resources :products, :categories, :comments&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, Rails 3 provides you with the ability to extend the routing beyond the seven basic RESTful actions.  There are multiple ways in which you can extend this functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Defining extra collection RESTful actions within a block.&lt;br /&gt;
resources :products do&lt;br /&gt;
  collection do&lt;br /&gt;
    get  :sold&lt;br /&gt;
    post :on_offer&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# An inline extension.&lt;br /&gt;
resources :products do&lt;br /&gt;
  get :sold, :on =&amp;gt; :member&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Active Record==&lt;br /&gt;
Active Record, like all of the core Rails components, got a major overhaul. &lt;br /&gt;
&lt;br /&gt;
===Arel and the Query Interface===&lt;br /&gt;
The most prominent changes made that will be apparent to developers will be the integration with [https://github.com/nkallen/arel Arel]. With this integration, all of the core methods for Active Record return relations, allowing them to be chained together to develop complex queries. A full list and their functionality can be found in the [http://guides.rubyonrails.org/active_record_querying.html Rails Guide]. This will take the place of the options hash that would normally get passed in to one of the Active Record methods. To illustrate this, say we wanted to get a list of the first 10 products ordered alphabetically:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:order =&amp;gt; &amp;quot;name DESC&amp;quot;, :limit =&amp;gt; 10)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.order(&amp;quot;name DESC&amp;quot;).limit(10)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, there are a lot of methods that are losing the options parameter in their definitions&amp;lt;ref&amp;gt;Naik 2010&amp;lt;/ref&amp;gt;, including query methods find and all as well as calculation methods count and average. Also, the &amp;lt;tt&amp;gt;:all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;:first&amp;lt;/tt&amp;gt; parameters for the find method are being deprecated in favor of the &amp;lt;tt&amp;gt;all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;first&amp;lt;/tt&amp;gt; methods. Here are some examples of how things are changing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Changes to the find method.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:all)&lt;br /&gt;
Product.find(:first)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.all&lt;br /&gt;
Product.first&lt;br /&gt;
&lt;br /&gt;
# Examples of method options being removed.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.all(:joins =&amp;gt; :categories)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.joins(&amp;quot;categories&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Scopes===&lt;br /&gt;
The &amp;lt;tt&amp;gt;named_scope&amp;lt;/tt&amp;gt; definition has been changed to just &amp;lt;tt&amp;gt;scope&amp;lt;/tt&amp;gt;. The options hash is also being deprecated and should be replaced with the new methods provided by the Arel integration. Translating existing Rails 2 named scopes is easy enough:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
class Product&lt;br /&gt;
  named_scope :food, :conditions =&amp;gt; { :type =&amp;gt; &amp;quot;food&amp;quot; }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
class Product&lt;br /&gt;
  scope :food, where(:type =&amp;gt; &amp;quot;food&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==JavaScript Support&amp;lt;ref&amp;gt;Gadbois 2010&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
There are many JavaScript libraries available to programmers to make their web applications more dynamic and smooth looking. Whenever you created a Rails 2 application, it would automatically install the Prototype and Script.aculo.us frameworks. Rails also provided helpers for these two JavaScript frameworks so that a few method calls were all that you needed to make your application more fluid.&lt;br /&gt;
&lt;br /&gt;
Rails 3 introduces the idea of Unobtrusive JavaScript (UJS). The JavaScript becomes unobtrusive by not being written in with the HTML objects producing cleaner code that is easier to debug. This is mostly done with the utilization of HTML 5 custom attributes such as data-method, data-confirm, data-remote and data-disable-with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;http://host.com&amp;quot; id=&amp;quot;create-post&amp;quot; method=&amp;quot;post&amp;quot; data-remote=&amp;quot;true&amp;quot; data-confirm=&amp;quot;Are you sure you want to submit?&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By doing this, it also frees Rails from being dependent on the Prototype framework, allowing it to become ''JavaScript framework agnostic''. With its current implementation, the framework can support Prototype, jQuery and MooTools.&lt;br /&gt;
&lt;br /&gt;
It might also be helpful to point out that as of Rails 3.1, the jQuery framework has replaced Prototype as the default JavaScript framework. Programmers that wish to still use Prototype must remember to specify this when they create the Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new myapp -j prototype&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 1.0: Party like it's one oh oh!&amp;quot;. Riding Rails. Retrieved September 2011.&amp;quot; http://weblog.rubyonrails.org/2005/12/13/rails-1-0-party-like-its-one-oh-oh/&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.0: It's done!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2007/12/7/rails-2-0-it-s-done&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails 2.3: Templates, Engines, Rack, Metal, much more!&amp;quot;. Riding Rails. Retrieved September 2011. http://weblog.rubyonrails.org/2009/3/16/rails-2-3-templates-engines-rack-metal-much-more&lt;br /&gt;
&lt;br /&gt;
&amp;quot;Rails Ruby on Rails 2.3 Release Notes&amp;quot; Riding Rails. Retrieved September 2011. http://guides.rubyonrails.org/2_3_release_notes.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Gadbois, John. ''Using Unobtrusive JavaScript and AJAX with Rails 3'' http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Lindsaar, Mikel. &amp;quot;Ruby on Rails 3.0 Release Notes&amp;quot; http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
&lt;br /&gt;
Naik, Pratik. &amp;quot;Active Record Query Interface 3.0&amp;quot; http://m.onkey.org/active-record-query-interface , 2010.&lt;br /&gt;
&lt;br /&gt;
Reza, Rizwan. &amp;quot;The Lowdown on Routes in Rails 3&amp;quot; http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. ''Agile Web Development with Rails: Third Edition'': The Pragmatic Programmers LLC, 2009.&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50311</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50311"/>
		<updated>2011-09-22T18:12:09Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Rails 3.0 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
The original Ruby on Rail platform was extracted from the web-based project-management tool called Basecamp developed by 37signals. Ruby on Rails’ creator, David Heinemeier Hansson, began work on the Rails in early 2003 and released it as open source code in 2004, but it wasn’t until 2005 that Hansson shared commit rights to the project. [REFERENCE] Hansson designed Ruby on Rails to be an out of the box development framework that includes everything a programmer needs to create database-driven web applications according to the Model-View-Control pattern of separation. Ruby on Rails is based on the two key paradigms of Convention over Configuration [WIKI LINK] and Don’t Repeat Yourself (DRY) [WIKI LINK].&lt;br /&gt;
&lt;br /&gt;
=History of Rails Development=&lt;br /&gt;
Throughout the development of the Rails framework there have been four significant releases alongside of the usual patches, fixes, and upgrades. &lt;br /&gt;
&lt;br /&gt;
===Rails 1.0===&lt;br /&gt;
Initially, Rails 1.0 was released 15 months after the original source was made available. [REFERENCE] This version contained the structural foundation of what would become a powerful open source motivator for the Ruby platform.  Though there were patches and code adjustments in Rails 1.2 the next major release was Rails 2.0. &lt;br /&gt;
&lt;br /&gt;
===Rails 2.0 ===&lt;br /&gt;
Rails 2.0 was a major facelift for the framework and much of the effort went into improving the resources and polishing the overall package by making it more lean. Among some of these improvements was a focus on increasing security by adding a new module to work with HTTP Basic Authentication and providing a built-in mechanism for dealing with CRSF attacks. Other improvements were in the areas of changing certain syntactical nomenclature as well as simplifying the format for a few templates. &lt;br /&gt;
&lt;br /&gt;
===Rails 2.3===&lt;br /&gt;
The next major update was Rails 2.3 at it was the most structural update prior to Rails 3.0 being released. The two primary changes in the architecture of the Rails application was the complete integration of the Rack modular web server interface and a renewed support for Rails Engines. {link 2} Alongside of these changes were updates to templates, the addition of built-in support for HTTP Digest Authentication, nested forms models, Metal, and improved caching performance among other things.&lt;br /&gt;
&lt;br /&gt;
===Rails 3.0===&lt;br /&gt;
Between Rails 2.3 and the release of Rails 3.0, Rails announced that it would merge with Merb and that Rails 3.0 would be the successor to both Rails 2 and Merb. Merb which is short for Mongrel+Erb, key feature that prompted the join Rails was its component modularity, extensible API design, and vertical scalability.&lt;br /&gt;
&lt;br /&gt;
=Rails 2 vs. Rails 3=&lt;br /&gt;
&lt;br /&gt;
A lot changed in the update from Rails 2 to Rails 3. We will explore a few of the key areas that will affect a developer's everyday coding.&lt;br /&gt;
&lt;br /&gt;
==Rails Application Scripts==&lt;br /&gt;
&lt;br /&gt;
When you create a new Rails application, a directory named &amp;lt;tt&amp;gt;script&amp;lt;/tt&amp;gt; is generated. In Rails 2, this directory is populated with a set of scripts that can be executed by the programmer. A few of the more important ones are as follows&amp;lt;ref&amp;gt;Ruby 2009, pp 257-258 &amp;lt;/ref&amp;gt;:&lt;br /&gt;
*&amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt;: Used to generate code for controllers, mailers, models, scaffolds and other sets of code.&lt;br /&gt;
*&amp;lt;tt&amp;gt;destroy&amp;lt;/tt&amp;gt;: Used to destroy code created by the &amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
*&amp;lt;tt&amp;gt;server&amp;lt;/tt&amp;gt;: Starts up the Rails application in a self-contained web server.&lt;br /&gt;
*&amp;lt;tt&amp;gt;plugin&amp;lt;/tt&amp;gt;: Helps with the installation and administration of plug-ins to the Rails framework.&lt;br /&gt;
*&amp;lt;tt&amp;gt;performance&amp;lt;/tt&amp;gt; directory: Contains scripts used by the programmer to help understand the performance characteristics of the application being built.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with all of these scripts and compiles them all into a single script named &amp;lt;tt&amp;gt;rails&amp;lt;/tt&amp;gt;. With this script, the programmer can access all of the functionality that was available with the separate scripts. To make things even easier, the necessity to call this script outright is even unnecessary. By using the operating systems installed &amp;lt;tt&amp;gt;'''rails'''&amp;lt;/tt&amp;gt; command (ex, &amp;lt;tt&amp;gt;/usr/bin/rails&amp;lt;/tt&amp;gt; on Linux systems) in the root of any Rails 3 application, it will know to call this script.&lt;br /&gt;
&lt;br /&gt;
==Gem Dependencies==&lt;br /&gt;
&lt;br /&gt;
The ability to extend the Rails framework using a Gem is a very powerful feature. In Rails 2, the &amp;lt;tt&amp;gt;config/environment.rb&amp;lt;/tt&amp;gt; file had to be edited in order to tell the Rails application to require specific Gems using the &amp;lt;tt&amp;gt;'''config.gem'''&amp;lt;/tt&amp;gt; method. To ensure that the Gems were actually installed on the system running the Rails application, the programmer had to execute the &amp;lt;tt&amp;gt;rake gem:install&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with the need to have that manual process of installing Gems by utilizing a file named &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; in the root of the application as well as the Bundler. The Bundler looks at the contents &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; and automatically installs any Gems that are missing from the system. This allows the programmer to focus on more important issues.&lt;br /&gt;
&lt;br /&gt;
==Routing DSL==&lt;br /&gt;
&lt;br /&gt;
The routing DSL for Rails went through a major reconditioning and has actually been renamed to Action Dispatch. This rewrite took the logic out of the Action Controller and is now a standalone piece of software resulting in a much cleaner implementation. As well, the definition of routes for each application are now named within your Application module instead of the Action Controller. The difference can be seen in the beginning line of the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
  map.resources :posts&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
AppName::Application.routes do&lt;br /&gt;
  resources :posts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With all these changes, the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file now has a new syntax. Here are some of the major differences&amp;lt;ref&amp;gt;Reza 2010&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Default Route===&lt;br /&gt;
&lt;br /&gt;
The differences between the default route are minor between the two versions, but the Rails 3 version is much more explicit, as the parentheses indicate parameters that are optional. Also worth noting is that this route is commented out in Rails 3 by default. The reasoning behind this is that there is a big push for a RESTful architecture with the Rails application and this type of route isn't really recommended for that kind of application. It can be uncommented but be warned that doing so will make all actions in every controller accessible via GET requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect ':controller/:action/:id'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match '/:controller(/:action(/:id))'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Regular Routes===&lt;br /&gt;
The way in which a regular route is defined is much more streamlined and readable. In Rails 2, you had to define a key for the controller and action. With Rails 3, this definition is streamlined with a &amp;lt;tt&amp;gt;:to&amp;lt;/tt&amp;gt; key where you define in a specific format the controller and action responsible for the request. Here is an example to illustrate this point:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect 'products/:id', :controller =&amp;gt; 'products', :action =&amp;gt; 'view'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'products/:id', :to =&amp;gt; 'catalog#view'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Routes===&lt;br /&gt;
Setting up named routes has changed a little bit as well. The rules for the regular routes apply here but the way that you define the name of the route has changed. Now, you utilize the &amp;lt;tt&amp;gt;:as&amp;lt;/tt&amp;gt; key to define the name of the route as shown here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.logout '/logout', :controller =&amp;gt; 'sessions', :action =&amp;gt; 'destroy'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'logout', :to =&amp;gt; 'sessions#destroy', :as =&amp;gt; &amp;quot;logout&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Empty Route===&lt;br /&gt;
The empty route defines where to route the application when a user navigates to the root of the website (for normal HTML sites, this is the same as the index page). Rails 2 had a quick way of defining it but in true Ruby fashion, Rails 3 makes it even simpler to define this route as you can see here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.root :controller =&amp;gt; &amp;quot;welcome&amp;quot;, :action =&amp;gt; 'show'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
root :to =&amp;gt; 'welcome#show'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RESTful Resources===&lt;br /&gt;
RESTful resources are very important and have been a part of Rails since version 1.2. Utilizing the &amp;lt;tt&amp;gt;rails generate scaffold&amp;lt;/tt&amp;gt; command, Rails will generate the route, controller, views and model necessary to have the basic REST functionality for a model. As you can see from the following example, defining a RESTful route is simple in either version. Rails 3 grants you the ability to use this line for multiple resources as well, saving lines of code and making it more readable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.resources :products&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
# Single resource definition&lt;br /&gt;
resources :products&lt;br /&gt;
# Multiple resource definitions&lt;br /&gt;
resources :products, :categories, :comments&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, Rails 3 provides you with the ability to extend the routing beyond the seven basic RESTful actions.  There are multiple ways in which you can extend this functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Defining extra collection RESTful actions within a block.&lt;br /&gt;
resources :products do&lt;br /&gt;
  collection do&lt;br /&gt;
    get  :sold&lt;br /&gt;
    post :on_offer&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# An inline extension.&lt;br /&gt;
resources :products do&lt;br /&gt;
  get :sold, :on =&amp;gt; :member&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Active Record==&lt;br /&gt;
Active Record, like all of the core Rails components, got a major overhaul. &lt;br /&gt;
&lt;br /&gt;
===Arel and the Query Interface===&lt;br /&gt;
The most prominent changes made that will be apparent to developers will be the integration with [https://github.com/nkallen/arel Arel]. With this integration, all of the core methods for Active Record return relations, allowing them to be chained together to develop complex queries. A full list and their functionality can be found in the [http://guides.rubyonrails.org/active_record_querying.html Rails Guide]. This will take the place of the options hash that would normally get passed in to one of the Active Record methods. To illustrate this, say we wanted to get a list of the first 10 products ordered alphabetically:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:order =&amp;gt; &amp;quot;name DESC&amp;quot;, :limit =&amp;gt; 10)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.order(&amp;quot;name DESC&amp;quot;).limit(10)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, there are a lot of methods that are losing the options parameter in their definitions&amp;lt;ref&amp;gt;Naik 2010&amp;lt;/ref&amp;gt;, including query methods find and all as well as calculation methods count and average. Also, the &amp;lt;tt&amp;gt;:all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;:first&amp;lt;/tt&amp;gt; parameters for the find method are being deprecated in favor of the &amp;lt;tt&amp;gt;all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;first&amp;lt;/tt&amp;gt; methods. Here are some examples of how things are changing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Changes to the find method.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:all)&lt;br /&gt;
Product.find(:first)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.all&lt;br /&gt;
Product.first&lt;br /&gt;
&lt;br /&gt;
# Examples of method options being removed.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.all(:joins =&amp;gt; :categories)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.joins(&amp;quot;categories&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Scopes===&lt;br /&gt;
The &amp;lt;tt&amp;gt;named_scope&amp;lt;/tt&amp;gt; definition has been changed to just &amp;lt;tt&amp;gt;scope&amp;lt;/tt&amp;gt;. The options hash is also being deprecated and should be replaced with the new methods provided by the Arel integration. Translating existing Rails 2 named scopes is easy enough:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
class Product&lt;br /&gt;
  named_scope :food, :conditions =&amp;gt; { :type =&amp;gt; &amp;quot;food&amp;quot; }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
class Product&lt;br /&gt;
  scope :food, where(:type =&amp;gt; &amp;quot;food&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==JavaScript Support&amp;lt;ref&amp;gt;Gadbois 2010&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
There are many JavaScript libraries available to programmers to make their web applications more dynamic and smooth looking. Whenever you created a Rails 2 application, it would automatically install the Prototype and Script.aculo.us frameworks. Rails also provided helpers for these two JavaScript frameworks so that a few method calls were all that you needed to make your application more fluid.&lt;br /&gt;
&lt;br /&gt;
Rails 3 introduces the idea of Unobtrusive JavaScript (UJS). The JavaScript becomes unobtrusive by not being written in with the HTML objects producing cleaner code that is easier to debug. This is mostly done with the utilization of HTML 5 custom attributes such as data-method, data-confirm, data-remote and data-disable-with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;http://host.com&amp;quot; id=&amp;quot;create-post&amp;quot; method=&amp;quot;post&amp;quot; data-remote=&amp;quot;true&amp;quot; data-confirm=&amp;quot;Are you sure you want to submit?&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By doing this, it also frees Rails from being dependent on the Prototype framework, allowing it to become ''JavaScript framework agnostic''. With its current implementation, the framework can support Prototype, jQuery and MooTools.&lt;br /&gt;
&lt;br /&gt;
It might also be helpful to point out that as of Rails 3.1, the jQuery framework has replaced Prototype as the default JavaScript framework. Programmers that wish to still use Prototype must remember to specify this when they create the Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new myapp -j prototype&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Gadbois, John. ''Using Unobtrusive JavaScript and AJAX with Rails 3'' http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Lindsaar, Mikel. &amp;quot;Ruby on Rails 3.0 Release Notes&amp;quot; http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
&lt;br /&gt;
Naik, Pratik. &amp;quot;Active Record Query Interface 3.0&amp;quot; http://m.onkey.org/active-record-query-interface , 2010.&lt;br /&gt;
&lt;br /&gt;
Reza, Rizwan. &amp;quot;The Lowdown on Routes in Rails 3&amp;quot; http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. ''Agile Web Development with Rails: Third Edition'': The Pragmatic Programmers LLC, 2009.&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50310</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2f mm</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2f_mm&amp;diff=50310"/>
		<updated>2011-09-22T18:11:55Z</updated>

		<summary type="html">&lt;p&gt;Ttbrown4: /* Rails 2.3 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Introduction=&lt;br /&gt;
The original Ruby on Rail platform was extracted from the web-based project-management tool called Basecamp developed by 37signals. Ruby on Rails’ creator, David Heinemeier Hansson, began work on the Rails in early 2003 and released it as open source code in 2004, but it wasn’t until 2005 that Hansson shared commit rights to the project. [REFERENCE] Hansson designed Ruby on Rails to be an out of the box development framework that includes everything a programmer needs to create database-driven web applications according to the Model-View-Control pattern of separation. Ruby on Rails is based on the two key paradigms of Convention over Configuration [WIKI LINK] and Don’t Repeat Yourself (DRY) [WIKI LINK].&lt;br /&gt;
&lt;br /&gt;
=History of Rails Development=&lt;br /&gt;
Throughout the development of the Rails framework there have been four significant releases alongside of the usual patches, fixes, and upgrades. &lt;br /&gt;
&lt;br /&gt;
===Rails 1.0===&lt;br /&gt;
Initially, Rails 1.0 was released 15 months after the original source was made available. [REFERENCE] This version contained the structural foundation of what would become a powerful open source motivator for the Ruby platform.  Though there were patches and code adjustments in Rails 1.2 the next major release was Rails 2.0. &lt;br /&gt;
&lt;br /&gt;
===Rails 2.0 ===&lt;br /&gt;
Rails 2.0 was a major facelift for the framework and much of the effort went into improving the resources and polishing the overall package by making it more lean. Among some of these improvements was a focus on increasing security by adding a new module to work with HTTP Basic Authentication and providing a built-in mechanism for dealing with CRSF attacks. Other improvements were in the areas of changing certain syntactical nomenclature as well as simplifying the format for a few templates. &lt;br /&gt;
&lt;br /&gt;
===Rails 2.3===&lt;br /&gt;
The next major update was Rails 2.3 at it was the most structural update prior to Rails 3.0 being released. The two primary changes in the architecture of the Rails application was the complete integration of the Rack modular web server interface and a renewed support for Rails Engines. {link 2} Alongside of these changes were updates to templates, the addition of built-in support for HTTP Digest Authentication, nested forms models, Metal, and improved caching performance among other things.&lt;br /&gt;
&lt;br /&gt;
===Rails 3.0===&lt;br /&gt;
&lt;br /&gt;
=Rails 2 vs. Rails 3=&lt;br /&gt;
&lt;br /&gt;
A lot changed in the update from Rails 2 to Rails 3. We will explore a few of the key areas that will affect a developer's everyday coding.&lt;br /&gt;
&lt;br /&gt;
==Rails Application Scripts==&lt;br /&gt;
&lt;br /&gt;
When you create a new Rails application, a directory named &amp;lt;tt&amp;gt;script&amp;lt;/tt&amp;gt; is generated. In Rails 2, this directory is populated with a set of scripts that can be executed by the programmer. A few of the more important ones are as follows&amp;lt;ref&amp;gt;Ruby 2009, pp 257-258 &amp;lt;/ref&amp;gt;:&lt;br /&gt;
*&amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt;: Used to generate code for controllers, mailers, models, scaffolds and other sets of code.&lt;br /&gt;
*&amp;lt;tt&amp;gt;destroy&amp;lt;/tt&amp;gt;: Used to destroy code created by the &amp;lt;tt&amp;gt;generate&amp;lt;/tt&amp;gt; method.&lt;br /&gt;
*&amp;lt;tt&amp;gt;server&amp;lt;/tt&amp;gt;: Starts up the Rails application in a self-contained web server.&lt;br /&gt;
*&amp;lt;tt&amp;gt;plugin&amp;lt;/tt&amp;gt;: Helps with the installation and administration of plug-ins to the Rails framework.&lt;br /&gt;
*&amp;lt;tt&amp;gt;performance&amp;lt;/tt&amp;gt; directory: Contains scripts used by the programmer to help understand the performance characteristics of the application being built.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with all of these scripts and compiles them all into a single script named &amp;lt;tt&amp;gt;rails&amp;lt;/tt&amp;gt;. With this script, the programmer can access all of the functionality that was available with the separate scripts. To make things even easier, the necessity to call this script outright is even unnecessary. By using the operating systems installed &amp;lt;tt&amp;gt;'''rails'''&amp;lt;/tt&amp;gt; command (ex, &amp;lt;tt&amp;gt;/usr/bin/rails&amp;lt;/tt&amp;gt; on Linux systems) in the root of any Rails 3 application, it will know to call this script.&lt;br /&gt;
&lt;br /&gt;
==Gem Dependencies==&lt;br /&gt;
&lt;br /&gt;
The ability to extend the Rails framework using a Gem is a very powerful feature. In Rails 2, the &amp;lt;tt&amp;gt;config/environment.rb&amp;lt;/tt&amp;gt; file had to be edited in order to tell the Rails application to require specific Gems using the &amp;lt;tt&amp;gt;'''config.gem'''&amp;lt;/tt&amp;gt; method. To ensure that the Gems were actually installed on the system running the Rails application, the programmer had to execute the &amp;lt;tt&amp;gt;rake gem:install&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
Rails 3 does away with the need to have that manual process of installing Gems by utilizing a file named &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; in the root of the application as well as the Bundler. The Bundler looks at the contents &amp;lt;tt&amp;gt;Gemfile&amp;lt;/tt&amp;gt; and automatically installs any Gems that are missing from the system. This allows the programmer to focus on more important issues.&lt;br /&gt;
&lt;br /&gt;
==Routing DSL==&lt;br /&gt;
&lt;br /&gt;
The routing DSL for Rails went through a major reconditioning and has actually been renamed to Action Dispatch. This rewrite took the logic out of the Action Controller and is now a standalone piece of software resulting in a much cleaner implementation. As well, the definition of routes for each application are now named within your Application module instead of the Action Controller. The difference can be seen in the beginning line of the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
ActionController::Routing::Routes.draw do |map|&lt;br /&gt;
  map.resources :posts&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
AppName::Application.routes do&lt;br /&gt;
  resources :posts&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With all these changes, the &amp;lt;tt&amp;gt;config/routes.rb&amp;lt;/tt&amp;gt; file now has a new syntax. Here are some of the major differences&amp;lt;ref&amp;gt;Reza 2010&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===Default Route===&lt;br /&gt;
&lt;br /&gt;
The differences between the default route are minor between the two versions, but the Rails 3 version is much more explicit, as the parentheses indicate parameters that are optional. Also worth noting is that this route is commented out in Rails 3 by default. The reasoning behind this is that there is a big push for a RESTful architecture with the Rails application and this type of route isn't really recommended for that kind of application. It can be uncommented but be warned that doing so will make all actions in every controller accessible via GET requests.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect ':controller/:action/:id'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match '/:controller(/:action(/:id))'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Regular Routes===&lt;br /&gt;
The way in which a regular route is defined is much more streamlined and readable. In Rails 2, you had to define a key for the controller and action. With Rails 3, this definition is streamlined with a &amp;lt;tt&amp;gt;:to&amp;lt;/tt&amp;gt; key where you define in a specific format the controller and action responsible for the request. Here is an example to illustrate this point:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.connect 'products/:id', :controller =&amp;gt; 'products', :action =&amp;gt; 'view'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'products/:id', :to =&amp;gt; 'catalog#view'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Routes===&lt;br /&gt;
Setting up named routes has changed a little bit as well. The rules for the regular routes apply here but the way that you define the name of the route has changed. Now, you utilize the &amp;lt;tt&amp;gt;:as&amp;lt;/tt&amp;gt; key to define the name of the route as shown here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.logout '/logout', :controller =&amp;gt; 'sessions', :action =&amp;gt; 'destroy'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
match 'logout', :to =&amp;gt; 'sessions#destroy', :as =&amp;gt; &amp;quot;logout&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Empty Route===&lt;br /&gt;
The empty route defines where to route the application when a user navigates to the root of the website (for normal HTML sites, this is the same as the index page). Rails 2 had a quick way of defining it but in true Ruby fashion, Rails 3 makes it even simpler to define this route as you can see here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.root :controller =&amp;gt; &amp;quot;welcome&amp;quot;, :action =&amp;gt; 'show'&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
root :to =&amp;gt; 'welcome#show'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===RESTful Resources===&lt;br /&gt;
RESTful resources are very important and have been a part of Rails since version 1.2. Utilizing the &amp;lt;tt&amp;gt;rails generate scaffold&amp;lt;/tt&amp;gt; command, Rails will generate the route, controller, views and model necessary to have the basic REST functionality for a model. As you can see from the following example, defining a RESTful route is simple in either version. Rails 3 grants you the ability to use this line for multiple resources as well, saving lines of code and making it more readable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
map.resources :products&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
# Single resource definition&lt;br /&gt;
resources :products&lt;br /&gt;
# Multiple resource definitions&lt;br /&gt;
resources :products, :categories, :comments&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, Rails 3 provides you with the ability to extend the routing beyond the seven basic RESTful actions.  There are multiple ways in which you can extend this functionality:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Defining extra collection RESTful actions within a block.&lt;br /&gt;
resources :products do&lt;br /&gt;
  collection do&lt;br /&gt;
    get  :sold&lt;br /&gt;
    post :on_offer&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
# An inline extension.&lt;br /&gt;
resources :products do&lt;br /&gt;
  get :sold, :on =&amp;gt; :member&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Active Record==&lt;br /&gt;
Active Record, like all of the core Rails components, got a major overhaul. &lt;br /&gt;
&lt;br /&gt;
===Arel and the Query Interface===&lt;br /&gt;
The most prominent changes made that will be apparent to developers will be the integration with [https://github.com/nkallen/arel Arel]. With this integration, all of the core methods for Active Record return relations, allowing them to be chained together to develop complex queries. A full list and their functionality can be found in the [http://guides.rubyonrails.org/active_record_querying.html Rails Guide]. This will take the place of the options hash that would normally get passed in to one of the Active Record methods. To illustrate this, say we wanted to get a list of the first 10 products ordered alphabetically:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:order =&amp;gt; &amp;quot;name DESC&amp;quot;, :limit =&amp;gt; 10)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.order(&amp;quot;name DESC&amp;quot;).limit(10)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also, there are a lot of methods that are losing the options parameter in their definitions&amp;lt;ref&amp;gt;Naik 2010&amp;lt;/ref&amp;gt;, including query methods find and all as well as calculation methods count and average. Also, the &amp;lt;tt&amp;gt;:all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;:first&amp;lt;/tt&amp;gt; parameters for the find method are being deprecated in favor of the &amp;lt;tt&amp;gt;all&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;first&amp;lt;/tt&amp;gt; methods. Here are some examples of how things are changing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Changes to the find method.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.find(:all)&lt;br /&gt;
Product.find(:first)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.all&lt;br /&gt;
Product.first&lt;br /&gt;
&lt;br /&gt;
# Examples of method options being removed.&lt;br /&gt;
# Rails 2&lt;br /&gt;
Product.all(:joins =&amp;gt; :categories)&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
Product.joins(&amp;quot;categories&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Named Scopes===&lt;br /&gt;
The &amp;lt;tt&amp;gt;named_scope&amp;lt;/tt&amp;gt; definition has been changed to just &amp;lt;tt&amp;gt;scope&amp;lt;/tt&amp;gt;. The options hash is also being deprecated and should be replaced with the new methods provided by the Arel integration. Translating existing Rails 2 named scopes is easy enough:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Rails 2&lt;br /&gt;
class Product&lt;br /&gt;
  named_scope :food, :conditions =&amp;gt; { :type =&amp;gt; &amp;quot;food&amp;quot; }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# Rails 3&lt;br /&gt;
class Product&lt;br /&gt;
  scope :food, where(:type =&amp;gt; &amp;quot;food&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==JavaScript Support&amp;lt;ref&amp;gt;Gadbois 2010&amp;lt;/ref&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
There are many JavaScript libraries available to programmers to make their web applications more dynamic and smooth looking. Whenever you created a Rails 2 application, it would automatically install the Prototype and Script.aculo.us frameworks. Rails also provided helpers for these two JavaScript frameworks so that a few method calls were all that you needed to make your application more fluid.&lt;br /&gt;
&lt;br /&gt;
Rails 3 introduces the idea of Unobtrusive JavaScript (UJS). The JavaScript becomes unobtrusive by not being written in with the HTML objects producing cleaner code that is easier to debug. This is mostly done with the utilization of HTML 5 custom attributes such as data-method, data-confirm, data-remote and data-disable-with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;http://host.com&amp;quot; id=&amp;quot;create-post&amp;quot; method=&amp;quot;post&amp;quot; data-remote=&amp;quot;true&amp;quot; data-confirm=&amp;quot;Are you sure you want to submit?&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By doing this, it also frees Rails from being dependent on the Prototype framework, allowing it to become ''JavaScript framework agnostic''. With its current implementation, the framework can support Prototype, jQuery and MooTools.&lt;br /&gt;
&lt;br /&gt;
It might also be helpful to point out that as of Rails 3.1, the jQuery framework has replaced Prototype as the default JavaScript framework. Programmers that wish to still use Prototype must remember to specify this when they create the Rails application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rails new myapp -j prototype&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
==Citation Notes==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Full Reference Information==&lt;br /&gt;
&lt;br /&gt;
Gadbois, John. ''Using Unobtrusive JavaScript and AJAX with Rails 3'' http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Lindsaar, Mikel. &amp;quot;Ruby on Rails 3.0 Release Notes&amp;quot; http://edgeguides.rubyonrails.org/3_0_release_notes.html&lt;br /&gt;
&lt;br /&gt;
Naik, Pratik. &amp;quot;Active Record Query Interface 3.0&amp;quot; http://m.onkey.org/active-record-query-interface , 2010.&lt;br /&gt;
&lt;br /&gt;
Reza, Rizwan. &amp;quot;The Lowdown on Routes in Rails 3&amp;quot; http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ , 2010.&lt;br /&gt;
&lt;br /&gt;
Ruby, Sam, Dave Thomas and David Hansson. ''Agile Web Development with Rails: Third Edition'': The Pragmatic Programmers LLC, 2009.&lt;/div&gt;</summary>
		<author><name>Ttbrown4</name></author>
	</entry>
</feed>