<?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=Srajago3</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=Srajago3"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Srajago3"/>
	<updated>2026-07-15T02:24:07Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54740</id>
		<title>CSC/ECE 517 Fall 2011/ch17 5b br</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch17_5b_br&amp;diff=54740"/>
		<updated>2011-11-03T13:58:10Z</updated>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		<summary type="html">&lt;p&gt;Srajago3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Syntactic Notation ==&lt;br /&gt;
'''&lt;br /&gt;
There 2 common syntax for getters and setters available in programming languages: &lt;br /&gt;
&lt;br /&gt;
Method Call:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
value = ObjectName.getAttribute();&lt;br /&gt;
&lt;br /&gt;
ObjectName.setAttribute(val); &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above notation a getter is the same as any other instance method call. Its common for the getters and setters to be defined as public methods. &lt;br /&gt;
&lt;br /&gt;
Instance Variables:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
ObjectName.Field = Value;&lt;br /&gt;
&lt;br /&gt;
Value = ObjectName.Field;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this notation getters and setters looks like a accessing a class field. In most languages, internally this would invoke a method which would behave very similar to the getter method call described above. This syntactic notation offers a convenience of using intuitive syntax of getting or setting a variable.   &lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* [http://java.about.com/od/workingwithobjects/a/accessormutator.htm Encapsulate] the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* The code is much [http://www.dotnetspider.com/tutorials/DotNet-Tutorial-273.aspx cleaner] when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx fine-grained] access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
'Getters' and 'Setters' allow you to effectively protect your data. This is a technique used greatly when creating classes in C++. The getters and setters are usually public and the variables are made private. The reason for it is the data members are not accessible outside the class. For each variable, a get method will return its value and a set method will set the value.The reason for not editing the methods directly is twofold. Firstly, if you decide that some action should be taken every time you change the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your [http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ CFC] and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx C#], properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#.yC# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden by polymorphism. They are very useful in GUI programming. The compiler generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in [http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Lua], which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set PHP] engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.touch-code-magazine.com/objective-c-properties/ Objective C] we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
[http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 JavaScript] supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Scala] has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000171.html Flex] it is not usually necessary to create the wrapper getter and setting functions on an object because ActionScript supports properties. This means that you can usually just create public properties like:&lt;br /&gt;
public var firstName:String;&lt;br /&gt;
If the internal implementation of getting or setting the firstName property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class:&lt;br /&gt;
private var _firstName:String;&lt;br /&gt;
&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var firstName:String;&lt;br /&gt;
private var _firstName:String;&lt;br /&gt;
&lt;br /&gt;
public function get firstName():String {&lt;br /&gt;
   return _firstName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_firstName:String):void {&lt;br /&gt;
   this._firstName = _firstName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.firstName;&lt;br /&gt;
var2 = obj['firstName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.firstName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a getter method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, we dont want UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot; and getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() getter method can return an object that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. This GetIdentity() call does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000171.html Using getters and setters]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://java.about.com/od/workingwithobjects/a/accessormutator.htm Accessors and Mutators]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49853</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49853"/>
		<updated>2011-09-21T00:45:02Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
'Getters' and 'Setters' allow you to effectively protect your data. This is a technique used greatly when creating classes in C++. The getters and setters are usually public and the variables are made private. The reason for it is the data members are not accessible outside the class. For each variable, a get method will return its value and a set method will set the value.The reason for not editing the methods directly is twofold. Firstly, if you decide that some action should be taken every time you change the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your [http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ CFC] and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx C#], properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#.yC# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden by polymorphism. They are very useful in GUI programming. The compiler generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in [http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Lua], which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set PHP] engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.touch-code-magazine.com/objective-c-properties/ Objective C] we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
[http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 JavaScript] supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Scala] has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000171.html Flex] it is not usually necessary to create the wrapper getter and setting functions on an object because ActionScript supports properties. This means that you can usually just create public properties like:&lt;br /&gt;
public var firstName:String;&lt;br /&gt;
If the internal implementation of getting or setting the firstName property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class:&lt;br /&gt;
private var _firstName:String;&lt;br /&gt;
&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var firstName:String;&lt;br /&gt;
private var _firstName:String;&lt;br /&gt;
&lt;br /&gt;
public function get firstName():String {&lt;br /&gt;
   return _firstName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_firstName:String):void {&lt;br /&gt;
   this._firstName = _firstName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.firstName;&lt;br /&gt;
var2 = obj['firstName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.firstName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000171.html Using getters and setters]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49852</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49852"/>
		<updated>2011-09-21T00:39:40Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: /* Actionscript */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
'Getters' and 'Setters' allow you to effectively protect your data. This is a technique used greatly when creating classes in C++. The getters and setters are usually public and the variables are made private. The reason for it is the data members are not accessible outside the class. For each variable, a get method will return its value and a set method will set the value.The reason for not editing the methods directly is twofold. Firstly, if you decide that some action should be taken every time you change the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your [http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ CFC] and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx C#], properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#.yC# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden by polymorphism. They are very useful in GUI programming. The compiler generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in [http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Lua], which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set PHP] engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.touch-code-magazine.com/objective-c-properties/ Objective C] we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Scala] has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000171.html Flex] it is not usually necessary to create the wrapper getter and setting functions on an object because ActionScript supports properties. This means that you can usually just create public properties like:&lt;br /&gt;
public var firstName:String;&lt;br /&gt;
If the internal implementation of getting or setting the firstName property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class:&lt;br /&gt;
private var _firstName:String;&lt;br /&gt;
&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var firstName:String;&lt;br /&gt;
private var _firstName:String;&lt;br /&gt;
&lt;br /&gt;
public function get firstName():String {&lt;br /&gt;
   return _firstName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_firstName:String):void {&lt;br /&gt;
   this._firstName = _firstName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.firstName;&lt;br /&gt;
var2 = obj['firstName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.firstName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000171.html Using getters and setters]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49851</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49851"/>
		<updated>2011-09-21T00:38:28Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: /* C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
'Getters' and 'Setters' allow you to effectively protect your data. This is a technique used greatly when creating classes in C++. The getters and setters are usually public and the variables are made private. The reason for it is the data members are not accessible outside the class. For each variable, a get method will return its value and a set method will set the value.The reason for not editing the methods directly is twofold. Firstly, if you decide that some action should be taken every time you change the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your [http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ CFC] and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx C#], properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#.yC# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden by polymorphism. They are very useful in GUI programming. The compiler generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in [http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Lua], which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set PHP] engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.touch-code-magazine.com/objective-c-properties/ Objective C] we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Scala] has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000171.html Flex] it is not usually necessary to create the wrapper getter and setting functions on an object because ActionScript supports properties. This means that you can usually just create public properties like:&lt;br /&gt;
&lt;br /&gt;
public var firstName:String;&lt;br /&gt;
&lt;br /&gt;
If the internal implementation of getting or setting the firstName property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class:&lt;br /&gt;
&lt;br /&gt;
private var _firstName:String;&lt;br /&gt;
&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var firstName:String;&lt;br /&gt;
private var _firstName:String;&lt;br /&gt;
&lt;br /&gt;
public function get firstName():String {&lt;br /&gt;
   return _firstName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_firstName:String):void {&lt;br /&gt;
   this._firstName = _firstName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.firstName;&lt;br /&gt;
var2 = obj['firstName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.firstName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000171.html Using getters and setters]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49850</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49850"/>
		<updated>2011-09-21T00:36:25Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: /* Actionscript */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your [http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ CFC] and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx C#], properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#.yC# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden by polymorphism. They are very useful in GUI programming. The compiler generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in [http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Lua], which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set PHP] engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.touch-code-magazine.com/objective-c-properties/ Objective C] we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Scala] has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000171.html Flex] it is not usually necessary to create the wrapper getter and setting functions on an object because ActionScript supports properties. This means that you can usually just create public properties like:&lt;br /&gt;
&lt;br /&gt;
public var firstName:String;&lt;br /&gt;
&lt;br /&gt;
If the internal implementation of getting or setting the firstName property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class:&lt;br /&gt;
&lt;br /&gt;
private var _firstName:String;&lt;br /&gt;
&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var firstName:String;&lt;br /&gt;
private var _firstName:String;&lt;br /&gt;
&lt;br /&gt;
public function get firstName():String {&lt;br /&gt;
   return _firstName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_firstName:String):void {&lt;br /&gt;
   this._firstName = _firstName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.firstName;&lt;br /&gt;
var2 = obj['firstName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.firstName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000171.html Using getters and setters]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49849</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49849"/>
		<updated>2011-09-21T00:35:50Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your [http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ CFC] and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx C#], properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#.yC# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden by polymorphism. They are very useful in GUI programming. The compiler generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in [http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Lua], which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set PHP] engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.touch-code-magazine.com/objective-c-properties/ Objective C] we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Scala] has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
In Flex it is not usually necessary to create the wrapper getter and setting functions on an object because ActionScript supports properties. This means that you can usually just create public properties like:&lt;br /&gt;
&lt;br /&gt;
public var firstName:String;&lt;br /&gt;
&lt;br /&gt;
If the internal implementation of getting or setting the firstName property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class:&lt;br /&gt;
&lt;br /&gt;
private var _firstName:String;&lt;br /&gt;
&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var firstName:String;&lt;br /&gt;
private var _firstName:String;&lt;br /&gt;
&lt;br /&gt;
public function get firstName():String {&lt;br /&gt;
   return _firstName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_firstName:String):void {&lt;br /&gt;
   this._firstName = _firstName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.firstName;&lt;br /&gt;
var2 = obj['firstName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.firstName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000171.html Using getters and setters]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49848</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49848"/>
		<updated>2011-09-21T00:33:34Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: /* Actionscript */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your [http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ CFC] and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx C#], properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#.yC# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden by polymorphism. They are very useful in GUI programming. The compiler generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in [http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Lua], which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set PHP] engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.touch-code-magazine.com/objective-c-properties/ Objective C] we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Scala] has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
In Flex it is not usually necessary to create the wrapper getter and setting functions on an object because ActionScript supports properties. This means that you can usually just create public properties like:&lt;br /&gt;
&lt;br /&gt;
public var firstName:String;&lt;br /&gt;
&lt;br /&gt;
If the internal implementation of getting or setting the firstName property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class:&lt;br /&gt;
&lt;br /&gt;
private var _firstName:String;&lt;br /&gt;
&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var firstName:String;&lt;br /&gt;
private var _firstName:String;&lt;br /&gt;
&lt;br /&gt;
public function get firstName():String {&lt;br /&gt;
   return _firstName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_firstName:String):void {&lt;br /&gt;
   this._firstName = _firstName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.firstName;&lt;br /&gt;
var2 = obj['firstName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.firstName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.jamesward.com/2010/05/07/flex-and-java-differences-getters-setters/  Flex and Java Differences: Getters &amp;amp; Setters]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49845</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49845"/>
		<updated>2011-09-21T00:28:33Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: /* Scala */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your [http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ CFC] and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx C#], properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#.yC# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden by polymorphism. They are very useful in GUI programming. The compiler generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in [http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Lua], which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set PHP] engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.touch-code-magazine.com/objective-c-properties/ Objective C] we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Scala] has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var fullName:String;&lt;br /&gt;
private var _fullName:String;&lt;br /&gt;
&lt;br /&gt;
public function get fullName():String {&lt;br /&gt;
   return _fullName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_fullName:String):void {&lt;br /&gt;
   this._fullName = _fullName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.fullName;&lt;br /&gt;
var2 = obj['fullName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.fullName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.jamesward.com/2010/05/07/flex-and-java-differences-getters-setters/  Flex and Java Differences: Getters &amp;amp; Setters]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49844</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49844"/>
		<updated>2011-09-21T00:27:59Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your [http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ CFC] and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx C#], properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#.yC# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden by polymorphism. They are very useful in GUI programming. The compiler generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in [http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Lua], which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set PHP] engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.touch-code-magazine.com/objective-c-properties/ Objective C] we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Scala has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var fullName:String;&lt;br /&gt;
private var _fullName:String;&lt;br /&gt;
&lt;br /&gt;
public function get fullName():String {&lt;br /&gt;
   return _fullName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_fullName:String):void {&lt;br /&gt;
   this._fullName = _fullName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.fullName;&lt;br /&gt;
var2 = obj['fullName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.fullName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.jamesward.com/2010/05/07/flex-and-java-differences-getters-setters/  Flex and Java Differences: Getters &amp;amp; Setters]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49843</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49843"/>
		<updated>2011-09-21T00:23:49Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your [http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ CFC] and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx C#], properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#.yC# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden by polymorphism. They are very useful in GUI programming. The compiler generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in [http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Lua], which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set PHP] engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.touch-code-magazine.com/objective-c-properties/ Objective C] we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
Scala has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var fullName:String;&lt;br /&gt;
private var _fullName:String;&lt;br /&gt;
&lt;br /&gt;
public function get fullName():String {&lt;br /&gt;
   return _fullName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_fullName:String):void {&lt;br /&gt;
   this._fullName = _fullName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.fullName;&lt;br /&gt;
var2 = obj['fullName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.fullName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.jamesward.com/2010/05/07/flex-and-java-differences-getters-setters/  Flex and Java Differences: Getters &amp;amp; Setters]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49842</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49842"/>
		<updated>2011-09-21T00:22:22Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your [http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ CFC] and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx C#], properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#.yC# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden by polymorphism. They are very useful in GUI programming. The compiler generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in [http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Lua], which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set PHP] engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In Objective C we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
Scala has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var fullName:String;&lt;br /&gt;
private var _fullName:String;&lt;br /&gt;
&lt;br /&gt;
public function get fullName():String {&lt;br /&gt;
   return _fullName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_fullName:String):void {&lt;br /&gt;
   this._fullName = _fullName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.fullName;&lt;br /&gt;
var2 = obj['fullName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.fullName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.jamesward.com/2010/05/07/flex-and-java-differences-getters-setters/  Flex and Java Differences: Getters &amp;amp; Setters]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49841</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49841"/>
		<updated>2011-09-21T00:21:20Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your [http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ CFC] and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx C#], properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#.yC# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden by polymorphism. They are very useful in GUI programming. The compiler generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in Lua, which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set PHP] engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In Objective C we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
Scala has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var fullName:String;&lt;br /&gt;
private var _fullName:String;&lt;br /&gt;
&lt;br /&gt;
public function get fullName():String {&lt;br /&gt;
   return _fullName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_fullName:String):void {&lt;br /&gt;
   this._fullName = _fullName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.fullName;&lt;br /&gt;
var2 = obj['fullName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.fullName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.jamesward.com/2010/05/07/flex-and-java-differences-getters-setters/  Flex and Java Differences: Getters &amp;amp; Setters]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49840</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49840"/>
		<updated>2011-09-21T00:18:41Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your [http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ CFC] and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx C#], properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#.yC# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden by polymorphism. They are very useful in GUI programming. The compiler generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in Lua, which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, PHP engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In Objective C we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
Scala has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var fullName:String;&lt;br /&gt;
private var _fullName:String;&lt;br /&gt;
&lt;br /&gt;
public function get fullName():String {&lt;br /&gt;
   return _fullName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_fullName:String):void {&lt;br /&gt;
   this._fullName = _fullName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.fullName;&lt;br /&gt;
var2 = obj['fullName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.fullName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.jamesward.com/2010/05/07/flex-and-java-differences-getters-setters/  Flex and Java Differences: Getters &amp;amp; Setters]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49838</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49838"/>
		<updated>2011-09-21T00:16:06Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your CFC and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx C#], properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#.yC# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden by polymorphism. They are very useful in GUI programming. The compiler generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in Lua, which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, PHP engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In Objective C we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
Scala has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var fullName:String;&lt;br /&gt;
private var _fullName:String;&lt;br /&gt;
&lt;br /&gt;
public function get fullName():String {&lt;br /&gt;
   return _fullName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_fullName:String):void {&lt;br /&gt;
   this._fullName = _fullName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.fullName;&lt;br /&gt;
var2 = obj['fullName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.fullName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.jamesward.com/2010/05/07/flex-and-java-differences-getters-setters/  Flex and Java Differences: Getters &amp;amp; Setters]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49837</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49837"/>
		<updated>2011-09-21T00:14:12Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: /* C#: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your CFC and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
In C#, properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#.yC# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden by polymorphism. They are very useful in GUI programming. The compiler generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in Lua, which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, PHP engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In Objective C we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
Scala has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var fullName:String;&lt;br /&gt;
private var _fullName:String;&lt;br /&gt;
&lt;br /&gt;
public function get fullName():String {&lt;br /&gt;
   return _fullName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_fullName:String):void {&lt;br /&gt;
   this._fullName = _fullName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.fullName;&lt;br /&gt;
var2 = obj['fullName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.fullName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.jamesward.com/2010/05/07/flex-and-java-differences-getters-setters/  Flex and Java Differences: Getters &amp;amp; Setters]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49835</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49835"/>
		<updated>2011-09-21T00:12:23Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: /* C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your CFC and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
C# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden polymorphicaly. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in Lua, which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, PHP engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In Objective C we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
Scala has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var fullName:String;&lt;br /&gt;
private var _fullName:String;&lt;br /&gt;
&lt;br /&gt;
public function get fullName():String {&lt;br /&gt;
   return _fullName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_fullName:String):void {&lt;br /&gt;
   this._fullName = _fullName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.fullName;&lt;br /&gt;
var2 = obj['fullName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.fullName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.jamesward.com/2010/05/07/flex-and-java-differences-getters-setters/  Flex and Java Differences: Getters &amp;amp; Setters]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49834</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49834"/>
		<updated>2011-09-21T00:10:19Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: /* C++ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
In C#, properties are nothing but natural extension of data fields. The properties are an important features added in language level inside C#. They are very useful in GUI programming The compiler actually generates the appropriate getter and setter methods when it parses the C# property syntax.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.  Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your CFC and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
C# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden polymorphicaly. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in Lua, which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, PHP engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In Objective C we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
Scala has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var fullName:String;&lt;br /&gt;
private var _fullName:String;&lt;br /&gt;
&lt;br /&gt;
public function get fullName():String {&lt;br /&gt;
   return _fullName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_fullName:String):void {&lt;br /&gt;
   this._fullName = _fullName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.fullName;&lt;br /&gt;
var2 = obj['fullName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.fullName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.jamesward.com/2010/05/07/flex-and-java-differences-getters-setters/  Flex and Java Differences: Getters &amp;amp; Setters]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49833</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1h hs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1h_hs&amp;diff=49833"/>
		<updated>2011-09-21T00:07:59Z</updated>

		<summary type="html">&lt;p&gt;Srajago3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''&lt;br /&gt;
== What are getters and setters ? ==&lt;br /&gt;
''' &lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide getter and setter methods to access/modify object data. When writing new classes, it's a good idea to pay attention to the issue of access control. Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. Because of this naming convention, accessor methods are more often referred to as getter methods. This is a good programming practice, since the data fields are not directly accessible outside the class. Thus 'Getters' and 'Setters' allow us to effectively protect your data. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and setters as method calls ==&lt;br /&gt;
'''&lt;br /&gt;
For each variable, a get method will return its value and a set method will set the value. Here are the advantages of using getter and setter as method calls: &lt;br /&gt;
* Encapsulate the data from accidental / inadvertent modification. The private member variable is not part of the public interface of your class; only the public getter and setter methods are, and you are free to change their implementations without changing the public interface of your class&lt;br /&gt;
* If we decide that some action should be taken every time you change/access the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. For Instance, we can keep track of how many times a variable is accessed. &lt;br /&gt;
* Another reason is that a person using the code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Advantages of getters and getters as variable references(properties) ==&lt;br /&gt;
'''&lt;br /&gt;
Properties are a robust way to handle object’s data. Properties support accessing the instance variables the same way as calling an instance method. A property communicates the idea of &amp;quot;I will make a value available to you, or accept a value from you.&amp;quot; It's not an implementation concept, it's an interface concept. Different languages follow specific syntax for adding a property to a class. Following are the advantages properties: &lt;br /&gt;
* the code is much cleaner when accessing a property when compared to Java or C++ which uses 2 separate methods for each instance variable. They are very useful in GUI programming which necessitates using a lot of fields for UI elements&lt;br /&gt;
* With properties we have fine-grained access control. For instance we can define a property as abstract thereby forcing the derived class to define that property. &lt;br /&gt;
* The main advantage of using properties is that if the internal implementation of getting or setting the property needs to change, then the class can be adapted to have getter and setter functions without changing the external interface of the class. This allows the interface of the object to stay the same even if the underlying implementation of getting and setting the property changes. &lt;br /&gt;
&lt;br /&gt;
Realizing the importance of getters and setters, popular IDEs like Eclipse, Visual studio has a built in mechanism to add getter and setter for a field or to create a property (in C#) for a given field.&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples Languages that use getters and setters as method calls: ==&lt;br /&gt;
'''&lt;br /&gt;
'''&lt;br /&gt;
===C++===&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
  private:&lt;br /&gt;
  int x_value;&lt;br /&gt;
&lt;br /&gt;
  public:&lt;br /&gt;
  // Getter&lt;br /&gt;
  int getx_value()&lt;br /&gt;
  {&lt;br /&gt;
      return x;&lt;br /&gt;
  }&lt;br /&gt;
  // Setter&lt;br /&gt;
  void setx_value( int x )&lt;br /&gt;
  {&lt;br /&gt;
      x_value = x;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
void main()&lt;br /&gt;
{ &lt;br /&gt;
    A a;&lt;br /&gt;
    a.setx_value(5);&lt;br /&gt;
    cout&amp;lt;&amp;lt;a.getx_value();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class BookStore&lt;br /&gt;
{ &lt;br /&gt;
  String title;&lt;br /&gt;
  public String getTitle()&lt;br /&gt;
  {&lt;br /&gt;
     return title;&lt;br /&gt;
  }&lt;br /&gt;
  // a setter method might check that the value that is being assigned to the variable is legal&lt;br /&gt;
  public void setTitle( String newTitle ) &lt;br /&gt;
  {&lt;br /&gt;
    if ( newTitle == null )   // Don't allow null strings as titles!&lt;br /&gt;
       title = &amp;quot;(Untitled)&amp;quot;;  // Use an appropriate default value instead.&lt;br /&gt;
    else&lt;br /&gt;
       title = newTitle;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main()&lt;br /&gt;
{&lt;br /&gt;
     BookStore obj;&lt;br /&gt;
     obj. setTitle(&amp;quot;&amp;quot;);&lt;br /&gt;
     System.out.println(obj.getTitle());&lt;br /&gt;
}&lt;br /&gt;
Output:&lt;br /&gt;
(Untitled)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== Perl ===&lt;br /&gt;
'''&lt;br /&gt;
Object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. [http://www.netalive.org/tinkering/serious-perl/#import_extend Perl] does not have private variables but we can still use the concept of getter, setter functions methods.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sub setName &lt;br /&gt;
{&lt;br /&gt;
   my ( $self, $Name ) = @_;&lt;br /&gt;
   $self-&amp;gt;{_Name} = $Name;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
sub getName &lt;br /&gt;
{&lt;br /&gt;
   my( $self ) = @_;&lt;br /&gt;
   return $self-&amp;gt;{_Name};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
=== ColdFusion === &lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
If we want to override the getter and setter method, you just need to define those methods in your CFC and ColdFusion will call our method instead of calling the implicit one.The implicit methods can also do some nice validation if you have given the ‘type’ for properties. For instance, if we define a property as numeric and try to provide a try to set a string as its value It will throw a nice error saying “The value cannot be converted to a numeric”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Component Person&lt;br /&gt;
{&lt;br /&gt;
   property name&lt;br /&gt;
   property age;&lt;br /&gt;
   property city;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;!--- Once an object is created we can directly call the setters and getters for the properties as ---&amp;gt;&lt;br /&gt;
&amp;lt;cfscript&amp;gt;&lt;br /&gt;
   person = new Person();&lt;br /&gt;
   person.setName(&amp;quot;Brad&amp;quot;);&lt;br /&gt;
   person.setAge(46);&lt;br /&gt;
&lt;br /&gt;
   writeOutput(&amp;quot;Name : #person.getName()#&amp;quot;);&lt;br /&gt;
   writeOutput(&amp;quot;Age : #person.getAge()#&amp;quot;); &lt;br /&gt;
&amp;lt;/cfscript&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== Examples from Languages that use getters and setters as instance variable references:  ==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== C#: ===&lt;br /&gt;
'''&lt;br /&gt;
C# also supports static properties, which belongs to the class rather than to the objects of the class. The properties of a Base class can be inherited to a Derived class and can be overridden polymorphicaly. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;class MyClass&lt;br /&gt;
{&lt;br /&gt;
      private int x;&lt;br /&gt;
      public int X&lt;br /&gt;
      {&lt;br /&gt;
            get&lt;br /&gt;
           {&lt;br /&gt;
             return x;&lt;br /&gt;
           }&lt;br /&gt;
          set&lt;br /&gt;
          {&lt;br /&gt;
             x = value;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
//Invocation&lt;br /&gt;
MyClass mc = new MyClass();&lt;br /&gt;
mc.X = 10;                   // calls set accessor of the property X, and pass 10 as value of the standard field 'value'.&lt;br /&gt;
Console.WriteLine(mc.X);     // displays 10. Calls the get accessor of the property X.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Lua===&lt;br /&gt;
'''&lt;br /&gt;
Getter and Setter functions can be implemented in Lua, which is similar to the properties concept in C#. This allows the functions to bind to a property that can be assigned and accessed as a value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class 'Test'&lt;br /&gt;
  function  Test : getLength()&lt;br /&gt;
   return # self.value&lt;br /&gt;
  end&lt;br /&gt;
  function  Test : setLength(newLength)&lt;br /&gt;
    for  i = self.length, newLength do&lt;br /&gt;
        table.insert(self.value, false )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
test =  Test({1, 2, 3, 4})&lt;br /&gt;
print(test.length)&lt;br /&gt;
self.length = property( Test.getLength, Test.setLength )    &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Ruby===&lt;br /&gt;
'''&lt;br /&gt;
Ruby simplifies the creation of getters and setters with a little metaprogramming and the methods attr, attr_reader, attr_writer, and attr_accessor, all from the Module class. By supplying the attr method, Ruby provides a way to acces the instances directly.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person&lt;br /&gt;
    def initialize(lname, fname)&lt;br /&gt;
        @lname = lname&lt;br /&gt;
        @fname = fname&lt;br /&gt;
    end&lt;br /&gt;
attr_reader :lname // creates a getter method&lt;br /&gt;
    attr_writer :lname    // creates a setter method&lt;br /&gt;
    attr_accessor :fname // creates both getter and setter method&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
x = Person.new(&amp;quot;ABC&amp;quot;, &amp;quot;XYZ&amp;quot;)&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
x.fname = &amp;quot;PQR&amp;quot;&lt;br /&gt;
print &amp;quot;My name is &amp;quot;, x.fname, &amp;quot; &amp;quot;, x.lname, &amp;quot;.\n&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
===Php===&lt;br /&gt;
'''&lt;br /&gt;
An interesting aspect of php worth noting is that we can assign values to undefined variables. Because of the above limitation, PHP engine provides two magic methods __get() and __set(). __get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class.In the above example when email@domain.com is assigned to the undefined variable $email, the magic method __set() is called. To this __set() method the name of the variable is passed into $dt variable of __set() method and the value i.e. email@domain.com is passed to $vl variable of the __set() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
class Customer&lt;br /&gt;
{&lt;br /&gt;
   public $name;&lt;br /&gt;
   private $data = array();&lt;br /&gt;
   public function __set($dt, $vl) &lt;br /&gt;
   {&lt;br /&gt;
      $this-&amp;gt;data[$dt] = $vl;&lt;br /&gt;
   }&lt;br /&gt;
   public function __get($dt) &lt;br /&gt;
   {&lt;br /&gt;
      return $this-&amp;gt;data[$dt];&lt;br /&gt;
   }&lt;br /&gt;
} &lt;br /&gt;
$c = new Customer();&lt;br /&gt;
$c-&amp;gt;name = “Sunil”; // $name is set because its public&lt;br /&gt;
$c-&amp;gt;email = “email@domain.com”; //assigning email@domain.com to the $email variable.&lt;br /&gt;
echo $c-&amp;gt;email;&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Objective C===&lt;br /&gt;
'''&lt;br /&gt;
In Objective C we can define a property using @property directive. @synthesize will create automatically for you a setter and a getter methods for the property which becomes accessible during the run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// public header file&lt;br /&gt;
@interface MyObject : NSObject&lt;br /&gt;
{&lt;br /&gt;
  NSString *make; &lt;br /&gt;
  NSString* model;&lt;br /&gt;
  NSNumber* vin;&lt;br /&gt;
}&lt;br /&gt;
@property (readonly, copy) NSString *language;&lt;br /&gt;
@property(readwrite, retain) NSString* make;&lt;br /&gt;
@property(readwrite, retain) NSString* model;&lt;br /&gt;
@property(readwrite, retain) NSNumber* vin;&lt;br /&gt;
@end&lt;br /&gt;
&lt;br /&gt;
#import &amp;quot;SimpleCar.h&amp;quot;&lt;br /&gt;
@implementation SimpleCar&lt;br /&gt;
@synthesize make, model, vin;&lt;br /&gt;
@end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Python ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DoubleIt(object):&lt;br /&gt;
   def _get_value(self):&lt;br /&gt;
       return self._value * 2&lt;br /&gt;
   def _set_value(self, value):&lt;br /&gt;
       self._value = value&lt;br /&gt;
   value = property(_get_value, _set_value)&lt;br /&gt;
&lt;br /&gt;
# Invocation &lt;br /&gt;
x = DoubleIt()&lt;br /&gt;
 &lt;br /&gt;
x.value = 10&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = 'foo'&lt;br /&gt;
print x.value&lt;br /&gt;
 &lt;br /&gt;
x.value = [1,2,3]&lt;br /&gt;
print x.value&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above [http://stefaanlippens.net/python_property_trick python] code, the output is doubled since the getter multiplies the value by 2&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Javascript ===&lt;br /&gt;
'''&lt;br /&gt;
Javascript supports accessing getters and setters as variable as properties. Javascript originally used __defineGetter__ and __defineSetter__ syntax to define properties which is depricated as the language evolved. Object.defineProperty is currently used for creating a property. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function addDataProperty()&lt;br /&gt;
{&lt;br /&gt;
   var newLine = &amp;quot;&amp;lt;br /&amp;gt;&amp;quot;;&lt;br /&gt;
   var obj = {};&lt;br /&gt;
  &lt;br /&gt;
   // Add a data property to the object.&lt;br /&gt;
   Object.defineProperty(obj, &amp;quot;newDataProperty&amp;quot;, &lt;br /&gt;
   {&lt;br /&gt;
       value: 101,&lt;br /&gt;
       writable: true,&lt;br /&gt;
       enumerable: true,&lt;br /&gt;
       configurable: true&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   // Set the property value.&lt;br /&gt;
   obj.newDataProperty = 102;&lt;br /&gt;
   document.write(&amp;quot;Property value: &amp;quot; + obj.newDataProperty + newLine);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Scala ===&lt;br /&gt;
'''&lt;br /&gt;
Scala has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties. This getter code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword. The setter method name is “age_=”. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Person() {&lt;br /&gt;
// Private age variable, renamed to _age&lt;br /&gt;
private var _age = 0&lt;br /&gt;
var name = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// Getter&lt;br /&gt;
def age = _age&lt;br /&gt;
&lt;br /&gt;
// Setter&lt;br /&gt;
def age_= (value:Int):Unit = _age = value&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// We can invoke the setter as follows : &lt;br /&gt;
 can invoke the setter using the following: &lt;br /&gt;
person.age = 99&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
'''&lt;br /&gt;
In [http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk] the names of getter methods are usually the same as the name of the instance variable. A colon is added to the end of setter method name e.g. colour:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     name                           &amp;quot;gets the value of name&amp;quot;&lt;br /&gt;
     name:aName              &amp;quot;sets name to the value of aName&amp;quot;&lt;br /&gt;
&lt;br /&gt;
     address                        &amp;quot;gets the value of address&amp;quot;&lt;br /&gt;
     address:anAddress     &amp;quot;sets anAddress to the value of anAddress&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== Actionscript ===&lt;br /&gt;
'''&lt;br /&gt;
Getting or setting the property would call the getter and setter functions instead of accessing the property directly&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public var fullName:String;&lt;br /&gt;
private var _fullName:String;&lt;br /&gt;
&lt;br /&gt;
public function get fullName():String {&lt;br /&gt;
   return _fullName;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public function set fullName(_fullName:String):void {&lt;br /&gt;
   this._fullName = _fullName;&lt;br /&gt;
}&lt;br /&gt;
// getters&lt;br /&gt;
var1 = obj.fullName;&lt;br /&gt;
var2 = obj['fullName'];&lt;br /&gt;
// setters&lt;br /&gt;
obj.fullName = &amp;quot;Hello World&amp;quot;;&lt;br /&gt;
obj['fullName'] = &amp;quot;Yes&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
==Getters and Setters from a different perspective==&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
A fundamental precept of OO systems is that an object should not expose any of its implementation details. This way, you can change the implementation without changing the code that uses the object. It follows then that in OO systems you should avoid getter and setter functions since they mostly provide access to implementation details. For instance if you need change the return type of a gettr method from int to long to accommodate the big numbers you need to change all the invocations of that getter method. This negates the concept of [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html information hiding]. The same issue arises when you need to change the accessed field’s type. You also have to change the getter’s return type. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Solution:	 &lt;br /&gt;
Maintainability is inversely proportionate to the amount of data that moves between objects. Thus, its best to minimize data movement as much as possible. The idea is to focus on what you will do rather than how you will do it. Don't ask for the information you need to do the work; ask the object that has the information to do the work for you. Lets use an example to explain this better. While building user interfaces, instead of having UI builder class call methods like &amp;quot;GetAttributes()&amp;quot;. Classes can have methods like &amp;quot;DrawYourself()&amp;quot;. Classes can have getters that returns an object that implements an interface. In our UI Construction example, GetIdentity() can return a method that implements Identity Interface and this Interface includes the method &amp;quot;DrawYourself()&amp;quot;. In this example, GetIdentity() does not return a simple field but a complex object with a defined behavior and the internals of this object is kept hidden &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
=== When to Use Getters and Setters ? ===&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
1. When a getter returns an object that implements an Interface since the interface isolates the class that uses this interface from the implementing class. &lt;br /&gt;
2. Generic interfaces like Java Database Connectivity (JDBC) are unaware of how it will be used and in such cases getters are used to provide flexibility&lt;br /&gt;
&lt;br /&gt;
Programmers use getters and setters because of procedural language mindset. A useful design strategy to this end is to center our design in terms of use cases without getters and setters by thinking on the lines of communication or interaction between the objects with well defined behavior.  &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
'''&lt;br /&gt;
Getters and setters provide a convenient method to access the instance variables. A lot of new languages have come up with the support for accessing getters and setters as instance variable reference which improves the readability of the code. While such convenient mechanisms are available, it is important to use the getters and setters wisely with the goal of minimizing the data movement between the objects. &lt;br /&gt;
&lt;br /&gt;
'''&lt;br /&gt;
== References ==&lt;br /&gt;
'''&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx Understanding properties in C#]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html why getters and setters are evil ]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.sunilb.com/php/php5-oops-tutorial-__get-and-__set php5 OOPS tutorial]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://stefaanlippens.net/python_property_trick python-hiding attribute getters and setters behind standard attribute access]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://msdn.microsoft.com/en-us/library/dd548687%28v=vs.94%29.aspx#Y2204 Object.defineProperty Function (JavaScript)]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.dustinmartin.net/2009/10/getters-and-setters-in-scala/ Getters and Setters in Scala]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-9-implicitgenerated-cfc-methods/ ColdFusion 9 : Implicit/Generated CFC Methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.jamesward.com/2010/05/07/flex-and-java-differences-getters-setters/  Flex and Java Differences: Getters &amp;amp; Setters]&lt;br /&gt;
&amp;lt;li&amp;gt; [http://www.touch-code-magazine.com/objective-c-properties/ Objective C properties]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.tutorials4u.com/smalltalk/smalltalk-tutorial-01.htm#Setter Smalltalk methods]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.gamedev.net/topic/590548-luabind-properties-within-lua/ Properties in Lua]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://www.netalive.org/tinkering/serious-perl/#import_extend Perl ]&lt;br /&gt;
&amp;lt;li&amp;gt;[http://http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Instance_Variables Ruby]&lt;/div&gt;</summary>
		<author><name>Srajago3</name></author>
	</entry>
</feed>