OOPS Concepts in C# with Example

 

pg4

In IT most of the programming languages support OOPS. OOPS is basic for an IT employee.

It is mandatory to know about OOPS. For a developer without OOPS interview question interview is not going to end. So here in this blog will discuss OOPS concepts and related some interview questions.


OOPS Concepts are as follows:

1. Abstraction

2. Encapsulation

3. Inheritance

4. Polymorphism


before going to OOPS concept first we should know about class and object.

Class: It is a blueprint of the properties of an object. when we create a class no memory will be allocation in storage.

Object: An object is created by class. when we create an object memory will create in in memory to store the class properties of an object.


1. Abstraction:

In Abstraction we show only the details which we need to show to user and hide the non essential details. We do this by implementing the logic in child class.

In Real time abstraction class will have a method with only definition in class and the child class will implement the logic of whatever logic we have. By this we can achieve abstraction.

Example:
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
abstractclass2 abstract2= new abstractclass2();
abstract2.getprint();
abstractclass1 abstract3= new abstractclass2();
abstract3.getprint();
}
}

public abstract class abstractclass1{
  public abstract void  getprint();//we need to use abstract keyword to the method we implement in child
}
public  class abstractclass2:abstractclass1{
  public override void  getprint(){//we need to use override keyword to implement the abstract method.
  Console.WriteLine("Hello World from abstract method");
  }
}

Output:
Hello World
Hello World from abstract method
Hello World from abstract method


2. Encapsulation:

In Encapsulation providing only essential details to the user and hide the data which is not required to user. We do this by using access specifiers.

Below are the access specifiers in C#:
  • Public
  • Private
  • Protected
  • Internal
  • Protected Internal
Public: This members have no restriction everywhere you can access public members.

Private: It is specific to a class it defined in. by default all class members are private.

Protected: It is accessible with in class and the classes that are inherited from this class.

Internal: It is accessible with in the current project assembly it defined in.

Protected Internal: It is accessible with in the current project assembly and the classes that are derived from this assembly. All the derived classes have the access to this members.

By using access specifiers we can define the scope of a object member or object method.

Only public internal specifiers are allowed for classes in a namespace. Other specifiers use for members of a class


pg1


3. Inheritance:

In Inheritance properties of a class are shared by child class. base class is the class which is been inherited by child class and all base class properties will be available in child class except private members.

In Real time we need this inheritance concept to avoid writing same property again and again reusability.

Example:
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
ChildClass abstract2= new ChildClass();
abstract2.getprint();
BaseClass abstract3= new ChildClass();
abstract3.getprint();
}
}

public  class BaseClass{
  public virtual void  getprint(){
  }
}
public  class ChildClass:BaseClass{
  public  override void  getprint(){
  Console.WriteLine("Hello World from abstract method");
  }
}

4. Polymorphism:

Polymorphism is ability of an object take on many forms. one thing many forms.

we have 2 types:
  • Method Overloading
  • Method Overriding


Method Overloading:

In Same class multiple methods with same signature. same method name different parameters. return type is not considered in method signature check.

Example:
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
BaseClass abstract2= new BaseClass();
abstract2.getprint();
abstract2.getprint("Method with parameter");
}
}

public  class BaseClass{
  public  void  getprint(){
  Console.WriteLine("Method without parameter");
  }
  public  void  getprint(string a){
Console.WriteLine(a);
  }
}


Method Overriding:

Method Overriding is achieved by declaring same signature methods in base class and child class.
child class method will override the base class method.

Example:
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
ChildClass abstract2= new ChildClass();
abstract2.getprint();
BaseClass abstract3= new ChildClass();
abstract3.getprint();
}
}

public  class BaseClass{
  public virtual void  getprint(){
  }
}
public  class ChildClass:BaseClass{
  public  override void  getprint(){
  Console.WriteLine("Hello World from abstract method");
  }
}


Thanks for Reading.

Popular blogs

Post a Comment

0 Comments