Object Oriented Programming Concepts (OOPs)
When you begin your learning journey in the Information Technology Ocean, the first tide that you have to face is Object Oriented Programming Concepts aka OOPs Concepts.
In this article, I have collated the simple explanations for the concepts which for the Object Oriented Programming of almost any language which you learn in Computer Programming. OOPs concepts is the mandatory topic which any interviewer pick to begin the interview for the entry-level and mid-level positions for IT Firms.
Class
A class is the blueprint from which the individual objects are created.
C#/VB .NET allows the below ways of declaring a class by the developer which has definitive usage based on the type of class you create objects.
- Static Class:
Static classes provide some benefits over standard classes. Firstly, the compiler ensures that no instance members are added accidentally. Secondly, the program can have improved performance because the overhead of creating objects is removed.
- It contains only static members.
- It can’t be instantiated.(cant use new to create instance of class)
- It’s sealed.
- It can’t contain Instance Constructors.
- It can’t be used in a local, instance, or static variable declaration, as a generic type argument, or as the element type of an array.
2. Abstract Class:
An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a truck is a kind of vehicle, and an airplane is a kind of vehicle, so you might have a base class Vehicle and derived classes Truck and Airplane. But “Vehicle” is abstract; there are no vehicles which are just vehicles without being some more specific kind of thing.
3. Sealed Class:
The sealed keyword enables you to prevent the inheritance of a class. A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster. For example: By making Horse sealed, it is not possible to inherit from it, e.g. making classes like Pony or WorkHorse which you like to be inheriting from Horse.
4. Partial Class:
A partial class is nothing more than splitting the file of a class into multiple smaller files. A reason to do this might be to share only part of the source code to others. If the reason is that the file gets too big, think about splitting the class in smaller classes first.
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.
Abstraction
Abstraction refers to the act of representing essential features without including the background details or explanations. Abstract class provide a common definition of a base class that multiple derived classes can share.
- Only abstract classes can have abstract methods.
- An abstract class is like a normal class
- An abstract class is like an interface
- But an abstract class can’t be instantiated
When to use
Need to create multiple versions of your component since versioning is not a problem with abstract class. You can add properties or methods to an abstract class without breaking the code and all inheriting classes are automatically updated with the change. Need to to provide default behaviors as well as common behaviors that multiple derived classes can share and override.
Encapsulation
Encapsulation: is a process of binding or wrapping the data and the codes that operates on the data into a single entity called as CLASS. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper
Polymorphism
Polymorphism is a generic term that means “many shapes” polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts — specifically, to allow an entity such as a function, or an object to have more than one form.
There are two types of polymorphism:
1. Compile time polymorphism — It is functions and operators overloading.
2. Runtime time polymorphism — It is done using inheritance (Override) and virtual functions.
Method Overloading ( Compile Time Polymorphism):
Method with same name but with different arguments is called method overloading. If you use overload for method, there are couple of restrictions that the compiler imposes. The rule is that overloads must be different in their signature, which means the name and the number and type of parameters.
Method Overriding (Run Time Polymorphism):
Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass. Method overriding forms Run-time polymorphism. Note: By default functions are not virtual in C# and so you need to write “virtual” explicitly. While by default in Java each function are virtual.
Inheritance
Ability of a new class to be created, from an existing class by extending it, is called inheritance.Inheritance allows new classes to be created based upon existing classes. Eliminates the Need To re-implement common functionality The .NET Framework classes make heavy use of inheritance. All classes inherit from System.Object.
Interface
An interface contains definitions for a group of related functionalities that a class or a struct can implement. Interfaces can contain methods, properties, events, indexers, or any combination of those four member types. An interface can’t contain constants, fields, operators, instance constructors, destructors, or types. Interface members are automatically public, and they can’t include any access modifiers. Members also can’t be static.
- An interface is like an abstract base class. Any class or struct that implements the interface must implement all its members.
- An interface can’t be instantiated directly. Its members are implemented by any class or struct that implements the interface.
- Interfaces can contain events, indexers, methods, and properties.
- Interfaces contain no implementation of methods.
- A class or struct can implement multiple interfaces. A class can inherit a base class and also implement one or more interfaces.
For Example: Suppose we want a horse to be able to Jump, an interface IJumping can be created. By adding this interface to Horse, all methods in IJumping should be implemented. In IJumping itself only the declarations (e.g. StartJump and EndJump are defined), in Horse the implementations of these two methods should be added
Constructors
Whenever a class or struct is created, its constructor is called. A class or struct may have multiple constructors that take different arguments. Constructors enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read. If you do not provide a constructor for your object, C# will create one by default that instantiates the object and sets member variables to the default values as listed in Default Values Table
Types of Constructors:
- Using Constructors
- Instance Constructors
- Private Constructors
- Static Constructors
Destructors
Destructors are used to destruct instances of classes. In the .NET Framework, the garbage collector automatically manages the allocation and release of memory for the managed objects in your application. However, you may still need destructors to clean up any unmanaged resources that your application creates. There can be only one destructor for a class.