Polymorph Is M
Polymorph Is M
Polymorphism means many forms (ability to take more than one form). In Polymorphism
poly means “multiple” and morph means “forms” so polymorphism means many forms.
In polymorphism we will declare methods with same name and different parameters in same
class or methods with same name and same parameters in different classes. Polymorphism
has ability to provide different implementation of methods that are implemented with same
name.
Overloading
Overloading means we will declare methods with same name but different signatures
because of this we will perform different tasks with same method name. This overloading
also called as compile time polymorphism or early binding.
Method Overloading or compile time polymorphism means same method names with
different signatures (different parameters)
Overriding
In this method overriding or run time polymorphism we can override a method in base class
by creating similar function in derived class this can be achieved by using inheritance
principle and using “virtual & override” keywords.
Polymorphism
Polymorphism means many forms (ability to take more than one form). In Polymorphism
poly means “multiple” and morph means “forms” so polymorphism means many forms.
In polymorphism we will declare methods with same name and different parameters in same
class or methods with same name and same parameters in different classes. Polymorphism
has ability to provide different implementation of methods that are implemented with same
name.
Compile time polymorphism means we will declare methods with same name but different
signatures because of this we will perform different tasks with same method name. This
compile time polymorphism also called as early binding or method overloading.
Method Overloading or compile time polymorphism means same method names with
different signatures (different parameters)
Example
Run time polymorphism also called as late binding or method overriding or dynamic
polymorphism. Run time polymorphism or method overriding means same method names
with same signatures.
In this run time polymorphism or method overriding we can override a method in base class
by creating similar function in derived class this can be achieved by using inheritance
principle and using “virtual & override” keywords.
In base class if we declare methods with virtual keyword then only we can override those
methods in derived class using override keyword
Example
//Base Class
public class Bclass
{
public virtual void Sample1()
{
Console.WriteLine("Base Class");
}
}
// Derived Class
public class DClass : Bclass
{
public override void Sample1()
{
Console.WriteLine("Derived Class");
}
}
// Using base and derived class
class Program
{
static void Main(string[] args)
{
// calling the overriden method
DClass objDc = new DClass();
objDc.Sample1();
// calling the base class method
Bclass objBc = new DClass();
objBc.Sample1();
}
}
If we run above code we will get output like as shown below
Output
----------------------------------
Derived Class
Derived Class
Arrays
Arrays are strongly typed collection of same datatype and these arrays are fixed length that
cannot be changed during runtime. Generally in arrays we will store values with index basis
that will start with zero. If we want to access values from arrays we need to pass index
values.
Declaration of Arrays
Generally we will declare arrays with fixed length and store values like as shown below
Arraylists
Array lists are not strongly type collection. It will store values of different datatypes or same
datatype. Array list size will increase or decrease dynamically it can take any size of values
from any data type. These Array lists will be accessible with “System.Collections”
namespace
Declaration of Arraylist
To know how to declare and store values in array lists check below code
Arrays ArrayLists
These are strong type collection and allow Array Lists are not strong type collection
to store fixed length and size will increase or decrease
dynamically
In arrays we can store only one datatype In arraylist we can store all the datatype
either int, string, char etc… values
A view represents a virtual table. By using view we can join multiple tables and present the
data as coming from a single table.
Stored Procedure
A stored procedure is a group of sql statements that has been created and stored in the
database. Stored procedure will accept input parameters so that a single procedure can be
used over the network by several clients using different input data. Stored procedure will
reduce network traffic and increase the performance. If we modify stored procedure all the
clients will get the updated stored procedure
USE AdventureWorks2008R2;
GO
CREATE PROCEDURE dbo.sp_who
AS
SELECT FirstName, LastName FROM Person.Person;
GO
EXEC sp_who;
EXEC dbo.sp_who;
GO
DROP PROCEDURE dbo.sp_who;
GO
Q: Method Overloading ?
Ans:
Method overloading means having two or more methods with the same name but different
signatures in the same scope. These two methods may exist in the same class or anoter one in
base class and another in derived class.
class Person
{
Person()
this.firstName = "";
this.lastName = "";
Person(String FirstName)
this.firstName = FirstName;
this.lastName = "";
this.firstName = FirstName;
this.lastName = LastName;
Person(userFirstName); // as a constructor and call method with one parameter(like User's first
Name)
Person(userFirstName,userLastName); // as a constructor and call method with one
parameter(like User's first Name)
Generally, you should consider overloading a method when you have required same reason that
take different signatures, but conceptually do the same thing.
-----------------------------------------------------------------------------------------
Q: Method Overriding?
Ans:
Method overriding means having a different implementation of the same method in the
inherited class. These two methods would have the same signature, but different
implementation. One of these would exist in the base class and another in the derived class.
These cannot exist in the same class.
Overriding methods
In a derived class, if you include a method definition that has the same name and exactly the
same number and types of parameters as a method already defined in the base class, this new
definition replaces the old definition of the method.
Explanation
A subclass inherits methods from a superclass. Sometimes, it is necessary for the subclass to
modify the methods defined in the superclass. This is referred to as method overriding. The
following example demonstrates method overriding.
Step 1
class Circle
{
When the getArea method is invoked from an instance of the Circle class, the method returns the
area of the circle.
Step 2
The next step is to define a subclass to override the getArea() method in the Circle class. The
derived class will be the Cylinder class. The getArea() method in the Circle class computes the
area of a circle, while the getArea method in the Cylinder class computes the surface area of a
cylinder.
When the overriden method (getArea) is invoked for an object of the Cylinder class, the new
definition of the method is called and not the old definition from the superclass(Circle).
Example code
Circle myCircle;
myCircle = new Circle(1.20);
Cylinder myCylinder;
myCylinder = new Cylinder(1.20,2.50);