Basic Object Oriented Programming (OOP) Concepts

Basic Object Oriented Programming (OOP) Concepts


Class


A class is basically a combination of a set of rules on which we will work in a specific program. It contains definitions of new data types like fields or variables, operations that we will perform on data (methods) and access rules for that data.

Example
  1. class Example    
  2. {    
  3.     /* fields,  
  4.     Variables,  
  5.     Methods,  
  6.     Properties,  
  7.     */    
  8. }   

Object

Objects are defined as an instance of a class. Objects are built on the model defined by the class. An object can be created in memory using the "new" keyword. In C# objects are reference types and other data type variables are value types. In C# objects are stored in the heap while other value types are stored in the stack.

Example

  1. Example exmplObject = new Example();   

Inheritance


Inheritance is relevant due to the concept of “Code Reusability”. Inheritance is a feature by which a class acquires attributes of another class. The class that provides its attributes is known as the base class and the class that accepts those attributes is known as a derived class. It allows programmers to enhance their class without reconstructing it.

Example
  1. class BaseClass  
  2. {  
  3. }  
  4. class DerivedClass : BaseClass  
  5. {  
  6. }  

Now an object of a derived class can use the accessible properties of the base class without defining it in the derived class.

Encapsulation


Encapsulation is defined as hiding irrelevant data from the user. A class may contain much information that is not useful for an outside class or interface. The idea behind this concept is “Don’t tell me how you do it. Just do it.”.

So classes use encapsulation to hide its members that are not relevant for an outside class or interface. Encapsulation can be done using access specifiers.

Abstraction


Abstraction is defined as showing only the relevant data to the user. Generally abstraction and encapsulation are explained in confusing manners.

Example

So just take an example of Mobile Phone Design.

Relevant Features of a phone in which the user is interested are Camera, Music player, Calling function, Voice recording and so on.

Irrelevant Features of a phone in which the user is not interested are circuit board design, hardware used and so on.

So in designing the Mobile Phone Model, both relevant and irrelevant features are required. But we need to show only relevant features. So we design a Mobile Phone in such a way that only relevant features are shown and irrelevant features are hidden. And remember one thing, that deciding relevant and irrelevant features is totally a user choice.

Polymorphism


Polymorphism is defined as the implementation of the same method with different attributes in a different context.

Example

For example, we have a class named shape with a method name buildshape(). We need to use it in the class Circle, class triangle and the class quadrilateral. So for every class we use the buildshape() method but with different attributes.


Similar Articles