Download - Oop concepts
Session 3
Advanced OOP Concepts in C#
C# Simplified / Session 3 / 2 of 28
Review Parameterised constructors are constructors that take in
parameters. Constructors can be differentiated during run-time based
on the number of arguments or the type of the arguments passed.
In C#, the destructor is called by Garbage Collector. Methods can be overloaded in C# in any of the two ways.
By specifying different number of parameters By specifying different type of parameters
C# allows us to overload operators. Overloading an operator means making an operator (for
example, the addition operator, +) behave differently when applied on certain object of classes or structs.
C# Simplified / Session 3 / 3 of 28
Review – Contd… C# does not support multiple inheritances.
To override an existing method of the base class, we declare a new method in the inherited class of the same name and prefix it with the new keyword.
C# Simplified / Session 3 / 4 of 28
Objectives Discuss Polymorphism
Use Virtual Functions
Discuss relationship between polymorphism and inheritance
Discuss Abstract Base classes
Discuss the concept of Interfaces
Use Interfaces
C# Simplified / Session 3 / 5 of 28
Polymorphism is the ability of an entity to have many forms
Polymorphism and virtual functions go hand in hand
Polymorphism allows us to implement methods of the derived class during run-time
Virtual functions come in handy when we need to call the derived class method from an object of the base class
Polymorphism in C#
public class ShapeObj{ public virtual void area() { System.Console.WriteLine ("This is the Virtual Area method"); }}
C# Simplified / Session 3 / 6 of 28
Polymorphism in C# - Contd…
Beep vrooomTring-Tring
Telephone Company
Ring()Ring()
Ring()
C# Simplified / Session 3 / 7 of 28
Advantages of Polymorphism
Polymorphism focuses on writing one routine that can operate on objects of more than one class
Amount of code that needs to be written is reduced
Code is easier to understand Helps the programmer to
remember available functionality
C# Simplified / Session 3 / 8 of 28
Polymorphism in C# - Example
using System;class Parent{
public int MethodA(){
return (MethodB()*MethodC());}public virtual int MethodB(){
return(10);}public int MethodC(){
return(20);}
}
C# Simplified / Session 3 / 9 of 28
Polymorphism in C# - Example
class Child : Parent{ public override int MethodB() {
return(30); }}class PolymorphDemo{ public static void Main() { Child ObjChild = new Child(); Console.WriteLine("The output is "
+ObjChild.MethodA()); }}
C# Simplified / Session 3 / 10 of 28
Polymorphism in C# - Output
C# Simplified / Session 3 / 11 of 28
Points to Remember Polymorphism is intelligent overriding Polymorphism – decision as to which
method to call is made at runtime Polymorphism requires virtual
functions, and virtual functions in turn require method overriding
C# Simplified / Session 3 / 12 of 28
Relationship between Polymorphism and Inheritance
C# enables polymorphism through inheritance
Inheritance means a derived class gets state and behaviour of the parent class.
Polymorphism means the “right” method gets called, the one belonging to the actual object that is being referenced
Polymorphism relies on inheritance to ensure that no two classes respond identically to a particular event.
C# Simplified / Session 3 / 13 of 28
Abstract Base Classes Abstract classes are classes that can be
inherited from Abstract base classes cannot be instantiated
nor can they be sealed C# allows creation of Abstract Base classes
by an addition of the abstract modifier to the class definition
An abstract method is defined in the abstract base class and its actual implementation is written in the derived class
C# Simplified / Session 3 / 14 of 28
Abstract Base Classes - Example
using System;abstract class BaseClass{ public abstract void MethodA(); public void MethodB() { Console.WriteLine ("This is the non abstract method”); }}class DerivedClass : BaseClass{ public override void MethodA() { Console.WriteLine ("This is the abstract method overriden in derived class"); }}
C# Simplified / Session 3 / 15 of 28
class AbstractDemo{ public static void Main() { DerivedClass objDerived = new DerivedClass(); BaseClass objBase = objDerived; objBase.MethodA(); objDerived.MethodB(); }}
C# Simplified / Session 3 / 16 of 28
Interfaces An interface is a pure abstract base
class It can contain only abstract methods
and no method implementation A class that implements a particular
interface must implement the members listed by that interfacepublic interface IFile
{ int delFile(); void disFile();}
C# Simplified / Session 3 / 17 of 28
Interfaces - Examplepublic interface IFile{ int delFile(); void disFile();}public class MyFile : IFile{ public int delFile() { System.Console.WriteLine ("DelFile Implementation!"); return(0); } public void disFile() { System.Console.WriteLine ("DisFile Implementation!"); }}
C# Simplified / Session 3 / 18 of 28
Interfaces - Outputclass InterfaceDemo{ public static void Main() { MyFile objMyFile = new MyFile(); objMyFile.disFile(); int retValue = objMyFile.delFile(); }}
public class BaseforInterface{ public void open() { System.Console.WriteLine ("This is the open method of BaseforInterface"); }}
C# Simplified / Session 3 / 19 of 28
Interfaces – Example with Inheritance
public interface IFile{ int delFile(); void disFile();}public class BaseforInterface{ public void open() { System.Console.WriteLine ("This is the open method of BaseforInterface"); }}
C# Simplified / Session 3 / 20 of 28
Interfaces – Example with Inheritance
public class MyFile : BaseforInterface, IFile
{ public int delFile() { System.Console.WriteLine ("DelFile Implementation!"); return(0); } public void disFile() { System.Console.WriteLine ("DisFile Implementation!"); }}
C# Simplified / Session 3 / 21 of 28
Interfaces – Output for Example with Inheritanceclass Test{ static void Main() { MyFile objMyFile = new MyFile(); objMyFile.disFile(); int retValue = objMyFile.delFile(); objMyFile.open(); }}
C# Simplified / Session 3 / 22 of 28
Multiple Interfaces C# allows multiple interface
implementations
public interface IFileTwo{ void applySecondInterface();}
C# Simplified / Session 3 / 23 of 28
Multiple Interfaces - Example
public class MyFile : BaseforInterface, IFile, IFileTwo{ public int delFile() { System.Console.WriteLine ("DelFile Implementation!"); return(0); } public void disFile() { System.Console.WriteLine ("DisFile Implementation!"); } public void applySecondInterface() { System.Console.WriteLine ("ApplySecondInterface Implementation!"); }}
C# Simplified / Session 3 / 24 of 28
Multiple Interfaces - Output
class MultipleInterfaces{ public static void Main() { MyFile objMyFile = new MyFile(); objMyFile.disFile(); int retValue = objMyFile.delFile(); objMyFile.open(); objMyFile.applySecondInterface(); }}
C# Simplified / Session 3 / 25 of 28
Explicit Interface Explicit interface implementation can be
used when a method with the same name is available in 2 interfaces
public interface IFile{ int delFile(); void disFile();}public interface IFileTwo{ void applySecondInterface(); void disFile();}
C# Simplified / Session 3 / 26 of 28
Explicit Interface – Contd…public class MyFile : BaseforInterface, IFile, IFileTwo{... void IFile.disFile() { System.Console.WriteLine ("IFile Implementation of DisFile"); } void IFileTwo.disFile() { System.Console.WriteLine ("IFileTwo Implementation of DisFile"); }..}
C# Simplified / Session 3 / 27 of 28
Interface Inheritance New Interfaces can be created by
combining together other interfaces The syntax for this is similar to that
used for inheritance, except that more than one interface can be merged to form a single interface.
interface IAllFile : IFile, IFileTwo{ //More operations can be added if necessary //(apart from that of IFile & IFileTwo)}
C# Simplified / Session 3 / 28 of 28
Summary Virtual functions are useful when we need to call the derived
class method from an object of the base class.
The difference between overriding and polymorphism is that in polymorphism, the decision as to which method to call is made at runtime.
Abstract Base Classes are classes that contain at least one abstract member (method without implementation). New instances of abstract base classes cannot be created.
The method with no implementation is known as an operation.
An interface is a pure abstract base class. It can contain only abstract methods, and no method implementations.
A class can implement more than one interface; in fact, a class can inherit from another class as well as implement an interface.