Monday, June 17, 2013

OOPS concept in C#

1.Class:
  • Class is group of object that share common properties and relationships.
  • Class members are private by default.
  • Classes are reference type that is they are stored on heap.
  • Class can be inherited and but can be instantiated.
Class Employee
{
public in CustID;
public in CustName;
}
2.Structure:
  • Structure is collection of different types of data types.
  • Structure members are public by default.
  • Structure are value type that is they are stored on stack.
  • Structure can not be inherited but can be instantiated.
Struct Employee
{
public int CustID;
public string name;
}
3.Object:
  • Object is basic runtime entity OR object is instance of class.
  • Object consistsof data and function together.
  • Object allows designing systems that are more robust and portable through the proper application of abstraction.
public class Student
{
public string First_Name { get; set; }
public int Weight { get; set; }
public Person(string First_Name, int Weight)
{
First_Name = first_Name;
Weight = weight;
}
//Other properties, methods, events...
}
class Program
{
static void Main()
{
Student person1 = new Student("Anil", 66);
Console.WriteLine("Student First_Name = {0} Weight = {1}", person1.First_Name, person1.Weight);
}
}
Output:
Student First_Name = Anil Weight = 66
4.Abstraction:
Abstraction is one of the principle of object oriented programming. It is used to display only necessary and essential features of an object to ouside the world.Means displaying what is necessary and encapsulate the unnecessary things to outside the world.Hiding can be achieved by using "private" access modifiers.
Note - Outside the world means when we use reference of object then it will show only necessary methods and properties and hide methods which are not necessary.
namespace Abstraction
{
public abstract class Shape
{
private float _area;
private float _perimeter;
public float Area
{
get
{
return _area;
}
set
{
_area = value;
}
}
public float Perimeter
{
get
{
return _perimeter;
}
set
{
_perimeter = value;
}
}
public abstract void CalculateArea();
public abstract void CalculatePerimeter();
}
}
Advantages of abstraction are the hiding of implementation details, component reuse, extensibility, and testability. When we hide implementation details, we reveal a cleaner, more comprehensible and usable interface to our users. We are separating our interface from our implementation, and this makes component reuse more practical. Many, if not all of the object-oriented concepts we have discussed throughout this document play a role in the abstraction principle. Working together, their end goal is the same, to produce software that is flexible, testable, maintainable, and extensible.
5.Data Encapsulation:
  • Encapsulation, in the context of C#, refers to an object's ability to hide data and behavior that are not necessary to its user.
  • Encapsulation enables a group of properties, methods and other members to be considered a single unit or object.
  • Encapsulation is also known as information hiding.
  • An encapsulated object is often called an abstract data type.
public class School
{
private string Schooldepartname;
public string SchoolDepartname
{
get
{
return Schooldepartname;
}
set
{
Schooldepartname =value;
}
}
}
public class Departmentmain
{
public static int Main(string[] args)
{
School d= new School();
d.SchoolDepartname="Communication";
Console.WriteLine("The Dept. Name is :{0}",d.SchoolDepartname);
return 0;
}
}
Output:
The Dept. Name is : Communication
Benefits of Encapsulation :
  • In Encapsulation fields of a class can be read-only or can be write-only.
  • A class can have control over in its fields.
  • A class can change data type of its fields anytime but users of this class do not need to change any code.
6. Inheritance:
  • Inheritance is a way to form new classes (instances of which are called objects) using classes that have already been defined.
  • Inheritance is employed to help reuse existing code with little or no modification.
  • The new classes, known as Sub-class or derived class, inherit attributes and behavior of the pre-existing classes, which are referred to as Super-class or Base class.
C# supports two types of Inheritance mechanisms

1) Implementation Inheritance
2) Interface Inheritance
Implementation Inheritance:
When a class (type) is derived from another class (type) such that it inherits all the members of the base type it is Implementation Inheritance.
Interface Inheritance:
When a type (class or a struct) inherits only the signatures of the functions from another type it is Interface Inheritance.
Benefits of using Inheritance:
  • Once a behavior (method) or property is defined in a super class (base class),that behavior or property is automatically inherited by all subclasses (derived class).
  • Code reusability increased through inheritance
    Inheritance provide a clear model structure which is easy to understand without much complexity.
  • Using inheritance, classes become grouped together in a hierarchical tree structure.
  • Code are easy to manage and divided into parent and child classes.
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}
public void print()
{
Console.WriteLine("Parent Class.");
}
}
public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
ChildClass child = new ChildClass();
child.print();
}
}
Output:
Parent Constructor.
Child Constructor.
Parent Class.
7. Polymorphism:
It allows you to invoke derived class methods through a base class reference during run-time. Polymorphism is extensively used in implementing Inheritance.
The Polymorphism can be classified into 2 types:
  • Compile Time Polymorphism (Method Overloading)
  • Run Time Polymorphism (Method Overriding)
public class Customer
{
public virtual void CustomerType()
{
Console.WriteLine("I am a customer");
}
}
public class CorporateCustomer : Customer
{
public override void CustomerType()
{
Console.WriteLine("I am a corporate customer");
}
}
public class PersonalCustomer : Customer
{
public override void CustomerType()
{
Console.WriteLine("I am a personal customer");
}
}
public class MainClass
{
public static void Main()
{
Customer[] C = new Customer[3];
C[0] = new CorporateCustomer();
C[1] = new PersonalCustomer();
C[2] = new Customer();
foreach (Customer CustomerObject in C)
{
CustomerObject.CustomerType();
}
}
}
Output:
I am a corporate customer.
I am a personal customer.
I am a customer.
Method Overloading ( Compile Time Polymorphism):
  • Method with same name but with different arguments is called method overloading.
  • Method Overloading forms compile-time polymorphism.
class A1
{
void hello()
{
Console.WriteLine("Hello");
}
void hello(string s)
{
Console.WriteLine("Hello {0}", s);
}
}
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.
class parent
{
virtual void hello()
{
Console.WriteLine("Hello from Parent");
}
}
class child : parent
{
override void hello()
{
Console.WriteLine("Hello from Child");
}
}
static void main()
{
parent objParent = new child();
objParent.hello();
}
Output:
Hello from Child.
8.Interface:
  • Interface is nothing but an contract of the system which can be implemented on accounts.
  • .Net doesn't support the multiple inheritance directly but using interfaces we can achieve multiple inheritance in .net.
  • An Interface can only contains abstract members and it is a reference type.
  • Interface members can be Methods, Properties, Events and Indexers. But the interfaces only contains declaration for its members.
  • Class which inherits interface needs to implement all it's methods.
  • All declared interface member are implicitly public.
class Program
{
interface BaseInterface
{
void BaseInterfaceMethod();
}
interface DerivedInterface : BaseInterface
{
void DerivedToImplement();
}
class InterfaceImplementer : DerivedInterface
{
public void DerivedToImplement()
{
Console.WriteLine("Method of Derived Interface called.");
}
public void BaseInterfaceMethod()
{
Console.WriteLine("Method of Base Interface called.");
}
}
static void Main(string[] args)
{
InterfaceImplementer er = new InterfaceImplementer();
er.DerivedToImplement();
er.BaseInterfaceMethod();
Console.Read();
}
}
Output:
Method of Derived Interface called.
Method of Base Interface called.