C# - Object Oriented Programming

Object Oriented Programming

Object oriented programming (OOP) organizes a program around its data (that is, objects) and a set of well-defined interfaces to that data. An object-oriented program can be characterized as data controlling access to code.

C# provides full support for object-oriented programming including encapsulation, inheritance, and polymorphism.

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 (object) can grip something, or a Student (object) can give their 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, or plan, or template, that describes 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.

Abstraction

Abstraction is "To represent the essential feature without representing the background details." Abstraction lets you focus on what the object does instead of how it does it. Abstraction provides you a generalized view of your classes or objects by providing relevant information. Abstraction is the process of hiding the working style of an object, and showing the information of an object in an understandable manner.

Encapsulation

Encapsulation binds together code and the data it manipulates and keeps them both safe from outside interference and misuse. Encapsulation is a protective container that prevents code and data from being accessed by other code defined outside the container.

Inheritance

When a class includes a property of another class it is known as inheritance.

Inheritance is the process by which one object acquires the properties of another object. A type derives from a base type, taking all the base type members fields and functions. Inheritance is most useful when you need to add functionality to an existing type. For example all .NET classes inherit from the System.Object class, so a class can include new functionality as well as use the existing object's class functions and properties as well.

Polymorphism

Polymorphism is a feature that allows one interface to be used for a general class of action. This concept is often expressed as "one interface, multiple actions". The specific action is determined by the exact nature of circumstances.

Polymorphism means one thing in many form.Basically polymorphism is capability of one object to behave in multiple ways. Example : A man role changes at home, college, and outside the home. There are following types of polymorphism :

  • Static polymorphism(compile time) :It is achieved using function overloading and operator overloading.
  • Dynamic polymorphism(runtime time) :It is achieved using function overriding means using virtual function and abstract method.