polymorphism (2)
TRANSCRIPT
![Page 1: Polymorphism (2)](https://reader036.vdocuments.mx/reader036/viewer/2022083109/58ecb1611a28abb85c8b46af/html5/thumbnails/1.jpg)
05/02/2023 1
We Can Implement Polymorphism In C # Through Method Overloading and Method
Overriding
POLYMORPHISM
![Page 2: Polymorphism (2)](https://reader036.vdocuments.mx/reader036/viewer/2022083109/58ecb1611a28abb85c8b46af/html5/thumbnails/2.jpg)
05/02/2023 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
![Page 3: Polymorphism (2)](https://reader036.vdocuments.mx/reader036/viewer/2022083109/58ecb1611a28abb85c8b46af/html5/thumbnails/3.jpg)
05/02/2023 3
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 .”);
METHOD OVERLOADING &
METHOD
OVERRID
ING
![Page 4: Polymorphism (2)](https://reader036.vdocuments.mx/reader036/viewer/2022083109/58ecb1611a28abb85c8b46af/html5/thumbnails/4.jpg)
05/02/2023 4
Public class Square : Drawing Object
{ public override void Draw()
{Console .Write Line(“ I’m a Square.”);
}}
Method Overloading &
Method Overriding
![Page 5: Polymorphism (2)](https://reader036.vdocuments.mx/reader036/viewer/2022083109/58ecb1611a28abb85c8b46af/html5/thumbnails/5.jpg)
05/02/2023 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
![Page 6: Polymorphism (2)](https://reader036.vdocuments.mx/reader036/viewer/2022083109/58ecb1611a28abb85c8b46af/html5/thumbnails/6.jpg)
05/02/2023 6
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
![Page 7: Polymorphism (2)](https://reader036.vdocuments.mx/reader036/viewer/2022083109/58ecb1611a28abb85c8b46af/html5/thumbnails/7.jpg)
05/02/2023 7
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
![Page 8: Polymorphism (2)](https://reader036.vdocuments.mx/reader036/viewer/2022083109/58ecb1611a28abb85c8b46af/html5/thumbnails/8.jpg)
05/02/2023 8
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
![Page 9: Polymorphism (2)](https://reader036.vdocuments.mx/reader036/viewer/2022083109/58ecb1611a28abb85c8b46af/html5/thumbnails/9.jpg)
05/02/2023 9
An Abstract class has no implementation like interface . Implementation of methods can be use by the child class
ABSTRACT CLASS