Saturday, 1 October 2016

OOPS Concepts, Features & Explanation with Example.

- OOP = Object Oriented Programming.
- It is a programming technique which provides an efficient way to manage Object and its behaviour across a system.
- It provides a way to show/hide relevant data.
- It provides an efficient way for code reusability.


Here I will explain you important concepts of OOP.

(1) Class

- The class can be considered as a blueprint for an object.
- A class is a collection of object.
- It is compulsory to create a class for representation of data.
- Class do not occupy memory space, so it is a merely logical representation of data.

The syntax for declaring a class.

public class College
{
       //your code goes here..
}

(2) Object

- The object is variable of type Class.
- Object possess property and behaviour.
- As a class do not occupy any memory, so to work with real-time data representation you need to make an object of the class.

Syntax
- Here is the syntax to create an object(we just created above).

College objCollege = new College();

(3) Encapsulation

- Encapsulation is the process of keeping item into one logical unit.
- Encapsulation is a technique used to protect the information in an object from another object.
- The concept of data hiding or information hiding can be achieved by encapsulation.
- In c#,Encapsulation is implemented using the access modifier keywords. C# provides 5 types of access modifier,
  which are listed below.
  (i) Public : Public members are accessible by any other code in the same  assembly or another            assembly that reference it.
  (ii)Private : Private member can only be accessed by code in the same class.
  (iii)Protected: Protected member can only be accessed by code in the same class or in a derived class.
  (iv)Internal : Internal member can be accessed by any code in the same assembly, but not from another assembly.
  (v) Protected Internal : Protected Internal member can be accessed by any code in the same assembly, or by any derived class in another assembly.

(4) Polymorphism

- Polymorphism is an ability to take more than one form in different case/scenario.
- It can make different logical units but with the same name. Its behaviour is executed based on input or the way it is called.

class PolymorphismExample
    {
        public void Add(int p_Value1, int p_Value2)
        {
            Console.WriteLine("Result ={0}", p_Value1 + p_Value2); // This will perform addition of p_Value1 and p_Value2
        }

        public void Add(string p_Value1, string p_Value2)
        {
            Console.WriteLine("Result ={0}", p_Value1 + p_Value2); // This will perform concatenation of p_Value1 and p_Value2
        }
    }
 
(5) Inheritance

- Inheritance provides a way to inherit member of another class.
- It Provides a way to reuse code which is already written.

class InheritanceExample
    {
        public abstract class AllCar
        {
            public abstract string GetCarName();
        }

        public class HondaSeries : AllCar
        {
            //this method name matches with same signature/parameters in AllCar class
            public override string GetCarName()
            {
                return "Car Name is Honda City";
            }
        }

        public static void Main(string[] args)
        {
            AllCar objAllCar = new HondaSeries();
            Console.WriteLine(objAllCar.GetCarName());
            Console.Read();
        }
    }

No comments:

Post a Comment