Object-Oriented Programming Concepts in C#
Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:
- Inheritance and
Object-Oriented Programming (OOP) is a programming structure where programs are organized around objects as opposed to action and logic. This is important for a design view as it uses a different set of programming languages such as C#. Understanding OOPs concepts can help in making decisions about how you should design an application and what language one must use. This is the most important concept for web application development.
Everything in oops placed together works as a self-contained object. An object is a combination of variable and function and data can perform a set of relevant activity.
- The software is split into a number of small units called objects. The data and functions are put together around these objects.
- The data of the objects can be accessed only by the functions related to that object.
- The functions of one object can access the functions of another object.
- The object is a runtime entities class.
|Version||Year||Key features Introduced|
|1.2||Oct 2003||Modern, object-oriented, Typesafe, automatic memory management, versioning control|
|2.0||Sep 2005||Generics, partial classes, anonymous types, iterators, nullable types, static classes, delegate interface|
|3.0||Aug 2007||Implicit types, object and collection initializers, auto-implemented properties, extension methods, query and lambda expressions, expression trees, partial methods|
|4.0||April 2010||Dynamic binding, named and optional arguments, Generic covariance and Contrivance, Embedded interop types|
|5.0||June 2013||Async methods Caller into Attributes|
|6.0||July 2015||Roslyn (compiler-as-a-service), exception filters, Await in catch/ finally block, auto property initialize, string interpolation, named operator, dictionary initializer|
|7.0||2016||Tuples,Pattern matching,record types,local function,async streams|
Important Features Of OOP
A class is a collection of method and variables. It is a blueprint that defines the data and behavior of a type. If a class does not declare a static, you are required to create an instance/object of a class stating what operation can be performed on that object. Objects are instances of a class. The methods and variables that constitute a class are called members of the class.
We can define a class using the class keyword and the class body enclosed by a pair of curly braces, as shown in the following example:
The keyword class is preceded by the access level. Because the public is used in this case, anyone can create objects from this class. The name of the class follows the keyword class. The remainder of the definition is the class body, where the behavior and data are defined. Fields, properties, methods and events on a class are collectively referred to as class members.
An object is a runtime entity of an object-oriented programing system. An object can be created by using the new keyword to allocate memory for the class in heap, the object is called an instance and its starting address will be stored in the object in stack memory.
When an object is created without the new operator, a 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.
The syntax for creating an object of a class employee:
Abstraction is important because it can hide unnecessary details from reference objects to names. 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.
This binds the member function and data member into a single class. This also allows for abstraction. Within OOP, encapsulation can be achieved through creating classes. Those classes then display public methods and properties. Encapsulation is like an enclosing in a capsule, that is enclosing the related operation and data related to an object into that object.
Encapsulation is just like your bag, in which you can keep your details. It implies it is the property of encapsulating members and functions. Encapsulation is a process to hide internal details to an object; in other words, it states the working of an object.
Encapsulation is a technique used to protect the information in an object from another object. Hide the data for security such as making the variables private and expose the property to access the private data that will be public.
Real world example encapsulation
Let’s take an example of ATM machine and ATM machine manufacturers.
Suppose you are an ATM machine manufacturer and you have designed and developed an ATM machine design (a class). Now by using machinery, you are manufacturing ATM machine (objects) for selling. When you sell your ATM machine the user only learns how to use the ATM machine, but not how the ATM machine works.
Suppose you are creating the class with functions and by with objects of which you are making available the functionality of your class by that object and without the intermediate in the original class.
Inheritance is a feature of object-oriented programming. It allows code reusability when a class includes property of another class it is known as inheritance.
- Parent class constructor called
- child class constructor called
- Parent class method
Polymorphism is a feature of object-oriented programming. It allows you to invoke methods of a derived class through base class reference during runtime. In polymorphism, we will declare methods with same name and different parameters in same class or methods with same name and same parameters in different classes.
Polymorphism has the ability to provide the different implementation of methods that are implemented with the same name.
Types Of Polymorphism
There are two types of polymorphism:
Compile Time Polymorphism (Early Binding or Overloading or static binding)
Compile time polymorphism means we will declare a method with same name and different parameter/signature because of this we will perform different tasks with same method name in the same class is called compile time polymorphism.
Runtime polymorphism (Late Binding or Overriding or dynamic binding)
Runtime polymorphism means we will declare a method with same name and same parameter/signature in the different class is called runtime polymorphism.
In the runtime polymorphism we can override a method in base class by creating a similar function in derived class this can be achieved by using inheritance principle and using virtual & override keywords. We can declare methods with the virtual keyword then you can override those methods using virtual keyword in the derived class.
Child class method
I hope, you enjoyed reading this article. Your valuable feedback, questions or comments about this article are always welcome.