Early Binding
Early Binding means that our code directly interacts with the object, by directly calling its methods. Since the compiler knows the object's data type ahead of time, it can directly compile code to invoke the methods on the object. Early Binding also allows the IDE to use Intellisense to aid our development efforts; it allows the compiler to ensure that we are referencing methods that do exist and that we are providing the proper parameter values.
Late Binding
Late Binding means that our code interacts with an object dynamically at run-time. This provides a great deal of flexibility since our code literally doesn't care what type of object it is interacting with as long as the object supports the methods we want to call. Because the type of the object isn't known by the IDE or compiler, neither Intellisense nor compile time syntax checking is possible but we get unprecedented flexibility in exchange.
If we enable strict type checking by using Option Strict On at the top of our code modules, then the IDE and compiler will enforce early binding behavior. By default, Option Strict is turned Off and so we have easy access to the use of late binding within our code.
Polymorphism
Polymorphism means same operation may behave differently on different classes.
1. Compile Time Polymorphism: Method Overloading
Method with same name but with different parameters is called method overloading.
Example:
class A
{
void hello()
{
Console.WriteLine(“Hello”);
}
void hello(string s)
{
Console.WriteLine(“Hello {0}”,s);
}
}
2. Run Time Polymorphism: Method Overriding
Method overriding occurs when sub class declares a method that has the same type of parameters as the method declared by one of its super class.
Example:
Class superClass
{
virtual void hello()
{
Console.WriteLine(“Hello from Parent or super or main or base class”);
}
}
Class subClass : superClass
{
override void hello()
{
Console.WriteLine(“Hello from Child or sub or derived class”);
}
}
static void main()
{
superClass objParent = new subClass();
objParent.hello();
}
//Output
Hello from Child.
Inheritance
Creating a new class from an existing class is inheritance.
Hence, the class that is inherited is called base class and the class that does the inheritance is called derived class.
Inheritance provides a mechanism of creating hierarchies of objects.
1. Implementation Inheritance
2. Interface Inheritance
3. Multiple Interface Inheritance
Advantage:
Reusability of code - when a new class needs same members as an existing class then instead of creating those members again in new class, the new class can be created from existing class.
Example:
class baseClass
{
//statements or methods}
class derivedClass : baseClass
{
//statements or methods}
Encapsulation:
Encapsulation is hiding the internal state and behavior of an object, making your code more maintainable. In C#, you can manage encapsulation with access modifiers.
1. private
2. protected
3. internal
4. protected internal
5. public
Example:
abstract public class Contact
{
private string address, city, state, zip;
public string FullAddress()
{
string fulladdress=address+'\n'+city+';'+state+''+zip;
return fullAddress;
}
}
public class SiteOwner:Contact
{
public string FullAddress()
{
string fullAddress=base.FullAddress();
return fullAddress;
}
}
Abstraction:
Abstract classes are special type of base classes. These abstract class members are methods and properties that are declared without an implementation.
- An abstract class can never be instantiated.
- They do not have any implementation.
- They need to be overridden and implemented in a derived non-abstract class.
- An abstract class can have one or more abstract methods.
- Abstract methods are virtual.
Example:
abstract public class Contact
{
protected string name;
public Contact()
{ }
public abstract void generateReport();
abstract public string Name
{ get; set; }
}
public class Customer : Contact
{
int numberOfVisits;
public Customer()
{ }
public override void generateReport()
{ //report implementation }
public override string Name
{
get { numberOfVisits++; return name; }
set { name = value; numberOfVisits=0; }
}
}
Interface:
Interface is declared as a set of methods, properties, events and fields that are declared Public in scope.
Example:
interface IMyInterface
{
void MethodToImplement();
}
class InterfaceImplementer : IMyInterface
{
public void MethodToImplement()
{
Console.WriteLine("Method Implemented.");
}
}
Keywords:
base - access the members of the base class
this - refer the current object for which a method is called
Delegate:
Multicast Delegate:
Delegate wraps a method. Calling delegate results in calling the method. It is possible to wrap more than one method in a delegate. This is known as a multicast delegate.
Uses:
In Events, we define a method that contains code for performing various tasks when an event (such as a mouse click) takes place.
This method needs to be invoked by the runtime when the event occurs. Hence this method, that we defined, is passed as a parameter to a delegate.
Example:
At first, a delegate is defined with a specific signature (return type, parameter type and order etc). To invoke a delegate object, one or more methods are required with the EXACT same signature. A delegate object is first created similar like a class object created. The delegate object will basically hold a reference of a function. The function will then can be called via the delegate object.
Early Binding means that our code directly interacts with the object, by directly calling its methods. Since the compiler knows the object's data type ahead of time, it can directly compile code to invoke the methods on the object. Early Binding also allows the IDE to use Intellisense to aid our development efforts; it allows the compiler to ensure that we are referencing methods that do exist and that we are providing the proper parameter values.
Late Binding
Late Binding means that our code interacts with an object dynamically at run-time. This provides a great deal of flexibility since our code literally doesn't care what type of object it is interacting with as long as the object supports the methods we want to call. Because the type of the object isn't known by the IDE or compiler, neither Intellisense nor compile time syntax checking is possible but we get unprecedented flexibility in exchange.
If we enable strict type checking by using Option Strict On at the top of our code modules, then the IDE and compiler will enforce early binding behavior. By default, Option Strict is turned Off and so we have easy access to the use of late binding within our code.
Polymorphism
Polymorphism means same operation may behave differently on different classes.
1. Compile Time Polymorphism: Method Overloading
Method with same name but with different parameters is called method overloading.
Example:
class A
{
void hello()
{
Console.WriteLine(“Hello”);
}
void hello(string s)
{
Console.WriteLine(“Hello {0}”,s);
}
}
2. Run Time Polymorphism: Method Overriding
Method overriding occurs when sub class declares a method that has the same type of parameters as the method declared by one of its super class.
Example:
Class superClass
{
virtual void hello()
{
Console.WriteLine(“Hello from Parent or super or main or base class”);
}
}
Class subClass : superClass
{
override void hello()
{
Console.WriteLine(“Hello from Child or sub or derived class”);
}
}
static void main()
{
superClass objParent = new subClass();
objParent.hello();
}
//Output
Hello from Child.
Inheritance
Creating a new class from an existing class is inheritance.
Hence, the class that is inherited is called base class and the class that does the inheritance is called derived class.
Inheritance provides a mechanism of creating hierarchies of objects.
1. Implementation Inheritance
2. Interface Inheritance
3. Multiple Interface Inheritance
Advantage:
Reusability of code - when a new class needs same members as an existing class then instead of creating those members again in new class, the new class can be created from existing class.
Example:
class baseClass
{
//statements or methods}
class derivedClass : baseClass
{
//statements or methods}
Encapsulation:
Encapsulation is hiding the internal state and behavior of an object, making your code more maintainable. In C#, you can manage encapsulation with access modifiers.
1. private
2. protected
3. internal
4. protected internal
5. public
Example:
abstract public class Contact
{
private string address, city, state, zip;
public string FullAddress()
{
string fulladdress=address+'\n'+city+';'+state+''+zip;
return fullAddress;
}
}
public class SiteOwner:Contact
{
public string FullAddress()
{
string fullAddress=base.FullAddress();
return fullAddress;
}
}
Abstraction:
Abstract classes are special type of base classes. These abstract class members are methods and properties that are declared without an implementation.
- An abstract class can never be instantiated.
- They do not have any implementation.
- They need to be overridden and implemented in a derived non-abstract class.
- An abstract class can have one or more abstract methods.
- Abstract methods are virtual.
Example:
abstract public class Contact
{
protected string name;
public Contact()
{ }
public abstract void generateReport();
abstract public string Name
{ get; set; }
}
public class Customer : Contact
{
int numberOfVisits;
public Customer()
{ }
public override void generateReport()
{ //report implementation }
public override string Name
{
get { numberOfVisits++; return name; }
set { name = value; numberOfVisits=0; }
}
}
Interface:
Interface is declared as a set of methods, properties, events and fields that are declared Public in scope.
Example:
interface IMyInterface
{
void MethodToImplement();
}
class InterfaceImplementer : IMyInterface
{
public void MethodToImplement()
{
Console.WriteLine("Method Implemented.");
}
}
Abstract Class | Interface |
A class may inherit only one abstract class. | A class may implement multiple number of Interfaces. |
An abstract class member may have any access modifier. | An Interface member is public by default, and cant have any other access modifier. |
Abstract class methods may or may not have an implementation. | Methods in an Interface only have a definition, no implementation. |
Fields and constants can be defined. | No fields or constants can be defined |
Keywords:
base - access the members of the base class
this - refer the current object for which a method is called
Delegate:
C# implements the concept of function pointers using delegates. That is, passing methods to other method instead of passing data.
The method passed is known as a Callback function. Call back functions are pointers to methods.
- Delegate wraps a method.
- Delegate is a type of object very similar to classes.
- Calling a delegate results in calling the method.
- Delegate gives a name to a method signature (return type, parameter type and order etc)
Multicast Delegate:
Delegate wraps a method. Calling delegate results in calling the method. It is possible to wrap more than one method in a delegate. This is known as a multicast delegate.
Uses:
In Events, we define a method that contains code for performing various tasks when an event (such as a mouse click) takes place.
This method needs to be invoked by the runtime when the event occurs. Hence this method, that we defined, is passed as a parameter to a delegate.
Example:
At first, a delegate is defined with a specific signature (return type, parameter type and order etc). To invoke a delegate object, one or more methods are required with the EXACT same signature. A delegate object is first created similar like a class object created. The delegate object will basically hold a reference of a function. The function will then can be called via the delegate object.
Step: 1 - Defining the delegate
public delegate int Calculate (int value1, int value2);
Step: 2 - Creating methods which will be assigned to delegate object
//a method, that will be assigned to delegate objects, having the EXACT signature of the delegate
public int add(int value1, int value2)
{
return value1 + value2;
}//a method, that will be assigned to delegate objects, having the EXACT signature of the delegate
public int sub( int value1, int value2)
{
return value1 - value2;
}
//a method, that will be assigned to delegate objects, having the EXACT signature of the delegate
public int add(int value1, int value2)
{
return value1 + value2;
}//a method, that will be assigned to delegate objects, having the EXACT signature of the delegate
public int sub( int value1, int value2)
{
return value1 - value2;
}
Step: 3 - Creating the delegate object and assigning methods to those delegate objects
//creating the class which contains the methods that will be assigned to delegate objectsMyClass mc = new MyClass();
//creating delegate objects and assigning appropriate methods, having the EXACT signature of the delegate
Calculate add = new Calculate(mc.add);
Calculate sub = new Calculate(mc.sub);
//creating the class which contains the methods that will be assigned to delegate objectsMyClass mc = new MyClass();
//creating delegate objects and assigning appropriate methods, having the EXACT signature of the delegate
Calculate add = new Calculate(mc.add);
Calculate sub = new Calculate(mc.sub);
Step: 4 - Calling the methods via delegate objects
//using the delegate objects to call the assigned methods
Console.WriteLine("Adding two values: " + add(10, 6));
Console.WriteLine("Subtracting two values: " + sub(10,4));
//using the delegate objects to call the assigned methods
Console.WriteLine("Adding two values: " + add(10, 6));
Console.WriteLine("Subtracting two values: " + sub(10,4));
No comments:
Post a Comment