Class - Collection of Objects Object - Instance of A Class Encapsulation - Process of Binding Data Members and Member Functions Into Single Unit
Class - Collection of Objects Object - Instance of A Class Encapsulation - Process of Binding Data Members and Member Functions Into Single Unit
Encapsulation – process of binding data members and member functions into single unit
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class rectangle
{
public double length;
public double width;
}
}
}
Abstraction - Abstraction is a process of hiding the implementation details and displaying the essential features.
Protected -- Just like private but Accessible in derived classes also through member
functions.
Protected Internal -- Visible inside the assembly through objects and in derived classes outside the assembly through member functions.
Public Private Protected
A1 A2 A1 A2 A1 A2
C1 C3 C1 C3 C1 C3
√ √ √ × √ ×
C2 C4 C2 C4 C2:C1 C4
√ √ × × √ ×
A1 A2 A1 A2
C1 C3 C1 C3:C2
√ × √ √
C2 C4 C2 C4
√ × √ ×
Inheritance:
Inheritance is a process of deriving the new class from already existing class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Shape
{
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
protected int width;
protected int height;
}
// Derived class
class Rectangle : Shape
{
public int getArea()
{
return (width * height);
}
}
class Program
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.setWidth(9);
r.setHeight(5);
r.getArea();
Console.WriteLine(r.getArea());
}
}
}
Polymorphism:
When a message can be processed in different ways is called polymorphism. Polymorphism means many forms.
Function overloading
Operator overloading
Runtime polymorphism/Overriding – changing the functionality if a method without changing its signature
Function Overloading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Printdata
{
void print(int i)
{
Console.WriteLine("Printing int: {0}", i);
}
void print(double f)
{
Console.WriteLine("Printing float: {0}", f);
}
void print(string s)
{
Console.WriteLine("Printing string: {0}", s);
}
class Program
{
static void Main(string[] args)
{
Printdata p = new Printdata();
p.print(2);
p.print(3.6);
p.print("pavan");
Console.ReadKey();
}
}
}
}
Operator Overloading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Box
{
private double length; // Length of a box
private double width; // Width of a box
private double height; // Height of a box
class Program
{
static void Main(string[] args)
{
Box Box1 = new Box(); // Declare Box1 of type Box
Box Box2 = new Box(); // Declare Box2 of type Box
Box Box3 = new Box(); // Declare Box3 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setWidth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setWidth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}", volume);
// volume of box 2
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);
// volume of box 3
volume = Box3.getVolume();
Console.WriteLine("Volume of Box3 : {0}", volume);
Console.ReadKey();
}
}
Runtime polymorphism/Overriding
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class BaseClass
{
public virtual void Method1()
{
Console.Write("Base Class Method");
}
}
// Derived class
public class DerivedClass : BaseClass
{
public override void Method1()
{
Console.Write("Derived Class Method");
}
}
class Program
{
static void Main(string[] args)
{
// calling the overriden method
DerivedClass objDC = new DerivedClass();
objDC.Method1();
// calling the baesd class method
BaseClass objBC = (BaseClass)objDC;
objDC.Method1();
}
}
Overriding in C#
1. Virtual – This keyword is used with a base class which signifies that the method
of a base class can be overridden.
2. Override – This keyword is used with a derived class which signifies that derived
class overrides a method of a base class.
3. Base – This keyword is used in a derived class to call the base class method.
Example 1 – Without Virtual and Override Keywords
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Bird b = new Peacock();
b.fly();
Console.ReadLine();
}
}
}
Example 2 (a)- With Virtual and Override Keywords
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Bird b = new Peacock();
b.fly();
Console.ReadLine();
}
}
}
Constructors
A class constructor is a special member function of a class that is executed whenever we create new objects of that class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Line
{
private double length; // Length of a line
public Line()
{
Console.WriteLine("Object is being created");
}
class Program
{
static void Main(string[] args)
{
Line line = new Line();
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
Console.ReadKey();
}
}
}
}
Destructors
A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope. A destructor will have exact same name as the
class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Line
{
private double length; // Length of a line
public Line() // constructor
{
Console.WriteLine("Object is being created");
}
~Line() //destructor
{
Console.WriteLine("Object is being deleted");
}
public void setLength(double len)
{
length = len;
}
public double getLength()
{
return length;
}
class Program
{
static void Main(string[] args)
{
Line line = new Line();
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
}
}
}
}
Interface : should be used if you want to imply a rule on the components which may
or may not be related to each other
Abstract Class : should be used where you want to have some basic or default behavior
or implementation for components related to each other
Pros:
Interface: Allows multiple inheritance
provides abstraction by not exposing what exact kind of object is being used in the context
provides consistency by a specific signature of the contract
Cons:
Interface : Must implement all the contracts defined
cannot have variables or delegates
once defined cannot be changed without breaking all the classes
namespace ConsoleApplication1
{
abstract class M1
{
public int add(int a, int b)
{
return (a + b);
}
}
class M2 :M1
{
public int mul(int a, int b)
{
return a * b;
}
}
class Program
{
static void Main(string[] args)
{
M2 ob = new M2();
int result = ob.add(10, 20);
Console.WriteLine("the result is {0}", result);
int muls = ob.mul(10, 20);
Console.WriteLine("the result is {0}", muls);
Console.ReadLine();
}
}
}
Delegates
C# delegates are similar to pointers to functions in C or C++. A delegate is a reference type variable that holds the reference to a method.
The reference can be changed at runtime.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SealedClass sealedCls = new SealedClass();
int total = sealedCls.Add(4, 5);
Console.WriteLine("Total = " + total.ToString());
}
}
// Sealed class
sealed class SealedClass
{
public int Add(int x, int y)
{
return x + y;
}
}
}
}
Partial Class
Splitting up of a class in to two or more sub classes is called as partial classes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public partial class MyTest
{
private int a;
private int b;