<?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=Ssounda2</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=Ssounda2"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Ssounda2"/>
	<updated>2026-05-11T13:13:47Z</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/ch6_6b_ss&amp;diff=56221</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6b ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56221"/>
		<updated>2011-11-24T03:03:45Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Is Liskov Substitution Principle restrictive? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;''' Subclassing '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://msdn.microsoft.com/en-us/library/chsw0hdc.aspx]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is the mechanism by which one can achieve categorization and it it also facilitates polymorphism. Inheritance is used to build a hierarchy of concepts separated in categories at different levels of abstraction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to [http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html interfaces] and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===[http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/ Polymorphism]===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve [http://www.ibm.com/developerworks/java/tutorials/j-lessismore/section2.html code reuse] using [http://www.wi-inf.uni-duisburg-essen.de/MobisPortal/upload/JOOP00.pdf Delegation] and [ Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract](DBC). The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
[2] http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
[4] http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
[5] http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
[6] [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;br /&gt;
&lt;br /&gt;
[7] http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple&lt;br /&gt;
&lt;br /&gt;
[8] http://www.objectmentor.com/resources/articles/lsp.pdf&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56220</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6b ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56220"/>
		<updated>2011-11-24T02:59:31Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Is Liskov Substitution Principle restrictive? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;''' Subclassing '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://msdn.microsoft.com/en-us/library/chsw0hdc.aspx]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is the mechanism by which one can achieve categorization and it it also facilitates polymorphism. Inheritance is used to build a hierarchy of concepts separated in categories at different levels of abstraction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to [http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html interfaces] and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===[http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/ Polymorphism]===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve [http://www.ibm.com/developerworks/java/tutorials/j-lessismore/section2.html code reuse] using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract](DBC). The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
[2] http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
[4] http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
[5] http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
[6] [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;br /&gt;
&lt;br /&gt;
[7] http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple&lt;br /&gt;
&lt;br /&gt;
[8] http://www.objectmentor.com/resources/articles/lsp.pdf&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56219</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6b ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56219"/>
		<updated>2011-11-24T02:59:13Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Is Liskov Substitution Principle restrictive? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;''' Subclassing '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://msdn.microsoft.com/en-us/library/chsw0hdc.aspx]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is the mechanism by which one can achieve categorization and it it also facilitates polymorphism. Inheritance is used to build a hierarchy of concepts separated in categories at different levels of abstraction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to [http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html interfaces] and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===[http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/ Polymorphism]===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve [http://www.ibm.com/developerworks/java/tutorials/j-lessismore/section2.html code reuse] using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
[2] http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
[4] http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
[5] http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
[6] [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;br /&gt;
&lt;br /&gt;
[7] http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple&lt;br /&gt;
&lt;br /&gt;
[8] http://www.objectmentor.com/resources/articles/lsp.pdf&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56218</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6b ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56218"/>
		<updated>2011-11-24T02:56:52Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Public Interface Perspective/ Behavioral subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;''' Subclassing '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://msdn.microsoft.com/en-us/library/chsw0hdc.aspx]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is the mechanism by which one can achieve categorization and it it also facilitates polymorphism. Inheritance is used to build a hierarchy of concepts separated in categories at different levels of abstraction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to [http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html interfaces] and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===[http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/ Polymorphism]===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
[2] http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
[4] http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
[5] http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
[6] [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;br /&gt;
&lt;br /&gt;
[7] http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple&lt;br /&gt;
&lt;br /&gt;
[8] http://www.objectmentor.com/resources/articles/lsp.pdf&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56217</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6b ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56217"/>
		<updated>2011-11-24T02:55:46Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;''' Subclassing '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://msdn.microsoft.com/en-us/library/chsw0hdc.aspx]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is the mechanism by which one can achieve categorization and it it also facilitates polymorphism. Inheritance is used to build a hierarchy of concepts separated in categories at different levels of abstraction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===[http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/ Polymorphism]===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
[2] http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
[4] http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
[5] http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
[6] [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;br /&gt;
&lt;br /&gt;
[7] http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple&lt;br /&gt;
&lt;br /&gt;
[8] http://www.objectmentor.com/resources/articles/lsp.pdf&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56216</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6b ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56216"/>
		<updated>2011-11-24T02:55:31Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;''' Subclassing '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://msdn.microsoft.com/en-us/library/chsw0hdc.aspx]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[[ [2] /*References*/]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is the mechanism by which one can achieve categorization and it it also facilitates polymorphism. Inheritance is used to build a hierarchy of concepts separated in categories at different levels of abstraction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===[http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/ Polymorphism]===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
[2] http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
[4] http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
[5] http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
[6] [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;br /&gt;
&lt;br /&gt;
[7] http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple&lt;br /&gt;
&lt;br /&gt;
[8] http://www.objectmentor.com/resources/articles/lsp.pdf&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56215</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6b ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56215"/>
		<updated>2011-11-24T02:55:14Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;''' Subclassing '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://msdn.microsoft.com/en-us/library/chsw0hdc.aspx]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[[[2] /*References*/]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is the mechanism by which one can achieve categorization and it it also facilitates polymorphism. Inheritance is used to build a hierarchy of concepts separated in categories at different levels of abstraction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===[http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/ Polymorphism]===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
[2] http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
[4] http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
[5] http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
[6] [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;br /&gt;
&lt;br /&gt;
[7] http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple&lt;br /&gt;
&lt;br /&gt;
[8] http://www.objectmentor.com/resources/articles/lsp.pdf&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56214</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6b ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56214"/>
		<updated>2011-11-24T02:54:52Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;''' Subclassing '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://msdn.microsoft.com/en-us/library/chsw0hdc.aspx]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[[2 /*References*/]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is the mechanism by which one can achieve categorization and it it also facilitates polymorphism. Inheritance is used to build a hierarchy of concepts separated in categories at different levels of abstraction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===[http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/ Polymorphism]===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
[2] http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
[4] http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
[5] http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
[6] [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;br /&gt;
&lt;br /&gt;
[7] http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple&lt;br /&gt;
&lt;br /&gt;
[8] http://www.objectmentor.com/resources/articles/lsp.pdf&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56213</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6b ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56213"/>
		<updated>2011-11-24T02:54:02Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;''' Subclassing '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://msdn.microsoft.com/en-us/library/chsw0hdc.aspx]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is the mechanism by which one can achieve categorization and it it also facilitates polymorphism. Inheritance is used to build a hierarchy of concepts separated in categories at different levels of abstraction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===[http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/ Polymorphism]===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
[2] http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
[4] http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
[5] http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
[6] [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;br /&gt;
&lt;br /&gt;
[7] http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple&lt;br /&gt;
&lt;br /&gt;
[8] http://www.objectmentor.com/resources/articles/lsp.pdf&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56212</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6b ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56212"/>
		<updated>2011-11-24T02:53:19Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;''' Subclassing '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://msdn.microsoft.com/en-us/library/chsw0hdc.aspx]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is the mechanism by which one can achieve categorization and it it also facilitates polymorphism. Inheritance is used to build a hierarchy of concepts separated in categories at different levels of abstraction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===[http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/ Polymorphism]===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
* http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
* http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
* http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
* http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
* [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;br /&gt;
&lt;br /&gt;
* http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple&lt;br /&gt;
&lt;br /&gt;
* http://www.objectmentor.com/resources/articles/lsp.pdf&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56211</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6b ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56211"/>
		<updated>2011-11-24T02:49:08Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subclassing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;''' Subclassing '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://msdn.microsoft.com/en-us/library/chsw0hdc.aspx]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is the mechanism by which one can achieve categorization and it it also facilitates polymorphism. Inheritance is used to build a hierarchy of concepts separated in categories at different levels of abstraction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
* http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
* http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
* http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
* http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
* [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;br /&gt;
&lt;br /&gt;
* http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple&lt;br /&gt;
&lt;br /&gt;
* http://www.objectmentor.com/resources/articles/lsp.pdf&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56209</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6b ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=56209"/>
		<updated>2011-11-24T02:32:57Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;''' Subclassing '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is the mechanism by which one can achieve categorization and it it also facilitates polymorphism. Inheritance is used to build a hierarchy of concepts separated in categories at different levels of abstraction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
* http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
* http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
* http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
* http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
* [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;br /&gt;
&lt;br /&gt;
* http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple&lt;br /&gt;
&lt;br /&gt;
* http://www.objectmentor.com/resources/articles/lsp.pdf&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=55500</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6b ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=55500"/>
		<updated>2011-11-17T17:50:22Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Is Liskov Substitution Principle restrictive? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;font-size: 20px&amp;quot;&amp;gt;''' Subclassing '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
* http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
* http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
* http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
* http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
* [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;br /&gt;
&lt;br /&gt;
* http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple&lt;br /&gt;
&lt;br /&gt;
* http://www.objectmentor.com/resources/articles/lsp.pdf&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54952</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54952"/>
		<updated>2011-11-15T02:06:15Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=='''Subclassing'''==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
/* This talks about behavioral subtyping which has almost same constraints as LSP Dont think we can mention it here*/&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
   &lt;br /&gt;
      &lt;br /&gt;
/*lifted (ll modify it latr*/&lt;br /&gt;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. To my way of thinking, polymorphism is the source of most of the power of OO. I think there must be people who believe the power comes from the re-use that comes from subclassing. Polymorphism is what makes it possible for the clients of an interface (those dependent on a type) to not care which implementation is used at any point in the code. Subclassing makes it possible to re-use the methods provided by a superclass to provide different behavior.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
* http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
* http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
* http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
* http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
* [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54951</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54951"/>
		<updated>2011-11-15T02:05:15Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=='''Subclassing'''==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
/* This talks about behavioral subtyping which has almost same constraints as LSP Dont think we can mention it here*/&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
   &lt;br /&gt;
      &lt;br /&gt;
/*lifted (ll modify it latr*/&lt;br /&gt;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. To my way of thinking, polymorphism is the source of most of the power of OO. I think there must be people who believe the power comes from the re-use that comes from subclassing. Polymorphism is what makes it possible for the clients of an interface (those dependent on a type) to not care which implementation is used at any point in the code. Subclassing makes it possible to re-use the methods provided by a superclass to provide different behavior.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
* http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
* http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
* http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
* http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
* [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54950</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54950"/>
		<updated>2011-11-15T02:04:45Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
/* This talks about behavioral subtyping which has almost same constraints as LSP Dont think we can mention it here*/&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
   &lt;br /&gt;
      &lt;br /&gt;
/*lifted (ll modify it latr*/&lt;br /&gt;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. To my way of thinking, polymorphism is the source of most of the power of OO. I think there must be people who believe the power comes from the re-use that comes from subclassing. Polymorphism is what makes it possible for the clients of an interface (those dependent on a type) to not care which implementation is used at any point in the code. Subclassing makes it possible to re-use the methods provided by a superclass to provide different behavior.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
* http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
* http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
* http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
* http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
* [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54949</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54949"/>
		<updated>2011-11-15T02:03:20Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle(LSP)''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
/* This talks about behavioral subtyping which has almost same constraints as LSP Dont think we can mention it here*/&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
   &lt;br /&gt;
      &lt;br /&gt;
/*lifted (ll modify it latr*/&lt;br /&gt;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. To my way of thinking, polymorphism is the source of most of the power of OO. I think there must be people who believe the power comes from the re-use that comes from subclassing. Polymorphism is what makes it possible for the clients of an interface (those dependent on a type) to not care which implementation is used at any point in the code. Subclassing makes it possible to re-use the methods provided by a superclass to provide different behavior.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
[2] http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
[4] http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
[5] http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
[6] [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54948</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54948"/>
		<updated>2011-11-15T02:02:52Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Is-A Perspective */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;[http://en.wikipedia.org/wiki/Is-a is-a]&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
/* This talks about behavioral subtyping which has almost same constraints as LSP Dont think we can mention it here*/&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
   &lt;br /&gt;
      &lt;br /&gt;
/*lifted (ll modify it latr*/&lt;br /&gt;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. To my way of thinking, polymorphism is the source of most of the power of OO. I think there must be people who believe the power comes from the re-use that comes from subclassing. Polymorphism is what makes it possible for the clients of an interface (those dependent on a type) to not care which implementation is used at any point in the code. Subclassing makes it possible to re-use the methods provided by a superclass to provide different behavior.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
[2] http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
[4] http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
[5] http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
[6] [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54947</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54947"/>
		<updated>2011-11-15T02:01:39Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the '''Liskov Substitution Principle''' which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;is-a&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
/* This talks about behavioral subtyping which has almost same constraints as LSP Dont think we can mention it here*/&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
   &lt;br /&gt;
      &lt;br /&gt;
/*lifted (ll modify it latr*/&lt;br /&gt;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. To my way of thinking, polymorphism is the source of most of the power of OO. I think there must be people who believe the power comes from the re-use that comes from subclassing. Polymorphism is what makes it possible for the clients of an interface (those dependent on a type) to not care which implementation is used at any point in the code. Subclassing makes it possible to re-use the methods provided by a superclass to provide different behavior.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
[2] http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
[4] http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
[5] http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
[6] [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54946</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54946"/>
		<updated>2011-11-15T01:59:29Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle[6] which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;is-a&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
/* This talks about behavioral subtyping which has almost same constraints as LSP Dont think we can mention it here*/&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
   &lt;br /&gt;
      &lt;br /&gt;
/*lifted (ll modify it latr*/&lt;br /&gt;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. To my way of thinking, polymorphism is the source of most of the power of OO. I think there must be people who believe the power comes from the re-use that comes from subclassing. Polymorphism is what makes it possible for the clients of an interface (those dependent on a type) to not care which implementation is used at any point in the code. Subclassing makes it possible to re-use the methods provided by a superclass to provide different behavior.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
[2] http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
[4] http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
[5] http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
[6] [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54945</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54945"/>
		<updated>2011-11-15T01:58:32Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;is-a&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
/* This talks about behavioral subtyping which has almost same constraints as LSP Dont think we can mention it here*/&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
   &lt;br /&gt;
      &lt;br /&gt;
/*lifted (ll modify it latr*/&lt;br /&gt;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. To my way of thinking, polymorphism is the source of most of the power of OO. I think there must be people who believe the power comes from the re-use that comes from subclassing. Polymorphism is what makes it possible for the clients of an interface (those dependent on a type) to not care which implementation is used at any point in the code. Subclassing makes it possible to re-use the methods provided by a superclass to provide different behavior.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[1] http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
[2] http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
[3] http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
[4] http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
[5] http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
[6] [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54944</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54944"/>
		<updated>2011-11-15T01:57:16Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;is-a&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
/* This talks about behavioral subtyping which has almost same constraints as LSP Dont think we can mention it here*/&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
   &lt;br /&gt;
      &lt;br /&gt;
/*lifted (ll modify it latr*/&lt;br /&gt;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. To my way of thinking, polymorphism is the source of most of the power of OO. I think there must be people who believe the power comes from the re-use that comes from subclassing. Polymorphism is what makes it possible for the clients of an interface (those dependent on a type) to not care which implementation is used at any point in the code. Subclassing makes it possible to re-use the methods provided by a superclass to provide different behavior.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
* http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
* http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
* http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
* http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
* [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54943</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54943"/>
		<updated>2011-11-15T01:57:06Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;is-a&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
/* This talks about behavioral subtyping which has almost same constraints as LSP Dont think we can mention it here*/&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
   &lt;br /&gt;
      &lt;br /&gt;
/*lifted (ll modify it latr*/&lt;br /&gt;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. To my way of thinking, polymorphism is the source of most of the power of OO. I think there must be people who believe the power comes from the re-use that comes from subclassing. Polymorphism is what makes it possible for the clients of an interface (those dependent on a type) to not care which implementation is used at any point in the code. Subclassing makes it possible to re-use the methods provided by a superclass to provide different behavior.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* 1. http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
* http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
* http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
* http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
* http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
* [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54942</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54942"/>
		<updated>2011-11-15T01:56:48Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;is-a&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
/* This talks about behavioral subtyping which has almost same constraints as LSP Dont think we can mention it here*/&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
   &lt;br /&gt;
      &lt;br /&gt;
/*lifted (ll modify it latr*/&lt;br /&gt;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. To my way of thinking, polymorphism is the source of most of the power of OO. I think there must be people who believe the power comes from the re-use that comes from subclassing. Polymorphism is what makes it possible for the clients of an interface (those dependent on a type) to not care which implementation is used at any point in the code. Subclassing makes it possible to re-use the methods provided by a superclass to provide different behavior.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
* http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
* http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
* http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
* http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;br /&gt;
&lt;br /&gt;
* [http://dl.acm.org/citation.cfm?id=62141 Keynote address - data abstraction and hierarchy], Barbara Liskov, MIT Laboratory for Computer Science, Cambridge, Ma.&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54941</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54941"/>
		<updated>2011-11-15T01:52:50Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Why do we Subclass? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
* Code reuse&lt;br /&gt;
* Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
* Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;is-a&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
/* This talks about behavioral subtyping which has almost same constraints as LSP Dont think we can mention it here*/&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
   &lt;br /&gt;
      &lt;br /&gt;
/*lifted (ll modify it latr*/&lt;br /&gt;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. To my way of thinking, polymorphism is the source of most of the power of OO. I think there must be people who believe the power comes from the re-use that comes from subclassing. Polymorphism is what makes it possible for the clients of an interface (those dependent on a type) to not care which implementation is used at any point in the code. Subclassing makes it possible to re-use the methods provided by a superclass to provide different behavior.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54940</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54940"/>
		<updated>2011-11-15T01:51:31Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subclassing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webopedia.com/TERM/B/base_class.html base class](superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;is-a&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
/* This talks about behavioral subtyping which has almost same constraints as LSP Dont think we can mention it here*/&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
   &lt;br /&gt;
      &lt;br /&gt;
/*lifted (ll modify it latr*/&lt;br /&gt;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. To my way of thinking, polymorphism is the source of most of the power of OO. I think there must be people who believe the power comes from the re-use that comes from subclassing. Polymorphism is what makes it possible for the clients of an interface (those dependent on a type) to not care which implementation is used at any point in the code. Subclassing makes it possible to re-use the methods provided by a superclass to provide different behavior.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54939</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54939"/>
		<updated>2011-11-15T01:48:22Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Is Liskov Substitution Principle restrictive? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;is-a&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
/* This talks about behavioral subtyping which has almost same constraints as LSP Dont think we can mention it here*/&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract]. The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype. When using Design by Contract, subclasses in an inheritance hierarchy are allowed to weaken preconditions (but not strengthen them) and strengthen postconditions and invariants (but not weaken them). These rules approximate behavioral subtyping.&lt;br /&gt;
   &lt;br /&gt;
      &lt;br /&gt;
/*lifted (ll modify it latr*/&lt;br /&gt;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. To my way of thinking, polymorphism is the source of most of the power of OO. I think there must be people who believe the power comes from the re-use that comes from subclassing. Polymorphism is what makes it possible for the clients of an interface (those dependent on a type) to not care which implementation is used at any point in the code. Subclassing makes it possible to re-use the methods provided by a superclass to provide different behavior.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54938</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54938"/>
		<updated>2011-11-15T01:11:10Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Polymorphism */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;is-a&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
===Is Liskov Substitution Principle restrictive?===&lt;br /&gt;
&lt;br /&gt;
It depends,&lt;br /&gt;
If the developer in solely interested in reusing the code rather than keeping the behaviour or semantics of the methods intact, then he can do so by using sub-classing .But one can also achieve code reuse using [http://en.wikipedia.org/wiki/Delegation_(programming) Delegation] and [http://en.wikipedia.org/wiki/Composition_over_inheritance Composition] which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle while subclassing as it is one of the most powerful OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user about the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up of class hierarchies. Mess being that whenever a subclass instance was passed as parameter to any method, strange behavior would occur.&lt;br /&gt;
&lt;br /&gt;
But again, if the developer is solely interested in achieving code reuse and if he finds Liskov Substitution Principle to be too restrictive then he can achieve the same using alternative methodologies such as [http://en.wikipedia.org/wiki/Design_by_contract  Design By Contract].&lt;br /&gt;
   &lt;br /&gt;
      The idea of DBC is to specify that part of the behaviour which must remain unchanged (by means of assertions and suchlike). This leads to a more precise notion of subtype.&lt;br /&gt;
&lt;br /&gt;
/*lifted (ll modify it latr*/&lt;br /&gt;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. To my way of thinking, polymorphism is the source of most of the power of OO. I think there must be people who believe the power comes from the re-use that comes from subclassing. Polymorphism is what makes it possible for the clients of an interface (those dependent on a type) to not care which implementation is used at any point in the code. Subclassing makes it possible to re-use the methods provided by a superclass to provide different behavior.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54863</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54863"/>
		<updated>2011-11-13T03:07:01Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Public Interface Perspective/ Behavioral subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;is-a&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
  class Person{&lt;br /&gt;
   String name;&lt;br /&gt;
   ... &lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Student extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Employee extends Person{&lt;br /&gt;
   ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54862</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54862"/>
		<updated>2011-11-13T03:06:13Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Four perspectives of Inheritance */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
The four perspectives help in determining when to subclass&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;is-a&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
class Person{&lt;br /&gt;
 String name;&lt;br /&gt;
 ...&lt;br /&gt;
 ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Student extends Person{&lt;br /&gt;
 ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Employee extends Person{&lt;br /&gt;
 ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54861</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54861"/>
		<updated>2011-11-13T03:02:34Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Public Interface Perspective/ Behavioral subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;is-a&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
[http://cs.colby.edu/djskrien/ Dale Skrien], in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
class Person{&lt;br /&gt;
 String name;&lt;br /&gt;
 ...&lt;br /&gt;
 ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Student extends Person{&lt;br /&gt;
 ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Employee extends Person{&lt;br /&gt;
 ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance.&lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54860</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54860"/>
		<updated>2011-11-13T03:01:52Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
Code reuse is the practice of using the same segment of code in multiple applications.&lt;br /&gt;
&lt;br /&gt;
''''Is Code Reuse alone a sufficient reason to use Inheritance?''''&lt;br /&gt;
No It is not.&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
    String name;&lt;br /&gt;
    String manufacturer_name;&lt;br /&gt;
    int part_id;&lt;br /&gt;
    &lt;br /&gt;
    void setManufacturerName(String mname)&lt;br /&gt;
    {manufacterer_name=mname;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    int getPartID()&lt;br /&gt;
    {return part_id;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Medicine extends Automobile_Part&lt;br /&gt;
{&lt;br /&gt;
     String cures_disease;&lt;br /&gt;
     String getDiseaseName()&lt;br /&gt;
     {&lt;br /&gt;
        return cures_disease;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here the Medicine class extends the Automobile_Part class in order to reuse the  attributes &amp;quot;name&amp;quot; and &amp;quot;manufacturer_name&amp;quot; and &amp;quot;setManufacturerName&amp;quot; method. But it also inherits the &amp;quot;getPartID&amp;quot; method which is not appropriate for the Medicine class.&lt;br /&gt;
    &lt;br /&gt;
So Code Reuse is in itself not a sufficient reason for using inheritance.&lt;br /&gt;
&lt;br /&gt;
So if one wants to only achieve code reuse ,they can do so using Composition rather than Inheritance. &lt;br /&gt;
&lt;br /&gt;
''''Then what is Inheritance for?''''&lt;br /&gt;
&lt;br /&gt;
Inheritance is a mechanism used to facilitate polymorphism and to achieve categorization. By using inheritance one can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, you can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
Now let us consider both &amp;quot;is-a&amp;quot; relationship and Code reuse perspectives.&lt;br /&gt;
&lt;br /&gt;
''''Is the combination of code reuse and the “is-a” relationship among the classes a sufficient reason for using subclassing?''''&lt;br /&gt;
&lt;br /&gt;
Consider the following example.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   String getName()&lt;br /&gt;
   {&lt;br /&gt;
       return name;&lt;br /&gt;
   }&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though Penguin is a Bird and Penguin class re uses the code in Bird class ,it is not appropriate to use inheritance here because the behavior of the inherited code is being changed.&lt;br /&gt;
&lt;br /&gt;
Consider the case where a user invokes the method &amp;quot;altitude&amp;quot; in the Penguin class .The &amp;quot;altitude&amp;quot; method in Penguin class when invoked instead of displaying the altitude at which the bird flies it throws an exception thereby confusing the user.Code is not considered elegant if the user of that code is surprised or confused by the behavior of that code. &lt;br /&gt;
&lt;br /&gt;
This violates the Principle of Least Astonishment &lt;br /&gt;
&lt;br /&gt;
  If a client thinks he has a reference to an object of type A but actually has a reference to an object of subtype B,&lt;br /&gt;
  there should be no surprises when he sends messages to the object.&lt;br /&gt;
&lt;br /&gt;
So according to Principle of Least Astonishment whenever a sub class inherits functionality from the Super class.The subclass is not allowed to change the existing behavior by over ridding the inherited methods.&lt;br /&gt;
&lt;br /&gt;
This also violates the Liskov Substitution Principle because both the &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of Penguin class doesn't do everything that &amp;quot;fly&amp;quot; and &amp;quot;altitude&amp;quot; methods of its parent class Bird does.   &lt;br /&gt;
&lt;br /&gt;
Therefore the combination of code reuse and “is-a” relationship among the classes is not a sufficient reason for using subclassing.&lt;br /&gt;
===Public Interface Perspective/ Behavioral subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
Dale Skrien, in his [http://search.barnesandnoble.com/Object-Oriented-Design-Using-Java/Dale-Skrien/e/9780072974164?cm_mmc=borders-_-sku-_-NA-_-NA&amp;amp;redir=borders book], suggests the following guideline for subclassing,&lt;br /&gt;
&amp;quot;If a class B models a role played by class A, especially a temporary role,then  B should not be a subclass of A. Instead objects of class B should have references to objects of class A.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
An example why inheritance should not be applied when the public interfaces for concerned classes are same but the above guideline does not hold&lt;br /&gt;
&lt;br /&gt;
class Person{&lt;br /&gt;
 String name;&lt;br /&gt;
 ...&lt;br /&gt;
 ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Student extends Person{&lt;br /&gt;
 ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Employee extends Person{&lt;br /&gt;
 ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Here the student and employee classes inherit from Person. &amp;quot;Student&amp;quot; could be an &amp;quot;Employee&amp;quot;. Since both student and Employee classes extend Person, they have the data fields of Person duplicated. A reference to a Person object would be a better option than inheritance. &lt;br /&gt;
&lt;br /&gt;
===Polymorphism===&lt;br /&gt;
&lt;br /&gt;
If all occurences of class A could be substituted by class B then it's good to have class B subclass A to avoid code duplication and by this we follow the LSP principle.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Code_reuse&lt;br /&gt;
&lt;br /&gt;
http://www.isase.us/wisr3/7.pdf&lt;br /&gt;
&lt;br /&gt;
http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54830</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54830"/>
		<updated>2011-11-11T07:06:23Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Public Interface Perspective/ Behavioural subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioural subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called '''behavioral subtyping''', and A is a subtype of B. For behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54829</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54829"/>
		<updated>2011-11-11T07:05:49Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Public Interface Perspective/ Behavioural subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioural subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. An interface is considered a type. When A does everything B does and more, A can replace B wherever it is used. This is called behavioral subtyping, and A is a subtype of B. For '''Behavioral subtyping''', LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54828</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54828"/>
		<updated>2011-11-11T07:04:23Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
==Four perspectives of Inheritance==&lt;br /&gt;
===Code Reuse===&lt;br /&gt;
&lt;br /&gt;
===Is-A Perspective===&lt;br /&gt;
&lt;br /&gt;
===Public Interface Perspective/ Behavioural subtyping===&lt;br /&gt;
&lt;br /&gt;
Classes are programmed to interfaces and interfaces might have an overlap in functionality. One interface(subinterface) can extend another(superinterface).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  interface B{&lt;br /&gt;
      void common_method1(B b);&lt;br /&gt;
  }&lt;br /&gt;
  interface A extends B,C{ &lt;br /&gt;
      void common_method2(A a);&lt;br /&gt;
      void common_method3();&lt;br /&gt;
  }&lt;br /&gt;
  A a = …;&lt;br /&gt;
  A a1 = …;&lt;br /&gt;
  a.common_method1(a1); // Behavioral subtyping – Argument of type B is replaced by its &lt;br /&gt;
                                         // subtype A&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interface A includes methods from B and C and transitively above B and C. Interface is considered a type, when A does everything B does and more, A can replace B wherever it is used. This is called behavioral subtyping, and A is a subtype of B. For Behavioral subtyping, LSP has to be followed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54823</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54823"/>
		<updated>2011-11-11T00:56:00Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and with stronger constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54822</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54822"/>
		<updated>2011-11-11T00:45:17Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and fewer constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54821</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54821"/>
		<updated>2011-11-11T00:42:32Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and fewer constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
======&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54820</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54820"/>
		<updated>2011-11-11T00:07:30Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and fewer constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54819</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54819"/>
		<updated>2011-11-11T00:06:54Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and fewer constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
'''Subtype'''&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int product(B b) &lt;br /&gt;
    { &lt;br /&gt;
      return x * y * b.x * b.y;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54818</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54818"/>
		<updated>2011-11-11T00:02:25Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and fewer constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54817</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54817"/>
		<updated>2011-11-11T00:02:04Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and fewer constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
'''Subclass not Subtype'''&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54816</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54816"/>
		<updated>2011-11-11T00:01:38Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and fewer constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
Subclass not Subtype&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54815</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54815"/>
		<updated>2011-11-11T00:00:58Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and fewer constraints. This is not checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
Subclass not Subtype&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54814</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54814"/>
		<updated>2011-11-10T23:59:38Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;===Subclassing===&lt;br /&gt;
&lt;br /&gt;
Subclassing is a principle of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and &lt;br /&gt;
&lt;br /&gt;
instance data from the baseclass.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
Code reuse&lt;br /&gt;
Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' implementation.&lt;br /&gt;
&lt;br /&gt;
===Is Subclassing same as Subtyping?===&lt;br /&gt;
==Subtyping==&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is &lt;br /&gt;
&lt;br /&gt;
substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a subtype, the subclass must &lt;br /&gt;
&lt;br /&gt;
follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and fewer constraints. This is not &lt;br /&gt;
&lt;br /&gt;
checked by the compiler and we can create subclasses which are not subtypes. This is considered a bad approach, one of the reasons being, an argument to a &lt;br /&gt;
&lt;br /&gt;
method may be declared of one class A, but the method may be called with an argument of some subclass B. If we do not know if B is a true subtype of A, then &lt;br /&gt;
&lt;br /&gt;
we cannot assume that the behavior guaranteed by A is actually guaranteed by B, and so we cannot reason locally about this method[link mit reference].&lt;br /&gt;
&lt;br /&gt;
Subclass not Subtype&lt;br /&gt;
&lt;br /&gt;
  class A {&lt;br /&gt;
    int x;&lt;br /&gt;
    int get_x()&lt;br /&gt;
    { &lt;br /&gt;
      return x;&lt;br /&gt;
    }&lt;br /&gt;
    int sum(A a) { return x + a.x }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class B {&lt;br /&gt;
    int y;&lt;br /&gt;
&lt;br /&gt;
    int get_y()&lt;br /&gt;
    { &lt;br /&gt;
      return y;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    int sum(B b) &lt;br /&gt;
    { &lt;br /&gt;
      if(y &amp;gt; 0)&lt;br /&gt;
        return x + b.x + y + b.y;&lt;br /&gt;
      else&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54813</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54813"/>
		<updated>2011-11-10T23:42:35Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Why do we Subclass? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is the concept of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse &lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' &lt;br /&gt;
implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of   &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Include Example here */&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a   subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and fewer constraints. This is not checked by the compiler and we can create subclasses which are not subtypes.&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
Declaring one class to be a subclass of another class there by allowing a &lt;br /&gt;
&lt;br /&gt;
subclass to inherit the functionality from its super class is called &lt;br /&gt;
&lt;br /&gt;
subclassing.&lt;br /&gt;
&lt;br /&gt;
/* we can include here the 4 perspectives of  inheritance  and then mention &lt;br /&gt;
&lt;br /&gt;
LP principle , principle of least astonishment and &lt;br /&gt;
some other solid principles of inheritance like the The Open-Closed &lt;br /&gt;
&lt;br /&gt;
Principle and The Single Responsibility Principle.&lt;br /&gt;
  */ &lt;br /&gt;
Links for the solid principles...&lt;br /&gt;
http://www.objectmentor.com/resources/articles/ocp.pdf&lt;br /&gt;
http://www.objectmentor.com/resources/articles/srp.pdf&lt;br /&gt;
&lt;br /&gt;
===Subclassing in Java===&lt;br /&gt;
&lt;br /&gt;
====Extends Keyword====&lt;br /&gt;
The extends is a Java keyword, which is used in inheritance process of &lt;br /&gt;
&lt;br /&gt;
Java. It specifies the superclass in a class declaration using extends &lt;br /&gt;
&lt;br /&gt;
keyword.To inherit a class, you simply incorporate the definition of one &lt;br /&gt;
&lt;br /&gt;
class into another by using the extends keyword. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class A&lt;br /&gt;
{ &lt;br /&gt;
   int a;&lt;br /&gt;
   char b;&lt;br /&gt;
   void method1()&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
class B extends A    //class B inherits the properties of class A&lt;br /&gt;
{&lt;br /&gt;
   void method2()&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===Abstract classes===&lt;br /&gt;
&lt;br /&gt;
Abstract classes in Java are used to declare common characteristics of &lt;br /&gt;
&lt;br /&gt;
subclasses. An abstract class cannot be instantiated i.e an object of an &lt;br /&gt;
&lt;br /&gt;
abstract class cannot be created. It can only be used as a superclass for &lt;br /&gt;
&lt;br /&gt;
other classes that extend the abstract class. Abstract classes are declared &lt;br /&gt;
&lt;br /&gt;
using the abstract keyword. Abstract classes are used to provide a template &lt;br /&gt;
&lt;br /&gt;
or design for concrete subclasses down the inheritance tree.&lt;br /&gt;
&lt;br /&gt;
An abstract class can contain fields that describe the characteristics and &lt;br /&gt;
&lt;br /&gt;
methods that describe the actions that a class can perform. An abstract &lt;br /&gt;
&lt;br /&gt;
class can include methods that contain no implementation. These are called &lt;br /&gt;
&lt;br /&gt;
abstract methods.If a class has any abstract methods, whether declared or &lt;br /&gt;
&lt;br /&gt;
inherited, the entire class must be declared abstract. Abstract methods are &lt;br /&gt;
&lt;br /&gt;
used to provide a template for the classes that inherit the abstract &lt;br /&gt;
&lt;br /&gt;
methods.&lt;br /&gt;
&lt;br /&gt;
Abstract classes cannot be instantiated; they must be subclassed, and &lt;br /&gt;
&lt;br /&gt;
actual implementations must be provided for the abstract methods. Any &lt;br /&gt;
&lt;br /&gt;
implementation specified can, of course, be overridden by additional sub-&lt;br /&gt;
&lt;br /&gt;
classes. An object must have an implementation for all of its methods. You &lt;br /&gt;
&lt;br /&gt;
need to create a subclass that provides an implementation for the abstract &lt;br /&gt;
&lt;br /&gt;
method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
abstract class A&lt;br /&gt;
{&lt;br /&gt;
  void method1()&lt;br /&gt;
  {&lt;br /&gt;
    ...&lt;br /&gt;
    ...&lt;br /&gt;
    ...&lt;br /&gt;
  }&lt;br /&gt;
  abstract void method2();&lt;br /&gt;
}&lt;br /&gt;
class B extends A&lt;br /&gt;
{&lt;br /&gt;
    void method2()&lt;br /&gt;
    {&lt;br /&gt;
        ...&lt;br /&gt;
        ...&lt;br /&gt;
    } &lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the class B does not implement the method &amp;quot;method2&amp;quot; then even class B &lt;br /&gt;
&lt;br /&gt;
has to be declared as an Abstract class.&lt;br /&gt;
&lt;br /&gt;
Rules that should be followed before subclassing&lt;br /&gt;
&lt;br /&gt;
===Subclassing vs Subtyping===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The Liskov Substitution Principle in class typed languages===&lt;br /&gt;
The Liskov Substitution Principle is a way of ensuring that inheritance is &lt;br /&gt;
&lt;br /&gt;
used correctly.&lt;br /&gt;
&lt;br /&gt;
 It states that, in a computer program, if S is a subtype of T, then &lt;br /&gt;
&lt;br /&gt;
objects of type T may be replaced with objects of type S (i.e., objects of &lt;br /&gt;
&lt;br /&gt;
type S may be substitutes for objects of type T) without altering any of &lt;br /&gt;
&lt;br /&gt;
the desirable properties of that program (correctness, task performed, &lt;br /&gt;
&lt;br /&gt;
etc.). &lt;br /&gt;
&lt;br /&gt;
If for each object o1 of type S there is an object o2 of type T such that &lt;br /&gt;
&lt;br /&gt;
for all programs P defined in terms of T, the behavior of P is unchanged &lt;br /&gt;
&lt;br /&gt;
when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In less formal terms, it says that if a client (program) expects objects of &lt;br /&gt;
&lt;br /&gt;
one type to behave in a certain way, then it’s only okay to substitute &lt;br /&gt;
&lt;br /&gt;
objects of another type if the same expectations are satisfied.&lt;br /&gt;
&lt;br /&gt;
Consider the following example which satisfies the Liskov Substitution &lt;br /&gt;
&lt;br /&gt;
Principle&lt;br /&gt;
&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Sparrow extends Birds&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
}  &lt;br /&gt;
&lt;br /&gt;
Example which does not satisfy the Liskov Substitution Principle&lt;br /&gt;
&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
If an override method does nothing or just throws an exception, then you’re &lt;br /&gt;
&lt;br /&gt;
probably violating the LSP.&lt;br /&gt;
Therefore,this does not satisfy the Liskov Substitution Principle though &lt;br /&gt;
&lt;br /&gt;
penguin &amp;quot;is-a&amp;quot; bird.&lt;br /&gt;
&lt;br /&gt;
/* This is a direct lift off so we need to paraphrase it*/&lt;br /&gt;
&lt;br /&gt;
The Liskov Substitution Principle in duck typed languages&lt;br /&gt;
In programming languages with duck typing (e.g. Ruby, Python, Smalltalk) &lt;br /&gt;
&lt;br /&gt;
classes don’t really define types. There is no type checking when assigning &lt;br /&gt;
&lt;br /&gt;
objects to variables or passing objects as method arguments. There is a &lt;br /&gt;
&lt;br /&gt;
kind of type checking when a method is called. It is checked that the &lt;br /&gt;
&lt;br /&gt;
method exists with a matching number of parameters.&lt;br /&gt;
Since classes don’t define types, inheritance in duck typed languages has &lt;br /&gt;
&lt;br /&gt;
nothing to do with the subtype relation or the LSP.&lt;br /&gt;
In a way clients define interfaces in an implicit way. Let’s look at an &lt;br /&gt;
&lt;br /&gt;
example:&lt;br /&gt;
def my_method1(a) do&lt;br /&gt;
    a.m1()&lt;br /&gt;
    a.m2()&lt;br /&gt;
    my_method2(a)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def my_method2(a) do&lt;br /&gt;
  a.m3()&lt;br /&gt;
end&lt;br /&gt;
Calling my_method1 with an object a will succeed if the object a provides &lt;br /&gt;
&lt;br /&gt;
the three methods m1, m2 and m3. This is the implicit interface the object &lt;br /&gt;
&lt;br /&gt;
a needs to implement. And with this implicit interface comes the clients &lt;br /&gt;
&lt;br /&gt;
expectation about the behaviour of m1, m2 and m3. That’s just the same as &lt;br /&gt;
&lt;br /&gt;
with the Java interface. If an object b provides m1, m2 and m3 but doesn’t &lt;br /&gt;
&lt;br /&gt;
fit the expected behaviour, the LSP is violated.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The LSP isn’t tied to inheritance or class based typing. It applies to duck &lt;br /&gt;
&lt;br /&gt;
types languages as well as to systems without inheritance. LSP is a concept &lt;br /&gt;
&lt;br /&gt;
that applies to all kinds of polymorphism. Only if you don’t use &lt;br /&gt;
&lt;br /&gt;
polymorphism of all you don’t need to care about the LSP.&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54812</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54812"/>
		<updated>2011-11-10T23:42:00Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Why do we Subclass? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is the concept of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse \n&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' &lt;br /&gt;
implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of   &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Include Example here */&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a   subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and fewer constraints. This is not checked by the compiler and we can create subclasses which are not subtypes.&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
Declaring one class to be a subclass of another class there by allowing a &lt;br /&gt;
&lt;br /&gt;
subclass to inherit the functionality from its super class is called &lt;br /&gt;
&lt;br /&gt;
subclassing.&lt;br /&gt;
&lt;br /&gt;
/* we can include here the 4 perspectives of  inheritance  and then mention &lt;br /&gt;
&lt;br /&gt;
LP principle , principle of least astonishment and &lt;br /&gt;
some other solid principles of inheritance like the The Open-Closed &lt;br /&gt;
&lt;br /&gt;
Principle and The Single Responsibility Principle.&lt;br /&gt;
  */ &lt;br /&gt;
Links for the solid principles...&lt;br /&gt;
http://www.objectmentor.com/resources/articles/ocp.pdf&lt;br /&gt;
http://www.objectmentor.com/resources/articles/srp.pdf&lt;br /&gt;
&lt;br /&gt;
===Subclassing in Java===&lt;br /&gt;
&lt;br /&gt;
====Extends Keyword====&lt;br /&gt;
The extends is a Java keyword, which is used in inheritance process of &lt;br /&gt;
&lt;br /&gt;
Java. It specifies the superclass in a class declaration using extends &lt;br /&gt;
&lt;br /&gt;
keyword.To inherit a class, you simply incorporate the definition of one &lt;br /&gt;
&lt;br /&gt;
class into another by using the extends keyword. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class A&lt;br /&gt;
{ &lt;br /&gt;
   int a;&lt;br /&gt;
   char b;&lt;br /&gt;
   void method1()&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
class B extends A    //class B inherits the properties of class A&lt;br /&gt;
{&lt;br /&gt;
   void method2()&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===Abstract classes===&lt;br /&gt;
&lt;br /&gt;
Abstract classes in Java are used to declare common characteristics of &lt;br /&gt;
&lt;br /&gt;
subclasses. An abstract class cannot be instantiated i.e an object of an &lt;br /&gt;
&lt;br /&gt;
abstract class cannot be created. It can only be used as a superclass for &lt;br /&gt;
&lt;br /&gt;
other classes that extend the abstract class. Abstract classes are declared &lt;br /&gt;
&lt;br /&gt;
using the abstract keyword. Abstract classes are used to provide a template &lt;br /&gt;
&lt;br /&gt;
or design for concrete subclasses down the inheritance tree.&lt;br /&gt;
&lt;br /&gt;
An abstract class can contain fields that describe the characteristics and &lt;br /&gt;
&lt;br /&gt;
methods that describe the actions that a class can perform. An abstract &lt;br /&gt;
&lt;br /&gt;
class can include methods that contain no implementation. These are called &lt;br /&gt;
&lt;br /&gt;
abstract methods.If a class has any abstract methods, whether declared or &lt;br /&gt;
&lt;br /&gt;
inherited, the entire class must be declared abstract. Abstract methods are &lt;br /&gt;
&lt;br /&gt;
used to provide a template for the classes that inherit the abstract &lt;br /&gt;
&lt;br /&gt;
methods.&lt;br /&gt;
&lt;br /&gt;
Abstract classes cannot be instantiated; they must be subclassed, and &lt;br /&gt;
&lt;br /&gt;
actual implementations must be provided for the abstract methods. Any &lt;br /&gt;
&lt;br /&gt;
implementation specified can, of course, be overridden by additional sub-&lt;br /&gt;
&lt;br /&gt;
classes. An object must have an implementation for all of its methods. You &lt;br /&gt;
&lt;br /&gt;
need to create a subclass that provides an implementation for the abstract &lt;br /&gt;
&lt;br /&gt;
method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
abstract class A&lt;br /&gt;
{&lt;br /&gt;
  void method1()&lt;br /&gt;
  {&lt;br /&gt;
    ...&lt;br /&gt;
    ...&lt;br /&gt;
    ...&lt;br /&gt;
  }&lt;br /&gt;
  abstract void method2();&lt;br /&gt;
}&lt;br /&gt;
class B extends A&lt;br /&gt;
{&lt;br /&gt;
    void method2()&lt;br /&gt;
    {&lt;br /&gt;
        ...&lt;br /&gt;
        ...&lt;br /&gt;
    } &lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the class B does not implement the method &amp;quot;method2&amp;quot; then even class B &lt;br /&gt;
&lt;br /&gt;
has to be declared as an Abstract class.&lt;br /&gt;
&lt;br /&gt;
Rules that should be followed before subclassing&lt;br /&gt;
&lt;br /&gt;
===Subclassing vs Subtyping===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The Liskov Substitution Principle in class typed languages===&lt;br /&gt;
The Liskov Substitution Principle is a way of ensuring that inheritance is &lt;br /&gt;
&lt;br /&gt;
used correctly.&lt;br /&gt;
&lt;br /&gt;
 It states that, in a computer program, if S is a subtype of T, then &lt;br /&gt;
&lt;br /&gt;
objects of type T may be replaced with objects of type S (i.e., objects of &lt;br /&gt;
&lt;br /&gt;
type S may be substitutes for objects of type T) without altering any of &lt;br /&gt;
&lt;br /&gt;
the desirable properties of that program (correctness, task performed, &lt;br /&gt;
&lt;br /&gt;
etc.). &lt;br /&gt;
&lt;br /&gt;
If for each object o1 of type S there is an object o2 of type T such that &lt;br /&gt;
&lt;br /&gt;
for all programs P defined in terms of T, the behavior of P is unchanged &lt;br /&gt;
&lt;br /&gt;
when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In less formal terms, it says that if a client (program) expects objects of &lt;br /&gt;
&lt;br /&gt;
one type to behave in a certain way, then it’s only okay to substitute &lt;br /&gt;
&lt;br /&gt;
objects of another type if the same expectations are satisfied.&lt;br /&gt;
&lt;br /&gt;
Consider the following example which satisfies the Liskov Substitution &lt;br /&gt;
&lt;br /&gt;
Principle&lt;br /&gt;
&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Sparrow extends Birds&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
}  &lt;br /&gt;
&lt;br /&gt;
Example which does not satisfy the Liskov Substitution Principle&lt;br /&gt;
&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
If an override method does nothing or just throws an exception, then you’re &lt;br /&gt;
&lt;br /&gt;
probably violating the LSP.&lt;br /&gt;
Therefore,this does not satisfy the Liskov Substitution Principle though &lt;br /&gt;
&lt;br /&gt;
penguin &amp;quot;is-a&amp;quot; bird.&lt;br /&gt;
&lt;br /&gt;
/* This is a direct lift off so we need to paraphrase it*/&lt;br /&gt;
&lt;br /&gt;
The Liskov Substitution Principle in duck typed languages&lt;br /&gt;
In programming languages with duck typing (e.g. Ruby, Python, Smalltalk) &lt;br /&gt;
&lt;br /&gt;
classes don’t really define types. There is no type checking when assigning &lt;br /&gt;
&lt;br /&gt;
objects to variables or passing objects as method arguments. There is a &lt;br /&gt;
&lt;br /&gt;
kind of type checking when a method is called. It is checked that the &lt;br /&gt;
&lt;br /&gt;
method exists with a matching number of parameters.&lt;br /&gt;
Since classes don’t define types, inheritance in duck typed languages has &lt;br /&gt;
&lt;br /&gt;
nothing to do with the subtype relation or the LSP.&lt;br /&gt;
In a way clients define interfaces in an implicit way. Let’s look at an &lt;br /&gt;
&lt;br /&gt;
example:&lt;br /&gt;
def my_method1(a) do&lt;br /&gt;
    a.m1()&lt;br /&gt;
    a.m2()&lt;br /&gt;
    my_method2(a)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def my_method2(a) do&lt;br /&gt;
  a.m3()&lt;br /&gt;
end&lt;br /&gt;
Calling my_method1 with an object a will succeed if the object a provides &lt;br /&gt;
&lt;br /&gt;
the three methods m1, m2 and m3. This is the implicit interface the object &lt;br /&gt;
&lt;br /&gt;
a needs to implement. And with this implicit interface comes the clients &lt;br /&gt;
&lt;br /&gt;
expectation about the behaviour of m1, m2 and m3. That’s just the same as &lt;br /&gt;
&lt;br /&gt;
with the Java interface. If an object b provides m1, m2 and m3 but doesn’t &lt;br /&gt;
&lt;br /&gt;
fit the expected behaviour, the LSP is violated.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The LSP isn’t tied to inheritance or class based typing. It applies to duck &lt;br /&gt;
&lt;br /&gt;
types languages as well as to systems without inheritance. LSP is a concept &lt;br /&gt;
&lt;br /&gt;
that applies to all kinds of polymorphism. Only if you don’t use &lt;br /&gt;
&lt;br /&gt;
polymorphism of all you don’t need to care about the LSP.&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54811</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54811"/>
		<updated>2011-11-10T23:41:16Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subtyping */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is the concept of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' &lt;br /&gt;
implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of   &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Include Example here */&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a   subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and fewer constraints. This is not checked by the compiler and we can create subclasses which are not subtypes.&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
Declaring one class to be a subclass of another class there by allowing a &lt;br /&gt;
&lt;br /&gt;
subclass to inherit the functionality from its super class is called &lt;br /&gt;
&lt;br /&gt;
subclassing.&lt;br /&gt;
&lt;br /&gt;
/* we can include here the 4 perspectives of  inheritance  and then mention &lt;br /&gt;
&lt;br /&gt;
LP principle , principle of least astonishment and &lt;br /&gt;
some other solid principles of inheritance like the The Open-Closed &lt;br /&gt;
&lt;br /&gt;
Principle and The Single Responsibility Principle.&lt;br /&gt;
  */ &lt;br /&gt;
Links for the solid principles...&lt;br /&gt;
http://www.objectmentor.com/resources/articles/ocp.pdf&lt;br /&gt;
http://www.objectmentor.com/resources/articles/srp.pdf&lt;br /&gt;
&lt;br /&gt;
===Subclassing in Java===&lt;br /&gt;
&lt;br /&gt;
====Extends Keyword====&lt;br /&gt;
The extends is a Java keyword, which is used in inheritance process of &lt;br /&gt;
&lt;br /&gt;
Java. It specifies the superclass in a class declaration using extends &lt;br /&gt;
&lt;br /&gt;
keyword.To inherit a class, you simply incorporate the definition of one &lt;br /&gt;
&lt;br /&gt;
class into another by using the extends keyword. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class A&lt;br /&gt;
{ &lt;br /&gt;
   int a;&lt;br /&gt;
   char b;&lt;br /&gt;
   void method1()&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
class B extends A    //class B inherits the properties of class A&lt;br /&gt;
{&lt;br /&gt;
   void method2()&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===Abstract classes===&lt;br /&gt;
&lt;br /&gt;
Abstract classes in Java are used to declare common characteristics of &lt;br /&gt;
&lt;br /&gt;
subclasses. An abstract class cannot be instantiated i.e an object of an &lt;br /&gt;
&lt;br /&gt;
abstract class cannot be created. It can only be used as a superclass for &lt;br /&gt;
&lt;br /&gt;
other classes that extend the abstract class. Abstract classes are declared &lt;br /&gt;
&lt;br /&gt;
using the abstract keyword. Abstract classes are used to provide a template &lt;br /&gt;
&lt;br /&gt;
or design for concrete subclasses down the inheritance tree.&lt;br /&gt;
&lt;br /&gt;
An abstract class can contain fields that describe the characteristics and &lt;br /&gt;
&lt;br /&gt;
methods that describe the actions that a class can perform. An abstract &lt;br /&gt;
&lt;br /&gt;
class can include methods that contain no implementation. These are called &lt;br /&gt;
&lt;br /&gt;
abstract methods.If a class has any abstract methods, whether declared or &lt;br /&gt;
&lt;br /&gt;
inherited, the entire class must be declared abstract. Abstract methods are &lt;br /&gt;
&lt;br /&gt;
used to provide a template for the classes that inherit the abstract &lt;br /&gt;
&lt;br /&gt;
methods.&lt;br /&gt;
&lt;br /&gt;
Abstract classes cannot be instantiated; they must be subclassed, and &lt;br /&gt;
&lt;br /&gt;
actual implementations must be provided for the abstract methods. Any &lt;br /&gt;
&lt;br /&gt;
implementation specified can, of course, be overridden by additional sub-&lt;br /&gt;
&lt;br /&gt;
classes. An object must have an implementation for all of its methods. You &lt;br /&gt;
&lt;br /&gt;
need to create a subclass that provides an implementation for the abstract &lt;br /&gt;
&lt;br /&gt;
method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
abstract class A&lt;br /&gt;
{&lt;br /&gt;
  void method1()&lt;br /&gt;
  {&lt;br /&gt;
    ...&lt;br /&gt;
    ...&lt;br /&gt;
    ...&lt;br /&gt;
  }&lt;br /&gt;
  abstract void method2();&lt;br /&gt;
}&lt;br /&gt;
class B extends A&lt;br /&gt;
{&lt;br /&gt;
    void method2()&lt;br /&gt;
    {&lt;br /&gt;
        ...&lt;br /&gt;
        ...&lt;br /&gt;
    } &lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the class B does not implement the method &amp;quot;method2&amp;quot; then even class B &lt;br /&gt;
&lt;br /&gt;
has to be declared as an Abstract class.&lt;br /&gt;
&lt;br /&gt;
Rules that should be followed before subclassing&lt;br /&gt;
&lt;br /&gt;
===Subclassing vs Subtyping===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The Liskov Substitution Principle in class typed languages===&lt;br /&gt;
The Liskov Substitution Principle is a way of ensuring that inheritance is &lt;br /&gt;
&lt;br /&gt;
used correctly.&lt;br /&gt;
&lt;br /&gt;
 It states that, in a computer program, if S is a subtype of T, then &lt;br /&gt;
&lt;br /&gt;
objects of type T may be replaced with objects of type S (i.e., objects of &lt;br /&gt;
&lt;br /&gt;
type S may be substitutes for objects of type T) without altering any of &lt;br /&gt;
&lt;br /&gt;
the desirable properties of that program (correctness, task performed, &lt;br /&gt;
&lt;br /&gt;
etc.). &lt;br /&gt;
&lt;br /&gt;
If for each object o1 of type S there is an object o2 of type T such that &lt;br /&gt;
&lt;br /&gt;
for all programs P defined in terms of T, the behavior of P is unchanged &lt;br /&gt;
&lt;br /&gt;
when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In less formal terms, it says that if a client (program) expects objects of &lt;br /&gt;
&lt;br /&gt;
one type to behave in a certain way, then it’s only okay to substitute &lt;br /&gt;
&lt;br /&gt;
objects of another type if the same expectations are satisfied.&lt;br /&gt;
&lt;br /&gt;
Consider the following example which satisfies the Liskov Substitution &lt;br /&gt;
&lt;br /&gt;
Principle&lt;br /&gt;
&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Sparrow extends Birds&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
}  &lt;br /&gt;
&lt;br /&gt;
Example which does not satisfy the Liskov Substitution Principle&lt;br /&gt;
&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
If an override method does nothing or just throws an exception, then you’re &lt;br /&gt;
&lt;br /&gt;
probably violating the LSP.&lt;br /&gt;
Therefore,this does not satisfy the Liskov Substitution Principle though &lt;br /&gt;
&lt;br /&gt;
penguin &amp;quot;is-a&amp;quot; bird.&lt;br /&gt;
&lt;br /&gt;
/* This is a direct lift off so we need to paraphrase it*/&lt;br /&gt;
&lt;br /&gt;
The Liskov Substitution Principle in duck typed languages&lt;br /&gt;
In programming languages with duck typing (e.g. Ruby, Python, Smalltalk) &lt;br /&gt;
&lt;br /&gt;
classes don’t really define types. There is no type checking when assigning &lt;br /&gt;
&lt;br /&gt;
objects to variables or passing objects as method arguments. There is a &lt;br /&gt;
&lt;br /&gt;
kind of type checking when a method is called. It is checked that the &lt;br /&gt;
&lt;br /&gt;
method exists with a matching number of parameters.&lt;br /&gt;
Since classes don’t define types, inheritance in duck typed languages has &lt;br /&gt;
&lt;br /&gt;
nothing to do with the subtype relation or the LSP.&lt;br /&gt;
In a way clients define interfaces in an implicit way. Let’s look at an &lt;br /&gt;
&lt;br /&gt;
example:&lt;br /&gt;
def my_method1(a) do&lt;br /&gt;
    a.m1()&lt;br /&gt;
    a.m2()&lt;br /&gt;
    my_method2(a)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def my_method2(a) do&lt;br /&gt;
  a.m3()&lt;br /&gt;
end&lt;br /&gt;
Calling my_method1 with an object a will succeed if the object a provides &lt;br /&gt;
&lt;br /&gt;
the three methods m1, m2 and m3. This is the implicit interface the object &lt;br /&gt;
&lt;br /&gt;
a needs to implement. And with this implicit interface comes the clients &lt;br /&gt;
&lt;br /&gt;
expectation about the behaviour of m1, m2 and m3. That’s just the same as &lt;br /&gt;
&lt;br /&gt;
with the Java interface. If an object b provides m1, m2 and m3 but doesn’t &lt;br /&gt;
&lt;br /&gt;
fit the expected behaviour, the LSP is violated.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The LSP isn’t tied to inheritance or class based typing. It applies to duck &lt;br /&gt;
&lt;br /&gt;
types languages as well as to systems without inheritance. LSP is a concept &lt;br /&gt;
&lt;br /&gt;
that applies to all kinds of polymorphism. Only if you don’t use &lt;br /&gt;
&lt;br /&gt;
polymorphism of all you don’t need to care about the LSP.&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54810</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54810"/>
		<updated>2011-11-10T23:36:57Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is the concept of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' &lt;br /&gt;
implementation.&lt;br /&gt;
&lt;br /&gt;
==Is Subclassing same as Subtyping?==&lt;br /&gt;
&lt;br /&gt;
===Subtyping===&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of   &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a   subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and fewer constraints. This is not checked by the compiler and we can create subclasses which are not subtypes.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
Declaring one class to be a subclass of another class there by allowing a &lt;br /&gt;
&lt;br /&gt;
subclass to inherit the functionality from its super class is called &lt;br /&gt;
&lt;br /&gt;
subclassing.&lt;br /&gt;
&lt;br /&gt;
/* we can include here the 4 perspectives of  inheritance  and then mention &lt;br /&gt;
&lt;br /&gt;
LP principle , principle of least astonishment and &lt;br /&gt;
some other solid principles of inheritance like the The Open-Closed &lt;br /&gt;
&lt;br /&gt;
Principle and The Single Responsibility Principle.&lt;br /&gt;
  */ &lt;br /&gt;
Links for the solid principles...&lt;br /&gt;
http://www.objectmentor.com/resources/articles/ocp.pdf&lt;br /&gt;
http://www.objectmentor.com/resources/articles/srp.pdf&lt;br /&gt;
&lt;br /&gt;
===Subclassing in Java===&lt;br /&gt;
&lt;br /&gt;
====Extends Keyword====&lt;br /&gt;
The extends is a Java keyword, which is used in inheritance process of &lt;br /&gt;
&lt;br /&gt;
Java. It specifies the superclass in a class declaration using extends &lt;br /&gt;
&lt;br /&gt;
keyword.To inherit a class, you simply incorporate the definition of one &lt;br /&gt;
&lt;br /&gt;
class into another by using the extends keyword. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class A&lt;br /&gt;
{ &lt;br /&gt;
   int a;&lt;br /&gt;
   char b;&lt;br /&gt;
   void method1()&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
class B extends A    //class B inherits the properties of class A&lt;br /&gt;
{&lt;br /&gt;
   void method2()&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===Abstract classes===&lt;br /&gt;
&lt;br /&gt;
Abstract classes in Java are used to declare common characteristics of &lt;br /&gt;
&lt;br /&gt;
subclasses. An abstract class cannot be instantiated i.e an object of an &lt;br /&gt;
&lt;br /&gt;
abstract class cannot be created. It can only be used as a superclass for &lt;br /&gt;
&lt;br /&gt;
other classes that extend the abstract class. Abstract classes are declared &lt;br /&gt;
&lt;br /&gt;
using the abstract keyword. Abstract classes are used to provide a template &lt;br /&gt;
&lt;br /&gt;
or design for concrete subclasses down the inheritance tree.&lt;br /&gt;
&lt;br /&gt;
An abstract class can contain fields that describe the characteristics and &lt;br /&gt;
&lt;br /&gt;
methods that describe the actions that a class can perform. An abstract &lt;br /&gt;
&lt;br /&gt;
class can include methods that contain no implementation. These are called &lt;br /&gt;
&lt;br /&gt;
abstract methods.If a class has any abstract methods, whether declared or &lt;br /&gt;
&lt;br /&gt;
inherited, the entire class must be declared abstract. Abstract methods are &lt;br /&gt;
&lt;br /&gt;
used to provide a template for the classes that inherit the abstract &lt;br /&gt;
&lt;br /&gt;
methods.&lt;br /&gt;
&lt;br /&gt;
Abstract classes cannot be instantiated; they must be subclassed, and &lt;br /&gt;
&lt;br /&gt;
actual implementations must be provided for the abstract methods. Any &lt;br /&gt;
&lt;br /&gt;
implementation specified can, of course, be overridden by additional sub-&lt;br /&gt;
&lt;br /&gt;
classes. An object must have an implementation for all of its methods. You &lt;br /&gt;
&lt;br /&gt;
need to create a subclass that provides an implementation for the abstract &lt;br /&gt;
&lt;br /&gt;
method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
abstract class A&lt;br /&gt;
{&lt;br /&gt;
  void method1()&lt;br /&gt;
  {&lt;br /&gt;
    ...&lt;br /&gt;
    ...&lt;br /&gt;
    ...&lt;br /&gt;
  }&lt;br /&gt;
  abstract void method2();&lt;br /&gt;
}&lt;br /&gt;
class B extends A&lt;br /&gt;
{&lt;br /&gt;
    void method2()&lt;br /&gt;
    {&lt;br /&gt;
        ...&lt;br /&gt;
        ...&lt;br /&gt;
    } &lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the class B does not implement the method &amp;quot;method2&amp;quot; then even class B &lt;br /&gt;
&lt;br /&gt;
has to be declared as an Abstract class.&lt;br /&gt;
&lt;br /&gt;
Rules that should be followed before subclassing&lt;br /&gt;
&lt;br /&gt;
===Subclassing vs Subtyping===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The Liskov Substitution Principle in class typed languages===&lt;br /&gt;
The Liskov Substitution Principle is a way of ensuring that inheritance is &lt;br /&gt;
&lt;br /&gt;
used correctly.&lt;br /&gt;
&lt;br /&gt;
 It states that, in a computer program, if S is a subtype of T, then &lt;br /&gt;
&lt;br /&gt;
objects of type T may be replaced with objects of type S (i.e., objects of &lt;br /&gt;
&lt;br /&gt;
type S may be substitutes for objects of type T) without altering any of &lt;br /&gt;
&lt;br /&gt;
the desirable properties of that program (correctness, task performed, &lt;br /&gt;
&lt;br /&gt;
etc.). &lt;br /&gt;
&lt;br /&gt;
If for each object o1 of type S there is an object o2 of type T such that &lt;br /&gt;
&lt;br /&gt;
for all programs P defined in terms of T, the behavior of P is unchanged &lt;br /&gt;
&lt;br /&gt;
when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In less formal terms, it says that if a client (program) expects objects of &lt;br /&gt;
&lt;br /&gt;
one type to behave in a certain way, then it’s only okay to substitute &lt;br /&gt;
&lt;br /&gt;
objects of another type if the same expectations are satisfied.&lt;br /&gt;
&lt;br /&gt;
Consider the following example which satisfies the Liskov Substitution &lt;br /&gt;
&lt;br /&gt;
Principle&lt;br /&gt;
&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Sparrow extends Birds&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
}  &lt;br /&gt;
&lt;br /&gt;
Example which does not satisfy the Liskov Substitution Principle&lt;br /&gt;
&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
If an override method does nothing or just throws an exception, then you’re &lt;br /&gt;
&lt;br /&gt;
probably violating the LSP.&lt;br /&gt;
Therefore,this does not satisfy the Liskov Substitution Principle though &lt;br /&gt;
&lt;br /&gt;
penguin &amp;quot;is-a&amp;quot; bird.&lt;br /&gt;
&lt;br /&gt;
/* This is a direct lift off so we need to paraphrase it*/&lt;br /&gt;
&lt;br /&gt;
The Liskov Substitution Principle in duck typed languages&lt;br /&gt;
In programming languages with duck typing (e.g. Ruby, Python, Smalltalk) &lt;br /&gt;
&lt;br /&gt;
classes don’t really define types. There is no type checking when assigning &lt;br /&gt;
&lt;br /&gt;
objects to variables or passing objects as method arguments. There is a &lt;br /&gt;
&lt;br /&gt;
kind of type checking when a method is called. It is checked that the &lt;br /&gt;
&lt;br /&gt;
method exists with a matching number of parameters.&lt;br /&gt;
Since classes don’t define types, inheritance in duck typed languages has &lt;br /&gt;
&lt;br /&gt;
nothing to do with the subtype relation or the LSP.&lt;br /&gt;
In a way clients define interfaces in an implicit way. Let’s look at an &lt;br /&gt;
&lt;br /&gt;
example:&lt;br /&gt;
def my_method1(a) do&lt;br /&gt;
    a.m1()&lt;br /&gt;
    a.m2()&lt;br /&gt;
    my_method2(a)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def my_method2(a) do&lt;br /&gt;
  a.m3()&lt;br /&gt;
end&lt;br /&gt;
Calling my_method1 with an object a will succeed if the object a provides &lt;br /&gt;
&lt;br /&gt;
the three methods m1, m2 and m3. This is the implicit interface the object &lt;br /&gt;
&lt;br /&gt;
a needs to implement. And with this implicit interface comes the clients &lt;br /&gt;
&lt;br /&gt;
expectation about the behaviour of m1, m2 and m3. That’s just the same as &lt;br /&gt;
&lt;br /&gt;
with the Java interface. If an object b provides m1, m2 and m3 but doesn’t &lt;br /&gt;
&lt;br /&gt;
fit the expected behaviour, the LSP is violated.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The LSP isn’t tied to inheritance or class based typing. It applies to duck &lt;br /&gt;
&lt;br /&gt;
types languages as well as to systems without inheritance. LSP is a concept &lt;br /&gt;
&lt;br /&gt;
that applies to all kinds of polymorphism. Only if you don’t use &lt;br /&gt;
&lt;br /&gt;
polymorphism of all you don’t need to care about the LSP.&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54809</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54809"/>
		<updated>2011-11-10T23:36:22Z</updated>

		<summary type="html">&lt;p&gt;Ssounda2: /* Subclassing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Subclassing==&lt;br /&gt;
&lt;br /&gt;
Subclassing is the concept of creating a specialization(subclass/ derived class) of a base class(superclass/ parent class) by inheriting the methods and instance data from the base class.&lt;br /&gt;
&lt;br /&gt;
==Why do we Subclass?==&lt;br /&gt;
1. Code reuse&lt;br /&gt;
2. Specialization: A subclass can define new methods it's superclass does not handle.&lt;br /&gt;
3. Method Overriding: An overridding method can either have minor modifications or be completely changed from its parent class' &lt;br /&gt;
implementation.&lt;br /&gt;
&lt;br /&gt;
===Is Subclassing same as Subtyping?===&lt;br /&gt;
&lt;br /&gt;
==Subtyping==&lt;br /&gt;
A is said to be a type of B if A's specification is same as B's. Subtypes should satisfy the Liskov Substitution Principle which states,&lt;br /&gt;
&lt;br /&gt;
  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of   &lt;br /&gt;
  P is unchanged when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In most programming languages,for example java, ruby, C++, subclassing does not essentially mean subtyping. For a class to be a   subtype, the subclass must follow the Liskov Substitution Principle. However it is easy to override a method with a completely new implementation and fewer constraints. This is not checked by the compiler and we can create subclasses which are not subtypes.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Subclassing==&lt;br /&gt;
Declaring one class to be a subclass of another class there by allowing a &lt;br /&gt;
&lt;br /&gt;
subclass to inherit the functionality from its super class is called &lt;br /&gt;
&lt;br /&gt;
subclassing.&lt;br /&gt;
&lt;br /&gt;
/* we can include here the 4 perspectives of  inheritance  and then mention &lt;br /&gt;
&lt;br /&gt;
LP principle , principle of least astonishment and &lt;br /&gt;
some other solid principles of inheritance like the The Open-Closed &lt;br /&gt;
&lt;br /&gt;
Principle and The Single Responsibility Principle.&lt;br /&gt;
  */ &lt;br /&gt;
Links for the solid principles...&lt;br /&gt;
http://www.objectmentor.com/resources/articles/ocp.pdf&lt;br /&gt;
http://www.objectmentor.com/resources/articles/srp.pdf&lt;br /&gt;
&lt;br /&gt;
===Subclassing in Java===&lt;br /&gt;
&lt;br /&gt;
====Extends Keyword====&lt;br /&gt;
The extends is a Java keyword, which is used in inheritance process of &lt;br /&gt;
&lt;br /&gt;
Java. It specifies the superclass in a class declaration using extends &lt;br /&gt;
&lt;br /&gt;
keyword.To inherit a class, you simply incorporate the definition of one &lt;br /&gt;
&lt;br /&gt;
class into another by using the extends keyword. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class A&lt;br /&gt;
{ &lt;br /&gt;
   int a;&lt;br /&gt;
   char b;&lt;br /&gt;
   void method1()&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
class B extends A    //class B inherits the properties of class A&lt;br /&gt;
{&lt;br /&gt;
   void method2()&lt;br /&gt;
   {&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
===Abstract classes===&lt;br /&gt;
&lt;br /&gt;
Abstract classes in Java are used to declare common characteristics of &lt;br /&gt;
&lt;br /&gt;
subclasses. An abstract class cannot be instantiated i.e an object of an &lt;br /&gt;
&lt;br /&gt;
abstract class cannot be created. It can only be used as a superclass for &lt;br /&gt;
&lt;br /&gt;
other classes that extend the abstract class. Abstract classes are declared &lt;br /&gt;
&lt;br /&gt;
using the abstract keyword. Abstract classes are used to provide a template &lt;br /&gt;
&lt;br /&gt;
or design for concrete subclasses down the inheritance tree.&lt;br /&gt;
&lt;br /&gt;
An abstract class can contain fields that describe the characteristics and &lt;br /&gt;
&lt;br /&gt;
methods that describe the actions that a class can perform. An abstract &lt;br /&gt;
&lt;br /&gt;
class can include methods that contain no implementation. These are called &lt;br /&gt;
&lt;br /&gt;
abstract methods.If a class has any abstract methods, whether declared or &lt;br /&gt;
&lt;br /&gt;
inherited, the entire class must be declared abstract. Abstract methods are &lt;br /&gt;
&lt;br /&gt;
used to provide a template for the classes that inherit the abstract &lt;br /&gt;
&lt;br /&gt;
methods.&lt;br /&gt;
&lt;br /&gt;
Abstract classes cannot be instantiated; they must be subclassed, and &lt;br /&gt;
&lt;br /&gt;
actual implementations must be provided for the abstract methods. Any &lt;br /&gt;
&lt;br /&gt;
implementation specified can, of course, be overridden by additional sub-&lt;br /&gt;
&lt;br /&gt;
classes. An object must have an implementation for all of its methods. You &lt;br /&gt;
&lt;br /&gt;
need to create a subclass that provides an implementation for the abstract &lt;br /&gt;
&lt;br /&gt;
method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
abstract class A&lt;br /&gt;
{&lt;br /&gt;
  void method1()&lt;br /&gt;
  {&lt;br /&gt;
    ...&lt;br /&gt;
    ...&lt;br /&gt;
    ...&lt;br /&gt;
  }&lt;br /&gt;
  abstract void method2();&lt;br /&gt;
}&lt;br /&gt;
class B extends A&lt;br /&gt;
{&lt;br /&gt;
    void method2()&lt;br /&gt;
    {&lt;br /&gt;
        ...&lt;br /&gt;
        ...&lt;br /&gt;
    } &lt;br /&gt;
    &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the class B does not implement the method &amp;quot;method2&amp;quot; then even class B &lt;br /&gt;
&lt;br /&gt;
has to be declared as an Abstract class.&lt;br /&gt;
&lt;br /&gt;
Rules that should be followed before subclassing&lt;br /&gt;
&lt;br /&gt;
===Subclassing vs Subtyping===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===The Liskov Substitution Principle in class typed languages===&lt;br /&gt;
The Liskov Substitution Principle is a way of ensuring that inheritance is &lt;br /&gt;
&lt;br /&gt;
used correctly.&lt;br /&gt;
&lt;br /&gt;
 It states that, in a computer program, if S is a subtype of T, then &lt;br /&gt;
&lt;br /&gt;
objects of type T may be replaced with objects of type S (i.e., objects of &lt;br /&gt;
&lt;br /&gt;
type S may be substitutes for objects of type T) without altering any of &lt;br /&gt;
&lt;br /&gt;
the desirable properties of that program (correctness, task performed, &lt;br /&gt;
&lt;br /&gt;
etc.). &lt;br /&gt;
&lt;br /&gt;
If for each object o1 of type S there is an object o2 of type T such that &lt;br /&gt;
&lt;br /&gt;
for all programs P defined in terms of T, the behavior of P is unchanged &lt;br /&gt;
&lt;br /&gt;
when o1 is substituted for o2, then S is a subtype of T.&lt;br /&gt;
&lt;br /&gt;
In less formal terms, it says that if a client (program) expects objects of &lt;br /&gt;
&lt;br /&gt;
one type to behave in a certain way, then it’s only okay to substitute &lt;br /&gt;
&lt;br /&gt;
objects of another type if the same expectations are satisfied.&lt;br /&gt;
&lt;br /&gt;
Consider the following example which satisfies the Liskov Substitution &lt;br /&gt;
&lt;br /&gt;
Principle&lt;br /&gt;
&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Sparrow extends Birds&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
}  &lt;br /&gt;
&lt;br /&gt;
Example which does not satisfy the Liskov Substitution Principle&lt;br /&gt;
&lt;br /&gt;
class Bird&lt;br /&gt;
{&lt;br /&gt;
   String name;&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
      ...&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
class Penguin extends Birds&lt;br /&gt;
{&lt;br /&gt;
   void fly()&lt;br /&gt;
   {&lt;br /&gt;
      throw new Exception();&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   void altitude()&lt;br /&gt;
   {&lt;br /&gt;
     throw new Exception();&lt;br /&gt;
   }  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
If an override method does nothing or just throws an exception, then you’re &lt;br /&gt;
&lt;br /&gt;
probably violating the LSP.&lt;br /&gt;
Therefore,this does not satisfy the Liskov Substitution Principle though &lt;br /&gt;
&lt;br /&gt;
penguin &amp;quot;is-a&amp;quot; bird.&lt;br /&gt;
&lt;br /&gt;
/* This is a direct lift off so we need to paraphrase it*/&lt;br /&gt;
&lt;br /&gt;
The Liskov Substitution Principle in duck typed languages&lt;br /&gt;
In programming languages with duck typing (e.g. Ruby, Python, Smalltalk) &lt;br /&gt;
&lt;br /&gt;
classes don’t really define types. There is no type checking when assigning &lt;br /&gt;
&lt;br /&gt;
objects to variables or passing objects as method arguments. There is a &lt;br /&gt;
&lt;br /&gt;
kind of type checking when a method is called. It is checked that the &lt;br /&gt;
&lt;br /&gt;
method exists with a matching number of parameters.&lt;br /&gt;
Since classes don’t define types, inheritance in duck typed languages has &lt;br /&gt;
&lt;br /&gt;
nothing to do with the subtype relation or the LSP.&lt;br /&gt;
In a way clients define interfaces in an implicit way. Let’s look at an &lt;br /&gt;
&lt;br /&gt;
example:&lt;br /&gt;
def my_method1(a) do&lt;br /&gt;
    a.m1()&lt;br /&gt;
    a.m2()&lt;br /&gt;
    my_method2(a)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def my_method2(a) do&lt;br /&gt;
  a.m3()&lt;br /&gt;
end&lt;br /&gt;
Calling my_method1 with an object a will succeed if the object a provides &lt;br /&gt;
&lt;br /&gt;
the three methods m1, m2 and m3. This is the implicit interface the object &lt;br /&gt;
&lt;br /&gt;
a needs to implement. And with this implicit interface comes the clients &lt;br /&gt;
&lt;br /&gt;
expectation about the behaviour of m1, m2 and m3. That’s just the same as &lt;br /&gt;
&lt;br /&gt;
with the Java interface. If an object b provides m1, m2 and m3 but doesn’t &lt;br /&gt;
&lt;br /&gt;
fit the expected behaviour, the LSP is violated.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The LSP isn’t tied to inheritance or class based typing. It applies to duck &lt;br /&gt;
&lt;br /&gt;
types languages as well as to systems without inheritance. LSP is a concept &lt;br /&gt;
&lt;br /&gt;
that applies to all kinds of polymorphism. Only if you don’t use &lt;br /&gt;
&lt;br /&gt;
polymorphism of all you don’t need to care about the LSP.&lt;br /&gt;
&lt;br /&gt;
===References===&lt;br /&gt;
http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.aix.cbl.doc%2Ftpoot30.htm&lt;br /&gt;
&lt;br /&gt;
http://courses.csail.mit.edu/6.170/old-www/2001-Spring/recitations/recitation4.html&lt;/div&gt;</summary>
		<author><name>Ssounda2</name></author>
	</entry>
</feed>