Tuesday, October 29, 2013

What is OOP?

    What is OOP?

     

    OOP is a design philosophy. It stands for Object Oriented Programming.

    Object-Oriented Programming (OOP) uses a different set of programming languages than old procedural programming languages (C, Pascal, etc.). Everything in OOP is grouped as self sustainable "objects". Hence, you gain re-usability by means of four main object-oriented programming concepts.

     

    In order to clearly understand the object orientation, let's take your "hand" as an example. The "hand" is a class. Your body has two objects of type hand, named left hand and right hand. Their main functions are controlled/ managed by a set of electrical signals sent through your shoulders (through an interface). So the shoulder is an interface which your body uses to interact with your hands. The hand is a well architected class. The hand is being re-used to create the left hand and the right hand by slightly changing the properties of it.

     

     

    What is an Object?

    An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object's behavior. For example, the hand can grip something or a Student (object) can give the name or address.

    In pure OOP terms an object is an instance of a class.

     

    What is a Class?

     

    A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an object. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations.

     

    A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an object. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations.

     

    public class Student
    {

    }

    According to the sample given below we can say that the student object, named objectStudent,

    has created out of the Student class.

     

    Student objectStudent = new Student();

     

    In real world, you'll often find many individual objects all of the same kind. As an example, there may be thousands of other bicycles in existence, all of the same make and model. Each bicycle has built from the same blueprint. In object-oriented terms, we say that the bicycle is an instance of the class of objects known as bicycles.

     

    In the software world, though you may not have realized it, you have already used classes. For example, the TextBox control, you always used, is made out of the TextBox class, which defines its appearance and capabilities. Each time you drag a TextBox control, you are actually creating a new instance of the TextBox class.

     

     

    Encapsulation

     

    In OOP the encapsulation is mainly achieved by creating classes, the classes expose public methods and properties. The class is kind of a container or capsule or a cell, which encapsulate the set of methods, attribute and properties to provide its indented functionalities to other classes.

     

     

    Encapsulation is a process of binding the data members and member functions into a single unit.

     

    Example for encapsulation is class. A class can contain data structures and methods.

     

    Consider the following class

     

    public class Aperture

    {

    public Aperture ()

    {

    }

    protected double height;

    protected double width;

    protected double thickness;

    public double get volume()

    {

    Double volume=height * width * thickness;

    if (volume<0)

    return 0;

    return volume;

    }

    }

     

     

    In this example we encapsulate some data such as height, width, thickness and method Get Volume. Other methods or objects can interact with this object through methods that have public access modifier

     

    In order to modularize/ define the functionality of a one class, that class can uses functions/ properties exposed by another class in many different ways. According to Object Oriented Programming there are several techniques, classes can use to link with each other and they are named association, aggregation, and composition.

     

    What is Association?

     

    Association is a simple structural connection or channel between classes and is a relationship where all objects have their own lifecycle and there is no owner.

     

     example of Department and Student.

     

    Multiple students can associate with a single Department and single student can associate with multiple Departments, but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.

     

    Here is respective Model and Code for the above example.

     

     

     

     

    What is Aggregation ?

     

     

    Aggregation is a specialize form of Association where all object have their own lifecycle but there is a ownership like parent and child. Child object can not belong to another parent object at the same time. We can think of it as "has-a" relationship.

     

    Implementation details:

    1. Typically we use pointer variables that point to an object that lives outside the scope of the aggregate class
    2. Can use reference values that point to an object that lives outside the scope of the aggregate class
    3. Not responsible for creating/destroying subclasses

    Lets take an example of Employee and Company.

     

    A single Employee can not belong to multiple Companies (legally!! ), but if we delete the Company, Employee object will not destroy.

     

    Here is respective Model and Code for the above example.

     

     

     

     

    What is Composition  ?

     

    Composition is again specialize form ofAggregation. It is a strong type of Aggregation. Here the Parent and Child objects have coincident lifetimes. Child object dose not have it's own lifecycle and if parent object gets deleted, then all of it's child objects will also be deleted.

     

    Implentation details:

     

    1. Typically we use normal member variables

    2. Can use pointer values if the composition class automatically handles

    allocation/deallocation

    3. Responsible for creation/destruction of subclasses

     

    Lets take an example of a relationship between House and it's Rooms.

     

    House can contain multiple rooms there is no independent life for room and any room can not belong to two different house. If we delete the house room will also be automatically deleted.

     

    Here is respective Model and Code for the above example.

     

     

     

    Abstraction:

     

    Abstraction is a process of hiding the implementation details and displaying the essential features.

     

    Example1: A Laptop consists of many things such as processor, motherboard, RAM, keyboard, LCD screen, wireless antenna, web camera, usb ports, battery, speakers etc. To use it, you don't need to know how internally LCD screens, keyboard, web camera, battery, wireless antenna, speaker's works.  You just need to know how to operate the laptop by switching it on. Think about if you would have to call to the engineer who knows all internal details of the laptop before operating it. This would have highly expensive as well as not easy to use everywhere by everyone.

    So here the Laptop is an object that is designed to hide its complexity.

     

    How to abstract: - By using Access Specifiers

     

    .Net has five access Specifiers

     

    Public -- Accessible outside the class through object reference.

    Private -- Accessible inside the class only through member functions.

    Protected -- Just like private but Accessible in derived classes also through member 

    functions.

    Internal -- Visible inside the assembly. Accessible through objects.

    Protected Internal -- Visible inside the assembly through objects and in derived classes outside the assembly through member functions.

     

     

    What is an Abstract class?

     

    Abstract classes, which declared with the abstract keyword, cannot be instantiated. It can only be used as a super-class for other classes that extend the abstract class. Abstract class is the concept and implementation gets completed when it is being realized by a subclass. In addition to this a class can inherit only from one abstract class (but a class may implement many interfaces) and must override all its abstract methods/ properties and may override virtual methods/ properties.

    Abstract classes are ideal when implementing frameworks. As an example, let's study the abstract class named LoggerBase below. Please carefully read the comments as it will help you to understand the reasoning behind this code.

     

    public abstract class LoggerBase
    {
        /// <summary>
        /// field is private, so it intend to use inside the class only
        /// </summary>
        private log4net.ILog logger = null;

        /// <summary>
        /// protected, so it only visible for inherited class
        /// </summary>
        protected LoggerBase()
        {
            // The private object is created inside the constructor
            logger = log4net.LogManager.GetLogger(this.LogPrefix);
            // The additional initialization is done immediately after
            log4net.Config.DOMConfigurator.Configure();
        }

        /// <summary>
        /// When you define the property as abstract,
        /// it forces the inherited class to override the LogPrefix
        /// So, with the help of this technique the log can be made,
        /// inside the abstract class itself, irrespective of it origin.
        /// If you study carefully you will find a reason for not to have "set" method here.
        /// </summary>
        protected abstract System.Type LogPrefix
        {
            get;
        }

        /// <summary>
        /// Simple log method,
        /// which is only visible for inherited classes
        /// </summary>
        /// <param name="message"></param>
        protected void LogError(string message)
        {
            if (this.logger.IsErrorEnabled)
            {
                this.logger.Error(message);
            }
        }

        /// <summary>
        /// Public properties which exposes to inherited class
        /// and all other classes that have access to inherited class
        /// </summary>
        public bool IsThisLogError
        {
            get
            {
                return this.logger.IsErrorEnabled;
            }
        }
    }

     

     

    Inheritance:

     

    Inheritance is a process of deriving the new class from already existing class

    C# is a complete object oriented programming language. Inheritance is one of the primary concepts of object-oriented programming. It allows you to reuse existing code. Through effective use of inheritance, you can save lot of time in your programming and also reduce errors, which in turn will increase the quality of work and productivity. A simple example to understand inheritance in C#.

     

    Using System;

    Public class BaseClass

    {

        Public BaseClass ()

        {

            Console.WriteLine ("Base Class Constructor executed");

        }

                                     

        Public void Write ()

        {

            Console.WriteLine ("Write method in Base Class executed");

        }

    }

                                     

    Public class ChildClass: BaseClass

    {

                                     

        Public ChildClass ()

        {

            Console.WriteLine("Child Class Constructor executed");

        }

       

        Public static void Main ()

        {

            ChildClass CC = new ChildClass ();

            CC.Write ();

        }

    }

    In the Main () method in ChildClass we create an instance of childclass. Then we call the write () method. If you observe the ChildClass does not have a write() method in it. This write () method has been inherited from the parent BaseClass.

    The output of the above program is

     

    Output:

      Base Class Constructor executed

      Child Class Constructor executed

      Write method in Base Class executed

    this output proves that when we create an instance of a child class, the base class constructor will automatically be called before the child class constructor. So in general Base classes are automatically instantiated before derived classes.

    In C# the syntax for specifying BaseClass and ChildClass relationship is shown below. The base class is specified by adding a colon, ":", after the derived class identifier and then specifying the base class name.

    Syntax:  class ChildClassName: BaseClass

                  {

                       //Body

                  }

    C# supports single class inheritance only. What this means is, your class can inherit from only one base class at a time. In the code snippet below, class C is trying to inherit from Class A and B at the same time. This is not allowed in C#. This will lead to a compile time 

    error: Class 'C' cannot have multiple base classes: 'A' and 'B'.

    public class A

    {

    }

    public class B

    {

    }

    public class C : A, B

    {

    }

    In C# Multi-Level inheritance is possible. Code snippet below demonstrates mlti-level inheritance. Class B is derived from Class A. Class C is derived from Class B. So class C, will have access to all members present in both Class A and Class B. As a result of multi-level inheritance Class has access to A_Method(),B_Method() and C_Method().

     

    Note: Classes can inherit from multiple interfaces at the same time. Interview Question: How can you implement multiple inheritance in C#? Ans : Using Interfaces. We will talk about interfaces in our later article.

     

     

     

     

     

     

     

     

     


If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website