This is my first post. This article won’t be the definitive guide of Object oriented Programming System an entire book could easily be devoted to that. Instead, think of this as a good place to start. Good for those how had already know about OOPS. If you get sometime wrong / new please comment me.

Basics of Object Oriented Programming System

What is Opps?
Oops is a problem solving technique.

What is Class?
Class is a description of an Object.

What is an Object?
An Object is an entity that has attribute, behavior and identity of a class.

What is the relation between Class and Object?
Class is the defection, while Object is an instance of the Class created.

What is Encapsulation?
Encapsulation is the localization or hiding of information and processes within an object. Because objects encapsulate both data and processes, the user of an object can view the object as a black box that provides services. Instance variables and methods can be added, deleted, or changed, but as long as the services provided by the object remain the same, code that uses the object can continue to use it without being rewritten.

public class TestMain
{
    private string _Name = string.Empty;

    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
        }
    }
}

Like the above way we can protect the private data from the outside world. In the above example we can’t access the private data Name from an object instance. We manipulate the data only using the property.

What is Polymorphism?
Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. This is called polymorphism. In C#, every type is polymorphic. Types can be used as their own type or as an object instance, because any type automatically treats Object as a base type.

public class BaseClass
{
    public virtual void DoWork() { }
    public virtual int WorkProperty
    {
        get { return 0; }
    }
}
public class DerivedClass : BaseClass
{
    public override void DoWork() { }
    public override int WorkProperty
    {
        get { return 0; }
    }
}

 

DerivedClass B = new DerivedClass();
B.DoWork();  // Calls the new method.
 
BaseClass A = (BaseClass)B;
A.DoWork();  // Also calls the new method.

 

What is Access Modifiers?
Access modifiers are keywords used to specify the declared accessibility of a member or a type. They are …….

  1. Public
  2. Private
  3. Protected
  4. Internal
  5. Protected Internal.

 

Public: All members have access in the classes.
Private: Only members of class have access.
Protected: All members in current class and in derived class can access the variables.
Internal: Only members in current project have access to the element.
Protected Internal: All members of the current project and all members in derived class can access the variables.

What is Properties?
Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields.

Public Int32 ID
    {
        get { return _ID;  }
        set { _ID = value; }
    }

Read Only Property

Public Int32 ID
    {
        get { return _ID;  }
    }
Write Only Property

Public Int32 ID
    {
        set { _ID = value; }
    }

 

What is Implementation Inheritance?
When a class is derived from another class such that it inherits all the members of the base type it is Implementation Inheritance.

What is Interface Inheritance?
When a type (class or a structure) inherits only the signatures of the functions from another type it is Interface Inheritance.

What is Abstract class?
Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but doesn’t contain implementation. Abstract classes can’t be instantiated, and require subclasses to provide implementations for the abstract methods. This class must be inherited. This class is mostly used as a base class.

Abstract Class

public abstract class TestAbstract
{
    public abstract int AddNumber(int One, int two);

    public virtual int AddTwoNumber(int One, int two)
    {
        int Result = One + two;
        return Result;
    }
}

Child Class

public class TestMain:TestAbstract
{
    public override int AddNumber(int One, int two)
    {
        int Result = One + two;
        return Result;
    }

    public override int AddTwoNumber(int One, int two)
    {
        int Result = One +two;
        return Result;
    }

}

What is Virtual function?
A virtual function is a function whose behavior can be overridden within an inheriting class by a function with the same signature. A base class must provide the virtual modifier for any virtual method, and derived classes must provide the override modifier for any override method inherited from a base class.
Virtual methods allow subclasses to provide their own implementation of that method using the override keyword.

What is Abstract  function?
Abstract functions in a class contain no method body, and are implicitly virtual function. Abstract function-modifier exists only to provide a uniform syntactical style.

Neither abstract nor virtual can be declared private, since it would defeat the purpose, and subclasses must override them using the same method signature. If a class has any abstract methods, then it must tag itself as abstract, and no instances of it can be created.

What is Interface?
An interface is a reference type containing only abstract members. These can be events, indexers, methods or properties, but only the member declarations. A class implementing an interface must provide the implementation of the interface members. An interface cannot contain constants, constructors, data fields, destructors, static members or other interfaces. Interface member declarations are implicitly public.

Interface
public interface TestInterface
{
    Int32 ID
    {
        get;
        set;
    }

    String Name
    {
        get;
        set;
    }

    string Address
    {
        get;
        set;
    }

    bool Add();

    bool Update();

    bool Delete();
}

Class
public class Employee : TestInterface
{
    public int ID
    {
        get
        {
            //Implement………
        }
        set
        {
            //Implement………
        }
    }

    public string Name
    {
        get
        {
            //Implement………
        }
        set
        {
            //Implement………
        }
    }

    public string Address
    {
        get
        {
            //Implement………
        }
        set
        {
            //Implement………
        }
    }

    public bool Add()
    {
        //Implement………
    }

    public bool Update()
    {
        //Implement………
    }

    public bool Delete()
    {
        //Implement………
    }
}

 

When a class or structure is said to inherit an interface, it means that the class or structure provides an implementation for all of the members defined by the interface. The interface itself provides no functionality that a class or structure can inherit in the way that base class functionality can be inherited. However, if a base class implements an interface, the derived class inherits that implementation. A class or structure can inherit more than one interface. When a class or structure inherits an interface, it inherits only the method names and signatures, because the interface itself contains no implementations.

Interface versus Abstract Class.
 
An Interface cannot implement methods.
An abstract class can implement methods.
An Interface can only inherit from another Interface.
An abstract class can inherit from a class and one or more interfaces.
An Interface cannot contain fields.
An abstract class can contain fields.
An Interface can contain property definitions.
An abstract class can implement a property.
An Interface cannot contain constructors or destructors.
An abstract class can contain constructors or destructors.
An Interface can be inherited from by structures.
An abstract class cannot be inherited from by structures.
An Interface can support multiple inheritances.
An abstract class cannot support multiple inheritances.

 

What is Sealed Class?
A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed classes prevent derivation.

What is Sealed Methods?
A class member, method, field, property, or event, on a derived class that is overriding a virtual member of the base class can declare that member as sealed. This negates the virtual aspect of the member for any further derived class. This is accomplished by putting the sealed keyword before the override keyword in the class member declaration.

public sealed override int AddTwoNumber(int One, int two)
    {
        int Result = One +two;
        return Result;
    }

What is Function Overloading?
Function overloading is the practice of declaring the same function with different signatures. The same function name will be used with different number of parameters and parameters of different type. But overloading of functions with different return types is not allowed.

public Int32 AddTwoNumber(Int16 One, Int16 Two)

public Int32 AddTwoNumber(Int32 One, Int32 Two)

 

What is function overriding?
If a base class is declared as virtual and derived class is declared as virtual. This redefinition of virtual in derived class is known as Function overriding. Both functions have same parameters.

public virtual int AddNumber(int One, int two)

public override int AddNumber(int One, int two)

What is Class Constructor?
A constructor is a special function which can be included in a class. It is unusual as a function because it is never specifically called, the same way that other functions are. A constructor function is called automatically when the class variable is declared. It is used to set up the class variable with sensible values in the member variables. There is more then one Constructor in a class within a class with different parameters.

public Class ABC
{
public ABC()
{
//Implement………

}
public ABC(string xyz)
{
                        //Implement………
}
}

What is Class Destructor?
Destructor is a special function that is called whenever a class variable is destroyed. A destructor is declared as having the same name as the function, except that the name is preceded by the ~ symbol before it. Apart from that symbol, the declaration of a destructor would be indistinguishable from a constructor. There is always one destructor in a class.

public Class ABC
{
public ABC()
{
//Implement………

}
public ~ABC()
{
                        //Implement………
}
}

What is Delegate?
A delegate is a function pointer. A delegate allows encapsulating a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Delegates are multicasting which means that they can point to more than one function at a time.

What is Event?
An event is a mechanism via which a class can notify its clients when something happens. For example when you click a button, a button-click-event notification is sent to the hosting the button. Events are declared using delegates.

What is Generic Class?
Generic classes encapsulate operations that are not specific to a particular data type. The most common use for generic classes is with collections like linked lists, hash tables, stacks, queues, trees, and so on. Operations such as adding and removing items from the collection are performed in basically the same way regardless of the type of data being stored.

 

3 Responses to “Basics of Object Oriented Programming System”

  1. Hi

    Kya chal raha hai ???

    Rakesh

  2. JP said

    Good one.

  3. Nice Start Pawan! I am the first one who commented on your blog.. :)

Leave a Reply