0% found this document useful (0 votes)
3 views9 pages

Polymorph Is M

Polymorphism refers to the ability to define methods with the same name but different parameters in the same class (overloading) or in different classes (overriding). Overloading is compile-time polymorphism, while overriding is runtime polymorphism, allowing derived classes to provide specific implementations of methods defined in base classes. Additionally, the document discusses arrays and ArrayLists, highlighting their differences, and explains the concepts of views and stored procedures in SQL Server.

Uploaded by

Kumar Gautam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Polymorph Is M

Polymorphism refers to the ability to define methods with the same name but different parameters in the same class (overloading) or in different classes (overriding). Overloading is compile-time polymorphism, while overriding is runtime polymorphism, allowing derived classes to provide specific implementations of methods defined in base classes. Additionally, the document discusses arrays and ArrayLists, highlighting their differences, and explains the concepts of views and stored procedures in SQL Server.

Uploaded by

Kumar Gautam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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.

In Polymorphism we have 2 different types those are

- Overloading (Called as Early Binding or Compile Time Polymorphism or static binding)

- Overriding (Called as Late Binding or Run Time Polymorphism or dynamic binding)

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)

For more details check this link polymorphism in c#

Overriding

Overriding also called as run time polymorphism or late binding or dynamic


polymorphism. Method overriding or run time polymorphism means same method names
with same signatures.

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.

In Polymorphism we have 2 different types those are

- Compile Time Polymorphism (Called as Early Binding or Overloading or static binding)


- Run Time Polymorphism (Called as Late Binding or Overriding or dynamic binding)

Compile Time Polymorphism

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

public class Class1


{
public void NumbersAdd(int a, int b)
{
Console.WriteLine(a + b);
}
public void NumbersAdd(int a, int b, int c)
{
Console.WriteLine(a + b + c);
}
}
In above class we have two methods with same name but having different input parameters
this is called method overloading or compile time polymorphism or early binding.

Run Time Polymorphism

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

string[] arr=new string[2];


arr[0] = "welcome";
arr[1] = "Aspdotnet-suresh";
In above code I declared array size 2 that means we can store only 2 string values in array.

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

ArrayList strarr = new ArrayList();


strarr.Add("welcome"); // Add string values
strarr.Add(10); // Add integer values
strarr.Add(10.05); // Add float values
If you observe above code I haven’t mentioned any size in array list we can add all the
required data there is no size limit and we will use add method to bind values to array list

Difference between Array and ArrayList

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

Arrays belong to System.Array namespace Arraylist belongs to System.Collection


namespaces

View in SQL Server

A view represents a virtual table. By using view we can join multiple tables and present the
data as coming from a single table.

For example consider we have two tables

1) UserInformation table with columns userid, username


2) SalaryInformation table with columns salid, userid, salary

Create VIEW by joining above two tables

CREATE VIEW VW_UserInfo


AS
BEGIN
SELECT a.userid,a.username,b.salary from UserInformation a INNER JOIN SalaryInformation
b ON a.userid=b.userid
END
By using above view we can get username or salary based on userid for that we need to
create procedure like as shown below
CREATE PROCEDURE GetUserInfo
@uid INT
AS
BEGIN
SELECT username from VW_UserInfo WHERE userid=@uid
END
If you observe above procedure it’s like getting username from single table (VW_UserInfo)
by passing userid

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

Sample of creating 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

METHOD Overload Vs. Overriding.

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
{

private String firstName;

private String lastName;

Person()

this.firstName = "";

this.lastName = "";

Person(String FirstName)

this.firstName = FirstName;

this.lastName = "";

Person(String FirstName, String LastName)

this.firstName = FirstName;

this.lastName = LastName;

Calling Overloaded Methods.

Person(); // as a constructor and call method without parameter

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)

When to use Method Overloading?

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

Overriding method definitions

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

In this example we will define a base class called Circle

class Circle
{

//declaring the instance variable


protected double radius;

public Circle(double radius)


{
this.radius = radius;
}

// other method definitions here

public double getArea()


{
return Math.PI*radius*radius;

}//this method returns the area of the circle

}// end of 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.

The Cylinder class is defined below.

class Cylinder extends Circle


{

//declaring the instance variable

protected double length;

public Cylinder(double radius, double length)


{
super(radius);
this.length = length;
}

// other method definitions here

public double getArea()


{
// method overriden here
return 2*super.getArea()+2*Math.PI*radius*length;
}//this method returns the cylinder surface area

}// end of class 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

This is the code to instantiate the above two classes

Circle myCircle;
myCircle = new Circle(1.20);
Cylinder myCylinder;
myCylinder = new Cylinder(1.20,2.50);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy