C# – OOPs Concept
Object Oriented Programming (OOP) is one of the most popular programming languages. This article is an introduction to Object Oriented Programming (OOP) and how to implement OOP in C# including abstraction, encapsulation, inheritance and polymorphism.
Object-oriented programming took the best ideas of structured programming and combined them with several new concepts. The result was a different and better way of organizing a program. In the most general sense, a program can be organized in one of two ways: around its code (what is happening) or around its data (what is being affected). Using only structured programming techniques, programs are typically organized around code. This approach can be thought of as “code acting on data.”
Object-oriented programs work the other way around. They are organized around data, with the key principle being “data controlling access to code.” In an object-oriented language, you define the data and the code that is permitted to act on that data. Thus, a data type defines precisely the operations that can be applied to that data. To support the principles of object-oriented programming, all OOP languages, including C#, have three traits in common: Encapsulation, Polymorphism, Inheritance, Abstraction.
Classes and objects
The terms class and object describe the type of objects, and the instances of classes, respectively. So, the act of creating an object is called instantiation. Using the blueprint analogy, a class is a blueprint, and an object is a building made from that blueprint.
class SampleClass
{
}
Objects are the basic run-time entities of an object oriented system. They may represent a person, a place or any item that the program must handle.
“An object is a software bundle of related variable and methods.” “An object is an instance of a class”
A class will not occupy any memory space. Hence to work with the data represented by the class you must create a variable for the class, that is called an object.
When an object is created using the new operator, memory is allocated for the class in the heap, the object is called an instance and its starting address will be stored in the object in stack memory. An object is created without the new operator, memory will not be allocated in the heap, in other words an instance will not be created and the object in the stack contains the value null. When an object contains null, then it is not possible to access the members of the class using that object.
SampleClass sm = new SampleClass();
Encapsulation
Encapsulation is a programming mechanism that binds together code and the data it manipulates, and that keeps both safe from outside interference and misuse. In an objectoriented language, code and data can be bound together in such a way that a self-contained black box is created. Within the box are all necessary data and code. When code and data are linked together in this fashion, an object is created. In other words, an object is the device that supports encapsulation. Within an object, the code, data, or both may be private to that object or public.
Private code or data is known to and accessible by only another part of the object. That is, private code or data cannot be accessed by a piece of the program that exists outside the object. When code or data is public, other parts of your program can access it even though it is defined within an object. C#’s basic unit of encapsulation is the class. A class defines the form of an object. It specifies both the data and the code that will operate on that data.
C# uses a class specification to construct objects. Objects are instances of a class. Thus, a class is essentially a set of plans that specify how to build an object. Collectively, the code and data that constitute a class are called its members. The data defined by the class is referred to as fields. Method is C#’s term for a subroutine. (Other function members include properties, events, and constructors.) Thus, the methods of a class contain code that acts on the fields defined by that class.
class User
{
private string location;
private string name;
public string Location
{
get
{
return location;
}
set
{
location = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
Polymorphism
Polymorphism (from Greek, meaning “many forms”) is the quality that allows one interface to access a general class of actions. A simple example of polymorphism is found in the steering wheel of an automobile. The steering wheel (the interface) is the same no matter what type of actual steering mechanism is used. That is, the steering wheel works the same whether your car has manual steering, power steering, or rack-and-pinion steering.
Thus,turning the steering wheel left causes the car to go left no matter what type of steering is used. The benefit of the uniform interface is, of course, that once you know how to operate the steering wheel, you can drive any type of car. The same principle can also apply to programming. For example, consider a stack (which is a first-in, last-out list). You might have a program that requires three different types of stacks. One stack is used for integer values, one for floating-point values, and one for characters. In this case, the algorithm that implements each stack is the same, even though the data being stored differs.
Polymorphism helps reduce complexity by allowing the same interface to be used to specify a general class of action. It is the compiler’s job to select the specific action (that is, method) as it applies to each situation. You, the programmer, don’t need to do this selection manually. You need only remember and utilize the general interface.
using System;
namespace method_overloading
{
class Program
{
public class Print
{
public void display(string name)
{
Console.WriteLine("Your name is : " + name);
}
public void display(int age, float marks)
{
Console.WriteLine("Your age is : " + age);
Console.WriteLine("Your marks are :" + marks);
}
}
static void Main(string[] args)
{
Print obj = new Print();
obj.display("George");
obj.display(34, 76.50f);
Console.ReadLine();
}
}
}
Inheritance
Inheritance is the process by which one object can acquire the properties of another object. This is important because it supports the concept of hierarchical classification. If you think about it, most knowledge is made manageable by hierarchical (that is, top-down) classifications. For example, a Red Delicious apple is part of the classification apple, which in turn is part of the fruit class, which is under the larger class food.
That is, the food class possesses certain qualities (edible, nutritious, and so on) which also, logically, apply to its subclass, fruit. In addition to these qualities, the fruit class has specific characteristics (juicy, sweet, and so on) that distinguish it from other food. The apple class defines those qualities specific to an apple (grows on trees, not tropical, and so on). A Red Delicious apple would, in turn, inherit all the qualities of all preceding classes and would define only those qualities that make it unique. Without the use of hierarchies, each object would have to explicitly define all of its characteristics.
Using inheritance, an object need only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. Thus, the inheritance mechanism makes it possible for one object to be a specific instance of a more general case.
class BaseClass
{
}
class DerivedClass : BaseClass
{
}
Abstraction
Abstraction is “To represent the essential feature without representing the background details.”
It 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.
It is also necessary for the construction of programs. Instead of showing how an object is represented or how it works, it focuses on what an object does. Therefore, data abstraction is often used for managing large and complex programs.