Saturday 24 May 2014

Classes and Objects


Classes create objects and objects use methods to communicate between them. We also know this technique as OOP.

BASIC PRINCIPLES OF OOP

All object oriented languages employ three core principles :
  • Inheritance
  • Polymorphism
  • Encapsulation
These are commonly known as three 'pillars' of OOP


Inheritance:

Inheritance is the concept we use to build new calsses using the existing class definitions. Through inheritance we can modify a class the way want to create new objects. The original class is know as base or parent class and the modified one is know as derived class or sub class or child class.
The concept of inheritance facilitates the re-usability of existing code and thus improves the integrity of programs and productivity of programmers.

Polymorphism:

In simple words it is the ability to take more than one form. For example, an operation may exhibit  different behaviour in different situations. The behaviour depends upon the types of data used in the operations. Exp:- an addition operation involving two numeric values will produce a sum and the same addition operation will produce a string if the operands are string values instead of numbers. Similarly, a method when called with one set of parameters may draw a circle but when called with another set of parameters may draw triangle.
Polymorphism is extensively use while implementing inheritance.

Encapsulation:

Encapsulation provides the ability to hide the internal details of an object from its users. The outside user may not be able to change the state of an object directly. However, the state of an object  may be altered indirectly using what are know as accessor and mutator methods.   In C#, encapsulation is implemented using the access modifier keywords public, private and protected. 


DEFINING A CLASS: 

you can say Class is a template of user defined data type with a template that serves to define its properties. Inside the calss wec define variable and  methods, these variables are termed as instances of classes, which are the actual objects.
The basic form of class is:
Class Name-of-the-class
{
    //here you declare variable
   // & methods
}
A class may contains properties, indexers, constructors, destructors and operators. 
 Adding Variables in Class:
 Class FirstClass   // name of the class
{
       int ABC;
       String DEF;
}
Here I declare class name <<FirstClass>> and inside the calss declare two variable one is integer type ABC and another one is String type DEF

 Adding Methods in Class:

There is no such use of datatype inside of the class without define Methods. If we create the object of the class having none method then the class cannot respond to any messages. Methods are declared inside the body of the class, usually after the declaration of instance variables. 

Example:
                type methodname  (parameter-list)
                {
                       // method body here
                 }

 The body actually describes the operations to be performed on the data. 

Class A
{
     int length;
     int width;

    public void Getdata(int x, int y) 

     {
           length= x;
           width= y;

      }

}

  Method Getdata its return type is void because it does not return any value. We pass two integer values x, y and the these value assign to local variables length and width
One more point to consider here that we use keyword public here when define method Getdata. In simple words if we wants to access any method outside of the class then we use public keyword. but we can use other keywords  also.
below I discuss the full use of this calss with return type discuss:


Class A
{
     int length;
     int width;

    public void Getdata(int x, int y) 

     {
           length= x;
           width= y;
      }
   public int Area()
     {
          int area= lenght * width ;
          return area;
     } 


}
Method Area()  is define and we calculate Area of Rectangle and put the result in area variable.


MEMBER ACCESS MODIFIERS:

 What to do if a class wants to hide its details from outsiders. In this context in OOP one term is used name: data hiding .
C# provides a set of access modifiers that can be used with the members of a class to control their visibility to outside users. 


MODIFIERS          

Private                                 
Public                           
Protected             
Internal                   
Public Internal      

All members are private access by default in C#. If we want to assign other visibility then we should specify suitable access modifier.

class Visibility-Test
{
        public int y;
        internal int z;
        protected double a;
        float b; // private default
 }

Methods and data fields that are declared public are directly accessible from an object instance. Private members cannot be accessed by an object instance but can be used directly by the methods inside the class.

HOW TO CREATE OBJECT:

Objects in C# create by using new operator.  The new operator creates an object of the specified class and return  a reference to that object.
here I am discussing previous declared class example:

A newA= new A() ;
we can define multiple object of the same class


A newA2= newA() ;
A newA3= new A () ;


CONSTRUCTORS:

Why we need Constructors: 

In a single line we can say constructors are required assign values to variables. We can do the say thing with the help of methods but its not the job methods, so constructors are come to the pictures. Constructors are valuable for other task also which we discuss later 
Constructors have the same name as the class itself and they do not specify a return type, not even void. This becuase they do not return any value.

Class A
{
     int length;
     int width;
    public A (int x, int y)  // Constructor  here
     {
           length= x;
           width= y;
      }
   public int RectArea()
     {
           int area= lenght * width ; // Area of Rectangle
           return area;
     } 
}
Constructor are usually declare as public but we can declare them as private or protected

Type of Constructor:

  1. Default Constructor
  2. Parameter Constructor
  3. Static Constructor
  4. Copy Constructor
Overloaded Constructors:
A class can contain more than one constructors, but all constructors have different- different parameters. This is called constructor overloading.

Static Constructor:
We can define a constructor as static also. A static constructor is called before any object of the class is created. It is usually used to assign initial values to static data members.

Class ABC
{
      static ABC ( )  // constructor
       {
                // initial static data values here
       }

}
Point to consider here that there is no access modifier on static constructors. It cannot take any, a class can have only one static constructors.

DESTRUCTORS:

Destructors is opposite to a constructor. It's amethod called when an object is no more required. The name of Desctuctor is same as class name and is preceded bu a tilde (~) sign. Like an constructor, destructor has no return type.

Class ABC
{
      ~ ABC ( )  // destructor
       {
          .....
       }
}

* destructor takes no parameters

Valuable Tips are:

  1. Static Variables are assigned when the class is loaded.
  2. Instance variables are assigned when an instance of the class created.
  3. This keyword which is a reference to the object that called the method.
  4. C# also permits to declaration of data field of a class as constants. This can be done by using const  keyword.
  5. The const members are implicitly static.
  6.  Indexers are location indicators and are used to access class objects.


No comments:

Post a Comment

stats counter
your visitor number