Object Oriented Programming Concepts in C#
We have achieved the many features in programming world by using the Object Orient Programming. Especially in .NET, it is fully object oriented one. We have lot of new concepts in both C# and VB.NET, introduce by the Microsoft.
An overview on Object Oriented Programming Concepts in C#
Below are the list of the Object oriented concepts and explanation.
Abstraction
Encapsulation
Object
Polymorphism
Method Overloading
Method Overriding
Abstract class
Interface
Sealed class
New Modifier
Now let us discuss one by one feature with an example. Abstraction:
It is one of the greatest features provided by .NET. Abstraction means to show only the necessary details to the client of the object and hiding unessential features out of an Object. It is a technique that helps to identify which specific information should be visible and which information should be hidden of an object.Encapsulation:
It is one of the fundamental principle of Object oriented Programs. An Encapsulation is the procedure of covering up of the data into single unit called Class. We can say an encapsulated object is often called an abstract data type.
Why we use the encapsulation is that an objects internal data should not be directly accessible to outside of the world. We can use the Accessor and mutetor methods to access them. We have the keywords public, private, internal and protected used to develop the encapsulation in C#.
Below is the example for encapsulation using the accessor and mutators in c#: Encapsulation using the accessor and mutators :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Encapsulation
{
class myProgram
{
static void Main(string[] args)
{
myProgram myObj = new myProgram();
myObj.SetStudentname("Tony");
Console.WriteLine("Student Name is :" + myObj.GetStudentname());
Console.Read();
}
private string studentName;
// Accessor.
public string GetStudentname()
{
return studentName;
}
// Mutator.
public void SetStudentname( string a)
{
studentName=a;
}
}
} Encapsulation by using the Properties:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Encapsulation
{
class myProgram
{
static void Main(string[] args)
{
myProgram d = new myProgram();
d.studentName = "Tony";
Console.WriteLine("The Department is :{0}", d.StudentName);
Console.Read();
}
private string studentName;
public string StudentName
{
get
{
return studentName;
}
set
{
studentName = value;
}
}
}
} Object:
As we know a class or struct is a blue print that declares what the type can do. An object is basically blocked of memory that has been allocate and configured for the class blue print. We can create an object for the class at runtime and access as you like. The creating the object is also called the instance. A general program may have the many objects and the object is interacting dynamically.
Below is the sample program to create the object in C#.
class myProgram
{
static void Main(string[] args)
{
BaseClass myObject = new BaseClass(); //Creating an object or instance for the BaseClass.
myObject.hello();
Console.Read();
}
public class BaseClass
{
public virtual void hello()
{
Console.WriteLine("Hello from Base Class");
}
}
} Polymorphism:
Polymorphism means same operation may behave differently on different classes. The compile Time Polymorphism is Method Overloading and Run Time Polymorphism: Method Overriding. Method Overloading:
Method with same name but the passing parameters may be different, it forms compile time polymorphism. The method name should be same but the parameters and return types may be different. Please go through the below code snippet. In myStudentName class having three getName methods but those input parameters are different, this is only called the Method OverLoading.
class myStudentName
{
public void getName()
{
Console.WriteLine("Hello");
}
public void getName(string stdentName)
{
Console.WriteLine("Hello {0}", stdentName);
}
public void getName(string stdentName, int studentNumber)
{
Console.WriteLine("Hello {0},{1}", stdentName,studentNumber);
}
} Method Overriding:
It is a concept to just like as virtual function in c++. Method overriding is a feature that allows you to invoke a method which have the same signature but in the different classes, but these classes should be inherited that is should be in same hierarchy.
"Virtual" is key word to use in the base class that is stating that the class is to override in the derived class. By default methods in the C# are not virtual. "Override" is a keyword to use in the derived class to hide the base class implementation on the child class.
class myProgram
{
static void Main(string[] args)
{
Derived objParent = new Derived();
objParent.hello();//Output is "Hello from Derived class"
Console.Read();
}
public class BaseClass
{
public virtual void hello()
{
Console.WriteLine("Hello from Base Class");
}
}
public class Derived : BaseClass
{
public override void hello()
{
Console.WriteLine("Hello from Derived class");
}
}
}
From the above program we have base class and Derived class, in the base class has a hello method and derived class have a hello method with same signature. Here Derived class inherited the Base class functionality but that hides the base class functionality and implemented its own functionality. This concept is called the Method Overriding. Abstract class:
In the object oriented programming one of best feature is Abstract class. It is just like as a class but we cannot create an object or instance for it. An abstract class can have both concrete and abstract methods, but abstract methods should not be implemented. We can have an implementation for concrete members. The basic Idea of implementing none instanced Abstract class is to enforce the base class behavior and functionality to the derived class. What I mean is the abstract member in base class should compulsory implement in the all derived classes.
Below is the code to implement the sample abstract class.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
myDerivedClass bojDerived = new myDerivedClass();
int sum = bojDerived.AddTwoNumbers(100, 2,30);
int multiplication = bojDerived.MultiplyTwoNumbers(100, 2, 30);
}
abstract class myAbstractClass
{
//A concrete method
public int AddTwoNumbers(int Num1, int Num2, int Num3)
{
return Num1 + Num2 + Num3;
}
//An abstract method, which should be overridden in derived class
public abstract int MultiplyThreeNumbers(int Num1, int Num2,int Num3);
}
//A Child Class of myAbstractClass
class myDerivedClass : myAbstractClass
{
public override int MultiplyThreeNumbers(int Num1, int Num2,int Num3)
{
return Num1 * Num2*Num3;
}
}
}
} Interface:
It is just like as abstract class, don't have to create the object or instance. We should have only the abstract members in it. It is used to create the multiple inheritance functionality in C#.
The sample Interface program looks like as below code snippet.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
interface IMyValue
{
int val1
{
get;
set;
}
int val2
{
get;
set;
}
}
class MyDerived : IMyValue
{
private int _val1;
private int _val2;
public MyDerived(int x, int y)
{
_val1 = x;
_val2 = y;
}
// Property implementation:
public int val1
{
get
{
return _val1;
}
set
{
_val1 = value;
}
}
public int val2
{
get
{
return _val2;
}
set
{
_val2 = value;
}
}
}
class MainClass
{
static void MyPrintPoint(IMyValue p)
{
Console.WriteLine("x={0}, y={1}", p.val1, p.val2);
}
static void Main()
{
MyDerived p = new MyDerived(2, 3);
Console.Write("My Point: ");
PrintPoint(p);
}
}
} Sealed class:
It is just like a normal class but we cannot inherit it. A calls can be prevent from inherit by adding sealed key word to a class. The basic idea of sealed class is to avoid the inheritance from the other classes.
Below is the sample program for the sealed class.
class myProgram
{
static void Main(string[] args)
{
myadd sealedClass= new myadd();
int total = sealedClass.Add(40, 50);
Console.WriteLine("Grand Total is = " + total.ToString());
Console.Read();
}
sealed class myadd
{
public int Add(int x, int y)
{
return x + y;
}
}
} New Modifier:
Why we use the new key word as a modifier is to explicitly hide a member inherited from a base class. If you want to hide an inherited member, declare it in the derived class using the same name and modify tit with the new modifier.
To hide the base class implementation from the derived class we use new modifier in C#. Below is the sample demonstration on New.
class myProgram
{
static void Main(string[] args)
{
Derived d=new Derived();
d.hello();//OutPut : Hello from Derived class
Console.Read();
}
}
public class BaseClass
{
public void hello()
{
Console.WriteLine("Hello from Base Class");
}
}
public class Derived : BaseClass
{
new public void hello()
{
Console.WriteLine("Hello from Derived class");
}
}
}