We Can Implement Polymorphism In C #
Through Method Overloading and Method
Overriding
POLYMORPHISM
12/7/2015 1
Method Overloading & Method Overriding
 Public class Drawing Object
 {
 public void Draw()
 {
 Console .Write Line(“ I’m just
a generic drawing object.”);
 }
 }
POLYMORPHISM
Shows the drawing object class this
will be the base class for other
objects to inherit from ,It has a
single method named Draw() .the
Draw Method has a virtual Modifier
.This virtual Modifier indicates to
derived classes that they can
override this method
12/7/2015 2
 Public class Line: Drawing Object
 {
 public override void Draw()
 {
 Console .Write Line(“ I’m a Line.”);
 }
 }
 Public class Circle : Drawing Object
 {
 public override void Draw()
 {
 Console .Write Line (“ I’m a Circle .”);
METHODOVERLOADING&
METHODOVERRIDING
12/7/2015 3
Public class Square :
Drawing Object
{
 public override void Draw()
{
Console .Write Line(“ I’m a
Square.”);
}
}
Method
Overloading
& Method
Overriding
12/7/2015 4
 Class Main
 {
 Public static void main()
 {
 Drawing ObjdObj=newDrawingObj();
 dObj.Draw();
 DrawingObject li=new Line();
 Li.Draw();
 DrawingObject cr=new Circle();
 Cr.Draw();
 DrawingObject sq=newSquare();
 Sq.Draw();
 Console.ReadKey();
 }
 }
 }
Method
Overloading
& Method
Overriding
12/7/2015 5
An abstract class can
implement methods
An abstract class can
contain fields
An abstract class can
contain constructors
or destructors
An interface cannot
implement methods
An interface can
inherit from another
class
An interface can
contain signature
ABSTRACT CLASS & INTERFACE
12/7/2015 6
An abstract class
cannot be inherited
from by a class .
Abstract class cannot
support multiple
inheritances
Interface cannot
contain constructors
or destructors
Interface can
support multiple
inheritances
ABSTRACT CLASS & INTERFACE
12/7/2015 7
An interface look like a class but has no
implementation.The only things that they
contain are only signature of methods events
and or properties .the reason interface only
provide declarations is because they are
inherit by classes and which provide
implementation for each interface member
declared
INTERFACE
12/7/2015 8
An Abstract class has no implementation like
interface . Implementation of methods can be
use by the child class
ABSTRACT CLASS
12/7/2015 9

Polymorphism (2)

  • 1.
    We Can ImplementPolymorphism In C # Through Method Overloading and Method Overriding POLYMORPHISM 12/7/2015 1
  • 2.
    Method Overloading &Method Overriding  Public class Drawing Object  {  public void Draw()  {  Console .Write Line(“ I’m just a generic drawing object.”);  }  } POLYMORPHISM Shows the drawing object class this will be the base class for other objects to inherit from ,It has a single method named Draw() .the Draw Method has a virtual Modifier .This virtual Modifier indicates to derived classes that they can override this method 12/7/2015 2
  • 3.
     Public classLine: Drawing Object  {  public override void Draw()  {  Console .Write Line(“ I’m a Line.”);  }  }  Public class Circle : Drawing Object  {  public override void Draw()  {  Console .Write Line (“ I’m a Circle .”); METHODOVERLOADING& METHODOVERRIDING 12/7/2015 3
  • 4.
    Public class Square: Drawing Object {  public override void Draw() { Console .Write Line(“ I’m a Square.”); } } Method Overloading & Method Overriding 12/7/2015 4
  • 5.
     Class Main {  Public static void main()  {  Drawing ObjdObj=newDrawingObj();  dObj.Draw();  DrawingObject li=new Line();  Li.Draw();  DrawingObject cr=new Circle();  Cr.Draw();  DrawingObject sq=newSquare();  Sq.Draw();  Console.ReadKey();  }  }  } Method Overloading & Method Overriding 12/7/2015 5
  • 6.
    An abstract classcan implement methods An abstract class can contain fields An abstract class can contain constructors or destructors An interface cannot implement methods An interface can inherit from another class An interface can contain signature ABSTRACT CLASS & INTERFACE 12/7/2015 6
  • 7.
    An abstract class cannotbe inherited from by a class . Abstract class cannot support multiple inheritances Interface cannot contain constructors or destructors Interface can support multiple inheritances ABSTRACT CLASS & INTERFACE 12/7/2015 7
  • 8.
    An interface looklike a class but has no implementation.The only things that they contain are only signature of methods events and or properties .the reason interface only provide declarations is because they are inherit by classes and which provide implementation for each interface member declared INTERFACE 12/7/2015 8
  • 9.
    An Abstract classhas no implementation like interface . Implementation of methods can be use by the child class ABSTRACT CLASS 12/7/2015 9