Object
- Instance if class
- It is a real time Entity.
- combination of variables, functions, and data structures.
- Object are Blue print of class
Ex: student objstudent=new student ();
Class
- Collection of Objects.
Ex: public class student{ }
- Encapsulation is a process of binding the data members and member functions into a single unit.
- Abstraction is a process of hiding the implementation details and displaying the essential features.
Inheritance
- Inheritance is a process of deriving the new class from already existing class
//Base Class
class A
{
public void fooA()
{
//TO DO:
}
}
//Derived Class
class B : A
{
public void fooB()
{
//TO DO:
}
}
//Derived Class
class C : B
{
public void fooC()
{
//TO DO:
}
}
-Compile time polymorphism is method and operators overloading. It is also called early binding. In method overloading method performs the different task at the different input parameters.
EX:
using System;
namespace method_overloading_polymorphism
{
Class Program
{
Public class Shape
{
Public void Area (float r)
{
float a = (float)3.14 * r; // here we have used function overload with 1 parameter.
Console.WriteLine ("Area of a circle: {0}",a);
}
Public void Area(float l, float b)
{
float x = (float)l* b; // here we have used function overload with 2 parameters.
Console.WriteLine ("Area of a rectangle: {0}",x); }
public void Area(float a, float b, float c)
{
float s = (float)(a*b*c)/2; // here we have used function overload with 3 parameters.
Console.WriteLine ("Area of a circle: {0}", s);
}
}
Static void Main (string[] args)
{
Shape ob = new Shape ();
ob.Area(2.0f);
ob.Area(20.0f,30.0f);
ob.Area(2.0f,3.0f,4.0f);
Console.ReadLine ();
}
}
}
2.Runtime Time Polymorphism
Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding. When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same prototype.
EX:// Base class
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");
}
}// Using base and derived class
public class Sample
{
public void TestMethod()
{// calling the overriden method
DerivedClass objDC = new DerivedClass();
objDC.Method1(); // calling the baesd class method
BaseClass objBC = (BaseClass)objDC;
objDC.Method1();
}
}
Output
Derived Class Method
Derived Class Method
Constructors
- Constructors do not have return types
- Constructors Name Same as Class Name
- Constructors cannot be declared with the keyword virtual.
Destructors
- Destructors do not have return types
- Destructors Name Same as Class Name
- using(~) symbol
Interface
- C# does not support Multiple Inherientane. so we are using Interafe.
- Contains Methods, Property, Events, Indexes
Public -- Accessible outside the class through object reference.
Private -- Accessible inside the class only through member functions.
Protected -- Just like private but Accessible in derived classes also through member functions.
Internal -- Visible inside the assembly. Accessible through objects.
Protected Internal -- Visible inside the assembly through objects and in derived classes outside the assembly through member functions.
- Instance if class
- It is a real time Entity.
- combination of variables, functions, and data structures.
- Object are Blue print of class
Ex: student objstudent=new student ();
Class
- Collection of Objects.
Ex: public class student{ }
Encapsulations
- Encapsulation is a process of hiding complex process out side the world make your object simple.
Abstraction
Abstraction
- Abstraction is a process of hiding the implementation details and displaying the essential features.
Inheritance
- Inheritance is a process of deriving the new class from already existing class
- When Class have Property of another class called Inheritance
- Process of Object Re-usability
Types of Inheritance
1.Single inheritance
//Base Class
class A
{
public void fooA()
{
//TO DO:
}
}
//Derived Class
class B : A
{
public void fooB()
{
//TO DO:
}
}
2.Multi-level inheritance
class A
{
public void fooA()
{
//TO DO:
}
}
//Derived Class
class B : A
{
public void fooB()
{
//TO DO:
}
}
//Derived Class
class C : B
{
public void fooC()
{
//TO DO:
}
}
3.Multiple inheritance
//Base Class
4.Multipath inheritance
5.Hierarchical inheritance
6.Hybrid inheritance
Polymorphism
Polymorphism
- one name many forms, Polymorphism means many forms.
- one function behaviors different forms
- many forms of single object called Polymorphism
- When a message can be processed in different ways is called polymorphism.
two types of Polymorphism :
- Compile time polymorphism/Overloading
- Runtime polymorphism/Overriding
-Compile time polymorphism is method and operators overloading. It is also called early binding. In method overloading method performs the different task at the different input parameters.
EX:
namespace method_overloading_polymorphism
Class Program
{
Public class Shape
{
Public void Area (float r)
{
float a = (float)3.14 * r; // here we have used function overload with 1 parameter.
Console.WriteLine ("Area of a circle: {0}",a);
}
Public void Area(float l, float b)
{
float x = (float)l* b; // here we have used function overload with 2 parameters.
Console.WriteLine ("Area of a rectangle: {0}",x); }
public void Area(float a, float b, float c)
{
float s = (float)(a*b*c)/2; // here we have used function overload with 3 parameters.
Console.WriteLine ("Area of a circle: {0}", s);
}
}
Static void Main (string[] args)
{
Shape ob = new Shape ();
ob.Area(2.0f);
ob.Area(20.0f,30.0f);
ob.Area(2.0f,3.0f,4.0f);
Console.ReadLine ();
}
}
}
2.Runtime Time Polymorphism
Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding. When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same prototype.
EX:// Base class
public class BaseClass
Console.Write("Base Class Method");
}
// Derived class
public class DerivedClass : BaseClass
public override void Method1()
Console.Write("Derived Class Method");
}// Using base and derived class
public class Sample
public void TestMethod()
DerivedClass objDC = new DerivedClass();
BaseClass objBC = (BaseClass)objDC;
}
}
Output
Derived Class Method
Derived Class Method
Constructors
- Constructors do not have return types
- Constructors Name Same as Class Name
- Constructors cannot be declared with the keyword virtual.
Destructors
- Destructors do not have return types
- Destructors Name Same as Class Name
- using(~) symbol
- Contains Methods, Property, Events, Indexes
Access Specifiers
Public -- Accessible outside the class through object reference.
Private -- Accessible inside the class only through member functions.
Protected -- Just like private but Accessible in derived classes also through member functions.
Internal -- Visible inside the assembly. Accessible through objects.
Protected Internal -- Visible inside the assembly through objects and in derived classes outside the assembly through member functions.