Thursday, January 14, 2016

Lesson 1

Outline
 
Chapter 10
  • Class
  • Object
  • Encapsulation
  • Constructors
 
 Example: HousePlans Class
 
Class Framework
 
A fundamental concept in object oriented programming languages, like C#, is the class. A class is a template or plan for an object. What's an object? It is a representation of thing based on a class. A class is to an object as house plans are to a house.


Lets create a simple class called HousePlans


namespace Chapter10_HousePlans_Sample
{
    /// <summary>
    /// Simple house plans class
    /// </summary>
    class HousePlans
    {
        /// <summary>
        /// Private (not visible outside of class) variable to keep track of door status (defaults to false:closed)
        /// </summary>
        private bool doorOpened;
 
        /// <summary>
        /// Public (visible outside the class and even the project) method to change to the door open status to true:opened
        /// </summary>
        public void OpenDoor()
        {
            doorOpened = true;
        }
 
        /// <summary>
        /// Public (visible outside the class and even the project) method to change to the door open status to false:closed
        /// </summary>
        public void CloseDoor()
        {
            doorOpened = false;
        }
 
        /// <summary>
        /// Overrides the standard tostring method (available in all object) to a specific string
        /// </summary>
        /// <returns>Status of the door(open or closed)</returns>
        public override string ToString()
        {
            return "The door is " + (doorOpened? "Open""Closed");
        }
    }
}



The above class defines our houseplans class. It contains 1 private variable doorOpened which will keep up with the status of the door whether it is opened or closed. Note: it is standard practice to name variables using camel casing. Using camel casing the first word is lower case and the first letter of each remaining word in capitalized. i.e. whiteHouse, incomeTaxStatement, etc.



Objects


When a object is created from a class it is said to be instantiated. An object based on the class (or instance) is created using the new statement.
HousePlans myHouse = new HousePlans();
 When a object is created from a class it is said to be instantiated. An object based on the class (or instance) is created using the new statement.



Creating multiple objects (instances) of a class. If we create two or more objects based on the same class there are not considered equal or the same. They will work identically but can contain different property values.




HousePlans myHouse = new HousePlans();

HousePlans yourHouse = new HousePlans();


Even though our houses are built using the same plans, yourHouse and myHouse are independent of each other. When I can call the myHouse.OpenDoor() method to open the door, nothing in yourHouse is affected.

Encapsulation

Encapsulation, in the context of C#, refers to an object's ability to hide data and behavior that are not necessary to its user. Encapsulation enables a group of properties, methods and other members to be considered a single unit or object.

 The following are the benefits of encapsulation:
  • Protection of data from accidental corruption
  • Specification of the accessibility of each of the members of a class to the code outside the class
  • Flexibility and extensibility of the code and reduction in complexity
  • Lower coupling between objects and hence improvement in code maintainability
Encapsulation is used to restrict access to the members of a class so as to prevent the user of a given class from manipulating objects in ways that are not intended by the designer. While encapsulation hides the internal implementation of the functionalities of class without affecting the overall functioning of the system, it allows the class to service a request for functionality and add or modify its internal structure (data or methods) to suit changing requirements.
We use terms like "scope" when defining an accessibility level.

Public - Accessible from anywhere in the application and outside the application
Internal - Accessible from within the application only
Private - Only accessible to its peers and subordinate methods and properties

Constructors

When an object is created from a class the class constructor is called. All classes have at least one constructor. The default constructor for a class is hidden but can be overridden.  In our class above the constructor is hidden. If it was visible it would look like this:

public HousePlans()
  { 
  }

A constructor is always named the same name as the class and can take 0 or more parameters.

When we instantiate a new object from a class we utilize the constructor of the class.

HousePlans yourHouse = new HousePlans();

The code right of the new command matches our default constructor. It uses the name of the class and takes zero parameters.

Feel free to comment below any questions or thoughts.

No comments:

Post a Comment