<?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=Speri</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=Speri"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Speri"/>
	<updated>2026-05-09T00:23:22Z</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=55949</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=55949"/>
		<updated>2011-11-20T00:53:37Z</updated>

		<summary type="html">&lt;p&gt;Speri: &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 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>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=55494</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=55494"/>
		<updated>2011-11-17T16:08:15Z</updated>

		<summary type="html">&lt;p&gt;Speri: &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;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. Polymorphism is the source of most of the power of OO and 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;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>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=55493</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=55493"/>
		<updated>2011-11-17T16:06:16Z</updated>

		<summary type="html">&lt;p&gt;Speri: &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;
/* 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;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. Polymorphism is the source of most of the power of OO and 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;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>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=55492</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=55492"/>
		<updated>2011-11-17T16:04:35Z</updated>

		<summary type="html">&lt;p&gt;Speri: Created page with &amp;quot;''' Subclassing '''   ==Subclassing==  Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a ...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&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;
/* 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;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. Polymorphism is the source of most of the power of OO and 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;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>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=55491</id>
		<title>CSC/ECE 517 Fall 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=55491"/>
		<updated>2011-11-17T16:03:43Z</updated>

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

		<summary type="html">&lt;p&gt;Speri: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&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;
/* 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;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. Polymorphism is the source of most of the power of OO and 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;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>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_6b_ss&amp;diff=55489</id>
		<title>CSC/ECE 517 Fall 2011/ch1 6b ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_6b_ss&amp;diff=55489"/>
		<updated>2011-11-17T15:56:25Z</updated>

		<summary type="html">&lt;p&gt;Speri: Created page with &amp;quot;=='''Subclassing'''==  Subclassing is a principle of creating a specialization(subclass/ [http://www.webopedia.com/TERM/D/derived_class.html derived class]) of a [http://www.webo...&amp;quot;&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;
==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;
/* 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;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. Polymorphism is the source of most of the power of OO and 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;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>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=55487</id>
		<title>Http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE 517 Fall 2011/ch6 6b ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch6_6b_ss&amp;diff=55487"/>
		<updated>2011-11-17T15:52:54Z</updated>

		<summary type="html">&lt;p&gt;Speri: moved Http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE 517 Fall 2011/ch6 6b ss to Mywiki2 over redirect&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[Mywiki2]]&lt;/div&gt;</summary>
		<author><name>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=55486</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=55486"/>
		<updated>2011-11-17T15:52:54Z</updated>

		<summary type="html">&lt;p&gt;Speri: moved Http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE 517 Fall 2011/ch6 6b ss to Mywiki2 over redirect&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;
==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;
/* 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;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. Polymorphism is the source of most of the power of OO and 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;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>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=55484</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=55484"/>
		<updated>2011-11-17T15:51:35Z</updated>

		<summary type="html">&lt;p&gt;Speri: moved Mywiki2 to Http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE 517 Fall 2011/ch6 6b ss&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;
==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;
/* 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;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. Polymorphism is the source of most of the power of OO and 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;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>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=55192</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=55192"/>
		<updated>2011-11-16T16:31:55Z</updated>

		<summary type="html">&lt;p&gt;Speri: &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;
==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;
/* 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;
LiskovSubstitutionPrinciple is about subtyping, which relates strongly to polymorphism. Polymorphism is the source of most of the power of OO and 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;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>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54867</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54867"/>
		<updated>2011-11-13T04:46:53Z</updated>

		<summary type="html">&lt;p&gt;Speri: &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>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54865</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54865"/>
		<updated>2011-11-13T04:41:01Z</updated>

		<summary type="html">&lt;p&gt;Speri: &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 which is one of the good OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user regarding the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up 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 re-use and if he finds Liskov Substitution Principle to be too restrictive then he can achieve it using alternate methodologies such as DesignByContract.&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;
==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>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54864</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54864"/>
		<updated>2011-11-13T04:32:16Z</updated>

		<summary type="html">&lt;p&gt;Speri: &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 he can also achieve code reuse using Delegation and Composition which are relatively difficult to implement when compared to inheritance .But it is a good practice not to violate LSP principle which is one of the good OO design principles which leads to good OO design.Furthermore,it provides more clarity to the user regarding the behaviour of the inherited methods inside the sub classes.Violation of LSP principle leads to messing up 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;
==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>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54833</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54833"/>
		<updated>2011-11-12T01:59:50Z</updated>

		<summary type="html">&lt;p&gt;Speri: &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;
==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>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54832</id>
		<title>Mywiki2</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Mywiki2&amp;diff=54832"/>
		<updated>2011-11-12T00:23:05Z</updated>

		<summary type="html">&lt;p&gt;Speri: &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;
''''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;
&lt;br /&gt;
&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>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=50388</id>
		<title>CSC/ECE 517 Fall 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=50388"/>
		<updated>2011-09-23T00:33:28Z</updated>

		<summary type="html">&lt;p&gt;Speri: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Link title]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a cs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ri]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b tj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c cm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c sj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c ka]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d sr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e vs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a sc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e dm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e an]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e lm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g vn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g jn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i zf]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g rn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h hs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d gs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b ns]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b jp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a av]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f jm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ad]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e kt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e gp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b qu]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2c bs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2c rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a ca]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b rv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[trial]]&lt;/div&gt;</summary>
		<author><name>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2b_sa&amp;diff=50379</id>
		<title>CSC/ECE 517 Fall 2011/ch2 2b sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch2_2b_sa&amp;diff=50379"/>
		<updated>2011-09-23T00:09:39Z</updated>

		<summary type="html">&lt;p&gt;Speri: Created page with &amp;quot;'''Access Control  in Object Oriented Languages''' :Each Object Oriented Language has its own way of implementing Access Control .This page discusses the various ways in which Ac...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Access Control  in Object Oriented Languages'''&lt;br /&gt;
:Each Object Oriented Language has its own way of implementing Access Control .This page discusses the various ways in which Access control is implemented in some of the Object Oriented Languages. &lt;br /&gt;
=='''Access Control'''==&lt;br /&gt;
Access control in programming languages provides the programmer the ability to define standards that are needed to hide the implementation of the modules from the public interface.Access control provides the programmer ,the ability to prevent under privileged users from access to internal components of a program and restricts them from changing the data within the component to an inconsistent state(i.e elements(members functions and variables) within a program(class)) there by protecting the integrity of the components .This concept of hiding the information in object oriented languages is called [http://en.wikipedia.org/wiki/Encapsulation Encapsulation].&lt;br /&gt;
&lt;br /&gt;
==Access Control in Ruby==&lt;br /&gt;
Access control in ruby is achieved using the following [http://en.wikipedia.org/wiki/Java_syntax#Access_modifiers access modifiers]for the methods.&lt;br /&gt;
*''public''    :The methods that are declared as public can be accessed by any object belong to the class .&lt;br /&gt;
*''private''   :The methods that are declared as private can only be invoked in the context of the current object.&lt;br /&gt;
*''protected'' :The methods that are declared as protected can be invoked by the objects of both the class and the sub-classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby these modifiers only apply to the methods but not the instance variables.&lt;br /&gt;
&lt;br /&gt;
====Public Methods in Ruby====&lt;br /&gt;
Public methods in ruby can be accessed by any object or instance of a Class and its Sub-classes.&lt;br /&gt;
Declaration of  public methods:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end               &lt;br /&gt;
end          &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end &lt;br /&gt;
  public :method              &lt;br /&gt;
end               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Private Methods in Ruby====&lt;br /&gt;
private methods in Ruby can only be invoked by the current object in question and one cannot invoke other object's private methods.&lt;br /&gt;
In Ruby private methods are not private to the class but they are private to the objects. No method is perfectly private in Ruby.&lt;br /&gt;
&lt;br /&gt;
======Declaration of Private methods======&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A &lt;br /&gt;
 private  &lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example to show that private methods in Ruby can only be invoked by the current object in context.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It gives an error because pubm method is called in the context of object 'a',so only object 'a' can &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        invoke the private method but not the new object. &lt;br /&gt;
  end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //private methods of the base class can be accessed by the sub class&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Protected Methods in Ruby===&lt;br /&gt;
protected methods are visible in both the base class as well as the sub-classes(i.e they can be invoked by the objects of both the class and its sub-classes).&lt;br /&gt;
&lt;br /&gt;
Declaration of protected methods is ruby.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 protected&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
 protected :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Unlike private methods ,In Ruby protected methods can be called with an explicit receiver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a protected method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  protected :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //protected methods of the base class can be accessed by the sub class.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby both the private as well as the protected methods can be accessed from outside the class using send method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
a.method                // gives an error&lt;br /&gt;
a.send(:method)         // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
a.send :method          // prints &amp;quot;I am a protected method&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Access Control in C++==&lt;br /&gt;
&lt;br /&gt;
===Controlling Access to the Member Data and Member Functions===&lt;br /&gt;
&lt;br /&gt;
In C++ one can restrict the level of access control on the member data and function using the access specifiers public,private and protected.&lt;br /&gt;
A constructor should never be declared as private.&lt;br /&gt;
Access control is applied to all the names uniformly.&lt;br /&gt;
&lt;br /&gt;
===Controlling the Level of accessibility of Base class and its Members===&lt;br /&gt;
If a class is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inherited]using a public access specifier than the public members of the base class are inherited as the public members of the derived class and  protected members of the base class are inherited as the protected members of the derived class.If a class is inherited using a private access specifier than both the public and protected members of the base class become the private members of the derived class.If a class is inherited using a protected access specifier than both the public and protected members of the base class become the protected. members of the derived class.&lt;br /&gt;
[[File:Access.png|400px|center|upright=0.56|Diagram Depicting the Visibility of members and the Inheritance effect.]]&lt;br /&gt;
However, the sub-class members can determine the values of the  base class private members by using the Base class public member functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
      int a;&lt;br /&gt;
      public:&lt;br /&gt;
             int value()&lt;br /&gt;
             {&lt;br /&gt;
                 a=10;&lt;br /&gt;
                 return a;&lt;br /&gt;
             }&lt;br /&gt;
};&lt;br /&gt;
class B:public A&lt;br /&gt;
{&lt;br /&gt;
      public:     &lt;br /&gt;
      int b;&lt;br /&gt;
      void bvalue()&lt;br /&gt;
      {&lt;br /&gt;
           b=value();     //now b is assigned a value 10 .This is how the sub class can determine the value of the base class private member &lt;br /&gt;
           printf(&amp;quot;%d&amp;quot;,b);  variables        &lt;br /&gt;
      }&lt;br /&gt;
};&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
    B o;&lt;br /&gt;
    o.bvalue();&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding] can be used to access the members of the derived class using the Base class pointer.Dynamic binding or late-binding or run time binding is achieved using virtual functions in the Base class.If the Base class pointer pointing to the derived class object and the matching function is declared virtual in base class, then which function is call is decided at run-time using virtual table entry.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Friendly Functions===&lt;br /&gt;
Like ''send'' method in Ruby,C++ allow the Friend Functions which are not within the scope of the class to access the private members of the class. The friend function are defined like any other function without using either the keyword friend or [http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm :: scope resolution operator].Since they are not within the scope of the class ,they cannot be called using the objects of the class.Declaration of a friend function in either the public or private part of the class does not change it functionality.&lt;br /&gt;
&lt;br /&gt;
===Friend Classes===&lt;br /&gt;
A class declared as a [http://en.wikipedia.org/wiki/Friend_class friend class] of an another class can access all the private and protected members of the class extending friendship.&lt;br /&gt;
&lt;br /&gt;
==Access Control in Java==&lt;br /&gt;
Access control can be defined as the methods that limit accessibility of the resources to a set of users or programs to enforce integrity, confidentiality or availability constraints. &lt;br /&gt;
Java provides two levels of access controls&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Top Level&amp;quot; – Public, or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Member Level&amp;quot; – Public, Private, Protected or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
===Top Level===&lt;br /&gt;
On the Top level come classes and interfaces. An Interface can only be declared as public. While a class can be declared to be either public or default. If a class is declared as public then it is visible to all the classes in the same or a different package. But if a class is declared with no access specifier i.e. default, then it is public in its own package i.e. it cannot be accessed outside the package other than the one in which it is declared.&lt;br /&gt;
===Member Level===&lt;br /&gt;
At the member level we can declare fields, methods and constructors as public, private, protected and default. Public and default modifiers are used in the same way as the top level and with the same meaning. When a member of a class is declared as private then it can be accessed only within the class. Also, when a member is declared as protected then it can be accessed by all the classes in the same package and the sub classes (of its class) in other packages. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
To understand it in more detail consider the following the example.&lt;br /&gt;
In the example each package contains two classes. Class C is a sub class of class A. Assuming class A as public then its members can be accessed like –&lt;br /&gt;
Class A – It can access all its members independent of their access modifier.&lt;br /&gt;
Class B – It can access all the members of class A except those are private. &lt;br /&gt;
Class C – It can access public and protected members of class A because it is a sub class of class A.&lt;br /&gt;
Class D – It can access only the public members of class A. &lt;br /&gt;
The access modifiers of the top level component govern whether its members are available in the other packages i.e. even if we are having public methods in a class but the class is declared as default then its methods will not be available in other packages. &lt;br /&gt;
If one’s class is going to be used by other coders then he must ensure that errors due to misuse do not happen. For this he must provide the most restrictive access to its members like always declare the fields as private and provide methods to access/modify them.&lt;br /&gt;
&lt;br /&gt;
==Access Control in C#==&lt;br /&gt;
The accessibility levels of the types (class, interface, struct, enum etc.) and the members, controls whether they can be used from the other code in our assembly (*) or other assemblies.&lt;br /&gt;
&lt;br /&gt;
===Accessibility levels provided by C#===&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Public&amp;quot;: It can be used for the types and type members. It allows them to be accessed everywhere in the same assembly or the other assembly.&lt;br /&gt;
*&amp;quot;Private&amp;quot;: It can be used only for the members. Private members are accessible only within the class or the struct in which they are declared. &lt;br /&gt;
*&amp;quot;Protected&amp;quot;: It is a member access modifier. It allows the class to hide its members from the other classes except the child classes. &lt;br /&gt;
*&amp;quot;Internal&amp;quot;: It can be used both for the types and type members. Internal types and members are accessible only by the code in the same assembly.&lt;br /&gt;
*&amp;quot;Protected Internal&amp;quot;: It’s a combination of protected and internal modifier. It allows the types and the members to be used by any code in the same assembly or by any derived class in another assembly. A protected internal element can be accessed in a derived class in another assembly by using an instance of the derived class.&lt;br /&gt;
[[File:Access2.png|400px|center|upright=0.56]]&lt;br /&gt;
&lt;br /&gt;
Usage of these access modifiers for all types or members is dependent on the conditions like the accessibility of the container restricts the accessibility of a type member. &lt;br /&gt;
*- Assembly is a physical unit that can be executed, deployed, versioned and secured.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
The philosophies of access controls in object oriented languages have not changed much over time but certainly the access control across different Object Oriented Languages are different if not completely.   &lt;br /&gt;
 &lt;br /&gt;
==References==&lt;br /&gt;
# http://ruby-doc.org/core/&lt;br /&gt;
# http://rubylearning.com/satishtalim/ruby_access_control.html&lt;br /&gt;
# http://phrogz.net/programmingruby/tut_classes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx&lt;br /&gt;
# http://www.kuzbass.ru:8086/docs/isocpp/access.html&lt;br /&gt;
# http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr130.htm&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx&lt;br /&gt;
# http://stackoverflow.com/questions/2084801/c-using-declaration-scope-and-access-control&lt;/div&gt;</summary>
		<author><name>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=User:Speri&amp;diff=50369</id>
		<title>User:Speri</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=User:Speri&amp;diff=50369"/>
		<updated>2011-09-22T23:52:55Z</updated>

		<summary type="html">&lt;p&gt;Speri: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Access Control  in Object Oriented Languages'''&lt;br /&gt;
:Each Object Oriented Language has its own way of implementing Access Control .This page discusses the various ways in which Access control is implemented in some of the Object Oriented Languages. &lt;br /&gt;
=='''Access Control'''==&lt;br /&gt;
Access control in programming languages provides the programmer the ability to define standards that are needed to hide the implementation of the modules from the public interface.Access control provides the programmer ,the ability to prevent under privileged users from access to internal components of a program and restricts them from changing the data within the component to an inconsistent state(i.e elements(members functions and variables) within a program(class)) there by protecting the integrity of the components .This concept of hiding the information in object oriented languages is called [http://en.wikipedia.org/wiki/Encapsulation Encapsulation].&lt;br /&gt;
&lt;br /&gt;
==Access Control in Ruby==&lt;br /&gt;
Access control in ruby is achieved using the following [http://en.wikipedia.org/wiki/Java_syntax#Access_modifiers access modifiers]for the methods.&lt;br /&gt;
*''public''    :The methods that are declared as public can be accessed by any object belong to the class .&lt;br /&gt;
*''private''   :The methods that are declared as private can only be invoked in the context of the current object.&lt;br /&gt;
*''protected'' :The methods that are declared as protected can be invoked by the objects of both the class and the sub-classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby these modifiers only apply to the methods but not the instance variables.&lt;br /&gt;
&lt;br /&gt;
====Public Methods in Ruby====&lt;br /&gt;
Public methods in ruby can be accessed by any object or instance of a Class and its Sub-classes.&lt;br /&gt;
Declaration of  public methods:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end               &lt;br /&gt;
end          &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end &lt;br /&gt;
  public :method              &lt;br /&gt;
end               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Private Methods in Ruby====&lt;br /&gt;
private methods in Ruby can only be invoked by the current object in question and one cannot invoke other object's private methods.&lt;br /&gt;
In Ruby private methods are not private to the class but they are private to the objects. No method is perfectly private in Ruby.&lt;br /&gt;
&lt;br /&gt;
======Declaration of Private methods======&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A &lt;br /&gt;
 private  &lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example to show that private methods in Ruby can only be invoked by the current object in context.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It gives an error because pubm method is called in the context of object 'a',so only object 'a' can &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        invoke the private method but not the new object. &lt;br /&gt;
  end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //private methods of the base class can be accessed by the sub class&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Protected Methods in Ruby===&lt;br /&gt;
protected methods are visible in both the base class as well as the sub-classes(i.e they can be invoked by the objects of both the class and its sub-classes).&lt;br /&gt;
&lt;br /&gt;
Declaration of protected methods is ruby.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 protected&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
 protected :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Unlike private methods ,In Ruby protected methods can be called with an explicit receiver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a protected method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  protected :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //protected methods of the base class can be accessed by the sub class.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby both the private as well as the protected methods can be accessed from outside the class using send method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
a.method                // gives an error&lt;br /&gt;
a.send(:method)         // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
a.send :method          // prints &amp;quot;I am a protected method&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Access Control in C++==&lt;br /&gt;
&lt;br /&gt;
===Controlling Access to the Member Data and Member Functions===&lt;br /&gt;
&lt;br /&gt;
In C++ one can restrict the level of access control on the member data and function using the access specifiers public,private and protected.&lt;br /&gt;
A constructor should never be declared as private.&lt;br /&gt;
Access control is applied to all the names uniformly.&lt;br /&gt;
&lt;br /&gt;
===Controlling the Level of accessibility of Base class and its Members===&lt;br /&gt;
If a class is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inherited]using a public access specifier than the public members of the base class are inherited as the public members of the derived class and  protected members of the base class are inherited as the protected members of the derived class.If a class is inherited using a private access specifier than both the public and protected members of the base class become the private members of the derived class.If a class is inherited using a protected access specifier than both the public and protected members of the base class become the protected. members of the derived class.&lt;br /&gt;
[[File:Access.png|400px|center|upright=0.56|Diagram Depicting the Visibility of members and the Inheritance effect.]]&lt;br /&gt;
However, the sub-class members can determine the values of the  base class private members by using the Base class public member functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
      int a;&lt;br /&gt;
      public:&lt;br /&gt;
             int value()&lt;br /&gt;
             {&lt;br /&gt;
                 a=10;&lt;br /&gt;
                 return a;&lt;br /&gt;
             }&lt;br /&gt;
};&lt;br /&gt;
class B:public A&lt;br /&gt;
{&lt;br /&gt;
      public:     &lt;br /&gt;
      int b;&lt;br /&gt;
      void bvalue()&lt;br /&gt;
      {&lt;br /&gt;
           b=value();     //now b is assigned a value 10 .This is how the sub class can determine the value of the base class private member &lt;br /&gt;
           printf(&amp;quot;%d&amp;quot;,b);  variables        &lt;br /&gt;
      }&lt;br /&gt;
};&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
    B o;&lt;br /&gt;
    o.bvalue();&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding] can be used to access the members of the derived class using the Base class pointer.Dynamic binding or late-binding or run time binding is achieved using virtual functions in the Base class.If the Base class pointer pointing to the derived class object and the matching function is declared virtual in base class, then which function is call is decided at run-time using virtual table entry.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Friendly Functions===&lt;br /&gt;
Like ''send'' method in Ruby,C++ allow the Friend Functions which are not within the scope of the class to access the private members of the class. The friend function are defined like any other function without using either the keyword friend or [http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm :: scope resolution operator].Since they are not within the scope of the class ,they cannot be called using the objects of the class.Declaration of a friend function in either the public or private part of the class does not change it functionality.&lt;br /&gt;
&lt;br /&gt;
===Friend Classes===&lt;br /&gt;
A class declared as a [http://en.wikipedia.org/wiki/Friend_class friend class] of an another class can access all the private and protected members of the class extending friendship.&lt;br /&gt;
&lt;br /&gt;
==Access Control in Java==&lt;br /&gt;
Access control can be defined as the methods that limit accessibility of the resources to a set of users or programs to enforce integrity, confidentiality or availability constraints. &lt;br /&gt;
Java provides two levels of access controls&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Top Level&amp;quot; – Public, or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Member Level&amp;quot; – Public, Private, Protected or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
===Top Level===&lt;br /&gt;
On the Top level come classes and interfaces. An Interface can only be declared as public. While a class can be declared to be either public or default. If a class is declared as public then it is visible to all the classes in the same or a different package. But if a class is declared with no access specifier i.e. default, then it is public in its own package i.e. it cannot be accessed outside the package other than the one in which it is declared.&lt;br /&gt;
===Member Level===&lt;br /&gt;
At the member level we can declare fields, methods and constructors as public, private, protected and default. Public and default modifiers are used in the same way as the top level and with the same meaning. When a member of a class is declared as private then it can be accessed only within the class. Also, when a member is declared as protected then it can be accessed by all the classes in the same package and the sub classes (of its class) in other packages. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
To understand it in more detail consider the following the example.&lt;br /&gt;
In the example each package contains two classes. Class C is a sub class of class A. Assuming class A as public then its members can be accessed like –&lt;br /&gt;
Class A – It can access all its members independent of their access modifier.&lt;br /&gt;
Class B – It can access all the members of class A except those are private. &lt;br /&gt;
Class C – It can access public and protected members of class A because it is a sub class of class A.&lt;br /&gt;
Class D – It can access only the public members of class A. &lt;br /&gt;
The access modifiers of the top level component govern whether its members are available in the other packages i.e. even if we are having public methods in a class but the class is declared as default then its methods will not be available in other packages. &lt;br /&gt;
If one’s class is going to be used by other coders then he must ensure that errors due to misuse do not happen. For this he must provide the most restrictive access to its members like always declare the fields as private and provide methods to access/modify them.&lt;br /&gt;
&lt;br /&gt;
==Access Control in C#==&lt;br /&gt;
The accessibility levels of the types (class, interface, struct, enum etc.) and the members, controls whether they can be used from the other code in our assembly (*) or other assemblies.&lt;br /&gt;
&lt;br /&gt;
===Accessibility levels provided by C#===&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Public&amp;quot;: It can be used for the types and type members. It allows them to be accessed everywhere in the same assembly or the other assembly.&lt;br /&gt;
*&amp;quot;Private&amp;quot;: It can be used only for the members. Private members are accessible only within the class or the struct in which they are declared. &lt;br /&gt;
*&amp;quot;Protected&amp;quot;: It is a member access modifier. It allows the class to hide its members from the other classes except the child classes. &lt;br /&gt;
*&amp;quot;Internal&amp;quot;: It can be used both for the types and type members. Internal types and members are accessible only by the code in the same assembly.&lt;br /&gt;
*&amp;quot;Protected Internal&amp;quot;: It’s a combination of protected and internal modifier. It allows the types and the members to be used by any code in the same assembly or by any derived class in another assembly. A protected internal element can be accessed in a derived class in another assembly by using an instance of the derived class.&lt;br /&gt;
[[File:Access2.png|400px|center|upright=0.56]]&lt;br /&gt;
&lt;br /&gt;
Usage of these access modifiers for all types or members is dependent on the conditions like the accessibility of the container restricts the accessibility of a type member. &lt;br /&gt;
*- Assembly is a physical unit that can be executed, deployed, versioned and secured.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# http://ruby-doc.org/core/&lt;br /&gt;
# http://rubylearning.com/satishtalim/ruby_access_control.html&lt;br /&gt;
# http://phrogz.net/programmingruby/tut_classes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx&lt;br /&gt;
# http://www.kuzbass.ru:8086/docs/isocpp/access.html&lt;br /&gt;
# http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr130.htm&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx&lt;br /&gt;
# http://stackoverflow.com/questions/2084801/c-using-declaration-scope-and-access-control&lt;/div&gt;</summary>
		<author><name>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=User:Speri&amp;diff=50359</id>
		<title>User:Speri</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=User:Speri&amp;diff=50359"/>
		<updated>2011-09-22T22:10:17Z</updated>

		<summary type="html">&lt;p&gt;Speri: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Access Control  in Object Oriented Languages'''&lt;br /&gt;
:Each Object Oriented Language has its own way of implementing Access Control .This page discusses the various ways in which Access control is implemented in some of the Object Oriented Languages. &lt;br /&gt;
=='''Access Control'''==&lt;br /&gt;
Access control in programming languages provides the programmer the ability to define standards that are needed to hide the implementation of the modules from the public interface.Access control provides the programmer ,the ability to prevent under privileged users from access to internal components of a program and restricts them from changing the data within the component to an inconsistent state(i.e elements(members functions and variables) within a program(class)) there by protecting the integrity of the components .This concept of hiding the information in object oriented languages is called [http://en.wikipedia.org/wiki/Encapsulation Encapsulation].&lt;br /&gt;
&lt;br /&gt;
==Access Control in Ruby==&lt;br /&gt;
Access control in ruby is achieved using the following [http://en.wikipedia.org/wiki/Java_syntax#Access_modifiers access modifiers]for the methods.&lt;br /&gt;
*''public''    :The methods that are declared as public can be accessed by any object belong to the class .&lt;br /&gt;
*''private''   :The methods that are declared as private can only be invoked in the context of the current object.&lt;br /&gt;
*''protected'' :The methods that are declared as protected can be invoked by the objects of both the class and the sub-classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby these modifiers only apply to the methods but not the instance variables.&lt;br /&gt;
&lt;br /&gt;
====Public Methods in Ruby====&lt;br /&gt;
Public methods in ruby can be accessed by any object or instance of a Class and its Sub-classes.&lt;br /&gt;
Declaration of  public methods:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end               &lt;br /&gt;
end          &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end &lt;br /&gt;
  public :method              &lt;br /&gt;
end               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Private Methods in Ruby====&lt;br /&gt;
private methods in Ruby can only be invoked by the current object in question and one cannot invoke other object's private methods.&lt;br /&gt;
In Ruby private methods are not private to the class but they are private to the objects. No method is perfectly private in Ruby.&lt;br /&gt;
&lt;br /&gt;
======Declaration of Private methods======&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A &lt;br /&gt;
 private  &lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example to show that private methods in Ruby can only be invoked by the current object in context.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It gives an error because pubm method is called in the context of object 'a',so only object 'a' can &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        invoke the private method but not the new object. &lt;br /&gt;
  end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //private methods of the base class can be accessed by the sub class&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Protected Methods in Ruby===&lt;br /&gt;
protected methods are visible in both the base class as well as the sub-classes(i.e they can be invoked by the objects of both the class and its sub-classes).&lt;br /&gt;
&lt;br /&gt;
Declaration of protected methods is ruby.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 protected&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
 protected :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Unlike private methods ,In Ruby protected methods can be called with an explicit receiver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a protected method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  protected :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //protected methods of the base class can be accessed by the sub class.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby both the private as well as the protected methods can be accessed from outside the class using send method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
a.method                // gives an error&lt;br /&gt;
a.send(:method)         // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
a.send :method          // prints &amp;quot;I am a protected method&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Access Control in C++==&lt;br /&gt;
&lt;br /&gt;
===Controlling Access to the Member Data and Member Functions===&lt;br /&gt;
&lt;br /&gt;
In C++ one can restrict the level of access control on the member data and function using the access specifiers public,private and protected.&lt;br /&gt;
A constructor should never be declared as private.&lt;br /&gt;
Access control is applied to all the names uniformly.&lt;br /&gt;
&lt;br /&gt;
===Controlling the Level of accessibility of Base class and its Members===&lt;br /&gt;
If a class is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inherited]using a public access specifier than the public members of the base class are inherited as the public members of the derived class and  protected members of the base class are inherited as the protected members of the derived class.If a class is inherited using a private access specifier than both the public and protected members of the base class become the private members of the derived class.If a class is inherited using a protected access specifier than both the public and protected members of the base class become the protected. members of the derived class.&lt;br /&gt;
[[File:Access.png|400px|center|upright=0.56|Diagram Depicting the Visibility of members and the Inheritance effect.]]&lt;br /&gt;
However, the sub-class members can determine the values of the  base class private members by using the Base class public member functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
      int a;&lt;br /&gt;
      public:&lt;br /&gt;
             int value()&lt;br /&gt;
             {&lt;br /&gt;
                 a=10;&lt;br /&gt;
                 return a;&lt;br /&gt;
             }&lt;br /&gt;
};&lt;br /&gt;
class B:public A&lt;br /&gt;
{&lt;br /&gt;
      public:     &lt;br /&gt;
      int b;&lt;br /&gt;
      void bvalue()&lt;br /&gt;
      {&lt;br /&gt;
           b=value();     //now b is assigned a value 10 .This is how the sub class can determine the value of the base class private member &lt;br /&gt;
           printf(&amp;quot;%d&amp;quot;,b);  variables        &lt;br /&gt;
      }&lt;br /&gt;
};&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
    B o;&lt;br /&gt;
    o.bvalue();&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding] can be used to access the members of the derived class using the Base class pointer.Dynamic binding or late-binding or run time binding is achieved using virtual functions in the Base class.If the Base class pointer pointing to the derived class object and the matching function is declared virtual in base class, then which function is call is decided at run-time using virtual table entry.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Friendly Functions===&lt;br /&gt;
Like ''send'' method in Ruby,C++ allow the Friend Functions which are not within the scope of the class to access the private members of the class. The friend function are defined like any other function without using either the keyword friend or [http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm :: scope resolution operator].Since they are not within the scope of the class ,they cannot be called using the objects of the class.Declaration of a friend function in either the public or private part of the class does not change it functionality.&lt;br /&gt;
&lt;br /&gt;
===Friend Classes===&lt;br /&gt;
A class declared as a [http://en.wikipedia.org/wiki/Friend_class friend class] of an another class can access all the private and protected members of the class extending friendship.&lt;br /&gt;
&lt;br /&gt;
==Access Control in Java==&lt;br /&gt;
Access control can be defined as the methods that limit accessibility of the resources to a set of users or programs to enforce integrity, confidentiality or availability constraints. &lt;br /&gt;
Java provides two levels of access controls&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Top Level&amp;quot; – Public, or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Member Level&amp;quot; – Public, Private, Protected or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
===Top Level===&lt;br /&gt;
On the Top level come classes and interfaces. An Interface can only be declared as public. While a class can be declared to be either public or default. If a class is declared as public then it is visible to all the classes in the same or a different package. But if a class is declared with no access specifier i.e. default, then it is public in its own package i.e. it cannot be accessed outside the package other than the one in which it is declared.&lt;br /&gt;
===Member Level===&lt;br /&gt;
At the member level we can declare fields, methods and constructors as public, private, protected and default. Public and default modifiers are used in the same way as the top level and with the same meaning. When a member of a class is declared as private then it can be accessed only within the class. Also, when a member is declared as protected then it can be accessed by all the classes in the same package and the sub classes (of its class) in other packages. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
To understand it in more detail consider the following the example.&lt;br /&gt;
In the example each package contains two classes. Class C is a sub class of class A. Assuming class A as public then its members can be accessed like –&lt;br /&gt;
Class A – It can access all its members independent of their access modifier.&lt;br /&gt;
Class B – It can access all the members of class A except those are private. &lt;br /&gt;
Class C – It can access public and protected members of class A because it is a sub class of class A.&lt;br /&gt;
Class D – It can access only the public members of class A. &lt;br /&gt;
The access modifiers of the top level component govern whether its members are available in the other packages i.e. even if we are having public methods in a class but the class is declared as default then its methods will not be available in other packages. &lt;br /&gt;
If one’s class is going to be used by other coders then he must ensure that errors due to misuse do not happen. For this he must provide the most restrictive access to its members like always declare the fields as private and provide methods to access/modify them.&lt;br /&gt;
&lt;br /&gt;
==Access Control in C#==&lt;br /&gt;
The accessibility levels of the types (class, interface, struct, enum etc.) and the members, controls whether they can be used from the other code in our assembly (*) or other assemblies.&lt;br /&gt;
&lt;br /&gt;
===Accessibility levels provided by C#===&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Public&amp;quot;: It can be used for the types and type members. It allows them to be accessed everywhere in the same assembly or the other assembly.&lt;br /&gt;
*&amp;quot;Private&amp;quot;: It can be used only for the members. Private members are accessible only within the class or the struct in which they are declared. &lt;br /&gt;
*&amp;quot;Protected&amp;quot;: It is a member access modifier. It allows the class to hide its members from the other classes except the child classes. &lt;br /&gt;
*&amp;quot;Internal&amp;quot;: It can be used both for the types and type members. Internal types and members are accessible only by the code in the same assembly.&lt;br /&gt;
*&amp;quot;Protected Internal&amp;quot;: It’s a combination of protected and internal modifier. It allows the types and the members to be used by any code in the same assembly or by any derived class in another assembly. A protected internal element can be accessed in a derived class in another assembly by using an instance of the derived class.&lt;br /&gt;
[[File:Access2.png|400px|center|upright=0.56]]&lt;br /&gt;
&lt;br /&gt;
Usage of these access modifiers for all types or members is dependent on the conditions like the accessibility of the container restricts the accessibility of a type member. &lt;br /&gt;
*- Assembly is a physical unit that can be executed, deployed, versioned and secured.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# http://ruby-doc.org/core/&lt;br /&gt;
# http://rubylearning.com/satishtalim/ruby_access_control.html&lt;br /&gt;
# http://phrogz.net/programmingruby/tut_classes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx&lt;br /&gt;
# http://www.kuzbass.ru:8086/docs/isocpp/access.html&lt;br /&gt;
# http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr130.htm&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx&lt;/div&gt;</summary>
		<author><name>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Access2.png&amp;diff=50358</id>
		<title>File:Access2.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Access2.png&amp;diff=50358"/>
		<updated>2011-09-22T21:59:59Z</updated>

		<summary type="html">&lt;p&gt;Speri: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=User:Speri&amp;diff=49854</id>
		<title>User:Speri</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=User:Speri&amp;diff=49854"/>
		<updated>2011-09-21T01:03:51Z</updated>

		<summary type="html">&lt;p&gt;Speri: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Access Control  in Object Oriented Languages'''&lt;br /&gt;
Each Object Oriented Language has its own way of implementing Access Control .This page discusses the various ways in which Access control is implemented in some of the Object Oriented Languages. &lt;br /&gt;
=='''Access Control'''==&lt;br /&gt;
Access control in programming languages provides the programmer the ability to define standards that are needed to hide the implementation of the modules from the public interface.Access control provides the programmer ,the ability to prevent under privileged users from access to internal components of a program and restricts them from changing the data within the component to an inconsistent state(i.e elements(members functions and variables) within a program(class)) there by protecting the integrity of the components .This concept of hiding the information in object oriented languages is called [http://en.wikipedia.org/wiki/Encapsulation Encapsulation].&lt;br /&gt;
&lt;br /&gt;
==Access Control in Ruby==&lt;br /&gt;
Access control in ruby is achieved using the following [http://en.wikipedia.org/wiki/Java_syntax#Access_modifiers access modifiers]for the methods.&lt;br /&gt;
*''public''    :The methods that are declared as public can be accessed by any object belong to the class .&lt;br /&gt;
*''private''   :The methods that are declared as private can only be invoked in the context of the current object.&lt;br /&gt;
*''protected'' :The methods that are declared as protected can be invoked by the objects of both the class and the sub-classes.&lt;br /&gt;
&lt;br /&gt;
In Ruby these modifiers only apply to the methods but not the instance variables.&lt;br /&gt;
&lt;br /&gt;
====Public Methods in Ruby====&lt;br /&gt;
Public methods in ruby can be accessed by any object or instance of a Class and its Sub-classes.&lt;br /&gt;
Declaration of  public methods:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end               &lt;br /&gt;
end          &lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;      &lt;br /&gt;
class A              &lt;br /&gt;
  def method     &lt;br /&gt;
    puts &amp;quot;hello&amp;quot;    &lt;br /&gt;
  end &lt;br /&gt;
  public :method              &lt;br /&gt;
end               &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Private Methods in Ruby====&lt;br /&gt;
private methods in Ruby can only be invoked by the current object in question and one cannot invoke other object's private methods.&lt;br /&gt;
In Ruby private methods are not private to the class but they are private to the objects. No method is perfectly private in Ruby.&lt;br /&gt;
&lt;br /&gt;
======Declaration of Private methods======&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A &lt;br /&gt;
 private  &lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
   def method&lt;br /&gt;
   end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example to show that private methods in Ruby can only be invoked by the current object in context.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It gives an error because pubm method is called in the context of object 'a',so only object 'a' can &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        invoke the private method but not the new object. &lt;br /&gt;
  end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //private methods of the base class can be accessed by the sub class&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Protected Methods in Ruby===&lt;br /&gt;
protected methods are visible in both the base class as well as the sub-classes(i.e they can be invoked by the objects of both the class and its sub-classes).&lt;br /&gt;
&lt;br /&gt;
Declaration of protected methods is ruby.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 protected&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
  end&lt;br /&gt;
 protected :method&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Unlike private methods ,In Ruby protected methods can be called with an explicit receiver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a protected method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  protected :method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
class B&lt;br /&gt;
  def m1&lt;br /&gt;
     method                         //protected methods of the base class can be accessed by the sub class.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
b=B.new&lt;br /&gt;
a.pubm&lt;br /&gt;
a.method                           //gives an error&lt;br /&gt;
b.m1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby both the private as well as the protected methods can be accessed from outside the class using send method.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  def method&lt;br /&gt;
    puts &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  def pubm&lt;br /&gt;
    method&lt;br /&gt;
    A.new.method                     // It does not give an error because in the context of the current object, another object belonging to same &lt;br /&gt;
    puts &amp;quot;I am a public  method&amp;quot;        class can invoke the protected method .&lt;br /&gt;
  end&lt;br /&gt;
  private :method&lt;br /&gt;
end&lt;br /&gt;
a=A.new&lt;br /&gt;
a.method                // gives an error&lt;br /&gt;
a.send(:method)         // prints &amp;quot;I am a private method&amp;quot;&lt;br /&gt;
a.send :method          // prints &amp;quot;I am a protected method&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Access Control in C++==&lt;br /&gt;
&lt;br /&gt;
===Controlling Access to the Member Data and Member Functions===&lt;br /&gt;
&lt;br /&gt;
In C++ one can restrict the level of access control on the member data and function using the access specifiers public,private and protected.&lt;br /&gt;
A constructor should never be declared as private.&lt;br /&gt;
Access control is applied to all the names uniformly.&lt;br /&gt;
&lt;br /&gt;
===Controlling the Level of accessibility of Base class and its Members===&lt;br /&gt;
If a class is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inherited]using a public access specifier than the public members of the base class are inherited as the public members of the derived class and  protected members of the base class are inherited as the protected members of the derived class.If a class is inherited using a private access specifier than both the public and protected members of the base class become the private members of the derived class.If a class is inherited using a protected access specifier than both the public and protected members of the base class become the protected. members of the derived class.&lt;br /&gt;
[[File:Access.png|400px|center|upright=0.56|Diagram Depicting the Visibility of members and the Inheritance effect.]]&lt;br /&gt;
However, the sub-class members can determine the values of the  base class private members by using the Base class public member functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
      int a;&lt;br /&gt;
      public:&lt;br /&gt;
             int value()&lt;br /&gt;
             {&lt;br /&gt;
                 a=10;&lt;br /&gt;
                 return a;&lt;br /&gt;
             }&lt;br /&gt;
};&lt;br /&gt;
class B:public A&lt;br /&gt;
{&lt;br /&gt;
      public:     &lt;br /&gt;
      int b;&lt;br /&gt;
      void bvalue()&lt;br /&gt;
      {&lt;br /&gt;
           b=value();     //now b is assigned a value 10 .This is how the sub class can determine the value of the base class private member &lt;br /&gt;
           printf(&amp;quot;%d&amp;quot;,b);  variables        &lt;br /&gt;
      }&lt;br /&gt;
};&lt;br /&gt;
  int main()&lt;br /&gt;
  {&lt;br /&gt;
    B o;&lt;br /&gt;
    o.bvalue();&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Dynamic_dispatch Dynamic Binding] can be used to access the members of the derived class using the Base class pointer.Dynamic binding or late-binding or run time binding is achieved using virtual functions in the Base class.If the Base class pointer pointing to the derived class object and the matching function is declared virtual in base class, then which function is call is decided at run-time using virtual table entry.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Friendly Functions===&lt;br /&gt;
Like ''send'' method in Ruby,C++ allow the Friend Functions which are not within the scope of the class to access the private members of the class. The friend function are defined like any other function without using either the keyword friend or [http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm :: scope resolution operator].Since they are not within the scope of the class ,they cannot be called using the objects of the class.Declaration of a friend function in either the public or private part of the class does not change it functionality.&lt;br /&gt;
&lt;br /&gt;
===Friend Classes===&lt;br /&gt;
A class declared as a [http://en.wikipedia.org/wiki/Friend_class friend class] of an another class can access all the private and protected members of the class extending friendship.&lt;br /&gt;
&lt;br /&gt;
==Access Control in Java==&lt;br /&gt;
Access control can be defined as the methods that limit accessibility of the resources to a set of users or programs to enforce integrity, confidentiality or availability constraints. &lt;br /&gt;
Java provides two levels of access controls&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Top Level&amp;quot; – Public, or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Member Level&amp;quot; – Public, Private, Protected or Package-Private (no explicit modifier, also known as default).&lt;br /&gt;
===Top Level===&lt;br /&gt;
On the Top level come classes and interfaces. An Interface can only be declared as public. While a class can be declared to be either public or default. If a class is declared as public then it is visible to all the classes in the same or a different package. But if a class is declared with no access specifier i.e. default, then it is public in its own package i.e. it cannot be accessed outside the package other than the one in which it is declared.&lt;br /&gt;
===Member Level===&lt;br /&gt;
At the member level we can declare fields, methods and constructors as public, private, protected and default. Public and default modifiers are used in the same way as the top level and with the same meaning. When a member of a class is declared as private then it can be accessed only within the class. Also, when a member is declared as protected then it can be accessed by all the classes in the same package and the sub classes (of its class) in other packages. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Example====&lt;br /&gt;
To understand it in more detail consider the following the example.&lt;br /&gt;
[[File:Access1.png|400px|center]]&lt;br /&gt;
In the example each package contains two classes. Class C is a sub class of class A. Assuming class A as public then its members can be accessed like –&lt;br /&gt;
Class A – It can access all its members independent of their access modifier.&lt;br /&gt;
Class B – It can access all the members of class A except those are private. &lt;br /&gt;
Class C – It can access public and protected members of class A because it is a sub class of class A.&lt;br /&gt;
Class D – It can access only the public members of class A. &lt;br /&gt;
The access modifiers of the top level component govern whether its members are available in the other packages i.e. even if we are having public methods in a class but the class is declared as default then its methods will not be available in other packages. &lt;br /&gt;
If one’s class is going to be used by other coders then he must ensure that errors due to misuse do not happen. For this he must provide the most restrictive access to its members like always declare the fields as private and provide methods to access/modify them.&lt;br /&gt;
&lt;br /&gt;
==Access Control in C#==&lt;br /&gt;
The accessibility levels of the types (class, interface, struct, enum etc.) and the members, controls whether they can be used from the other code in our assembly (*) or other assemblies.&lt;br /&gt;
&lt;br /&gt;
===Accessibility levels provided by C#===&lt;br /&gt;
&lt;br /&gt;
*&amp;quot;Public&amp;quot;: It can be used for the types and type members. It allows them to be accessed everywhere in the same assembly or the other assembly.&lt;br /&gt;
*&amp;quot;Private&amp;quot;: It can be used only for the members. Private members are accessible only within the class or the struct in which they are declared. &lt;br /&gt;
*&amp;quot;Protected&amp;quot;: It is a member access modifier. It allows the class to hide its members from the other classes except the child classes. &lt;br /&gt;
*&amp;quot;Internal&amp;quot;: It can be used both for the types and type members. Internal types and members are accessible only by the code in the same assembly.&lt;br /&gt;
*&amp;quot;Protected Internal&amp;quot;: It’s a combination of protected and internal modifier. It allows the types and the members to be used by any code in the same assembly or by any derived class in another assembly. A protected internal element can be accessed in a derived class in another assembly by using an instance of the derived class.&lt;br /&gt;
&lt;br /&gt;
Usage of these access modifiers for all types or members is dependent on the conditions like the accessibility of the container restricts the accessibility of a type member. &lt;br /&gt;
*- Assembly is a physical unit that can be executed, deployed, versioned and secured.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# http://ruby-doc.org/core/&lt;br /&gt;
# http://rubylearning.com/satishtalim/ruby_access_control.html&lt;br /&gt;
# http://phrogz.net/programmingruby/tut_classes.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/kktasw36(v=VS.80).aspx&lt;br /&gt;
# http://www.kuzbass.ru:8086/docs/isocpp/access.html&lt;br /&gt;
# http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html&lt;br /&gt;
# http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx&lt;/div&gt;</summary>
		<author><name>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Access1.png&amp;diff=49590</id>
		<title>File:Access1.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Access1.png&amp;diff=49590"/>
		<updated>2011-09-19T21:32:08Z</updated>

		<summary type="html">&lt;p&gt;Speri: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Access.png&amp;diff=49589</id>
		<title>File:Access.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Access.png&amp;diff=49589"/>
		<updated>2011-09-19T21:31:24Z</updated>

		<summary type="html">&lt;p&gt;Speri: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Access.png|400px|left|alt=A cartoon centipede reads books and types on a laptop.|The Wikipede edits ''[[Myriapoda]]''.]]&lt;/div&gt;</summary>
		<author><name>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Access.png&amp;diff=49586</id>
		<title>File:Access.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Access.png&amp;diff=49586"/>
		<updated>2011-09-19T21:27:30Z</updated>

		<summary type="html">&lt;p&gt;Speri: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:Access.png|200px|left|alt=A cartoon centipede reads books and types on a laptop.|The Wikipede edits ''[[Myriapoda]]''.]]&lt;/div&gt;</summary>
		<author><name>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Access.png&amp;diff=49584</id>
		<title>File:Access.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Access.png&amp;diff=49584"/>
		<updated>2011-09-19T21:23:43Z</updated>

		<summary type="html">&lt;p&gt;Speri: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Speri</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=User:Speri&amp;diff=48604</id>
		<title>User:Speri</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=User:Speri&amp;diff=48604"/>
		<updated>2011-09-11T16:45:47Z</updated>

		<summary type="html">&lt;p&gt;Speri: Created page with &amp;quot;Riverside]&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[[Riverside, California|Riverside]]]&lt;/div&gt;</summary>
		<author><name>Speri</name></author>
	</entry>
</feed>