oop concepts

28
Session 3 Advanced OOP Concepts in C#

Upload: abdur-rehman-muhammadi

Post on 25-May-2015

187 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Oop concepts

Session 3

Advanced OOP Concepts in C#

Page 2: Oop concepts

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.

Page 3: Oop concepts

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.

Page 4: Oop concepts

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

Page 5: Oop concepts

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");    }}

Page 6: Oop concepts

C# Simplified / Session 3 / 6 of 28

Polymorphism in C# - Contd…

Beep vrooomTring-Tring

Telephone Company

Ring()Ring()

Ring()

Page 7: Oop concepts

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

Page 8: Oop concepts

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);}

}

Page 9: Oop concepts

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()); }}

Page 10: Oop concepts

C# Simplified / Session 3 / 10 of 28

Polymorphism in C# - Output

Page 11: Oop concepts

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

Page 12: Oop concepts

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.

Page 13: Oop concepts

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

Page 14: Oop concepts

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"); }}

Page 15: Oop concepts

C# Simplified / Session 3 / 15 of 28

class AbstractDemo{ public static void Main() { DerivedClass objDerived = new DerivedClass(); BaseClass objBase = objDerived; objBase.MethodA(); objDerived.MethodB(); }}

Page 16: Oop concepts

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();}

Page 17: Oop concepts

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!"); }}

Page 18: Oop concepts

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"); }}

Page 19: Oop concepts

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"); }}

Page 20: Oop concepts

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!"); }}

Page 21: Oop concepts

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(); }}

Page 22: Oop concepts

C# Simplified / Session 3 / 22 of 28

Multiple Interfaces C# allows multiple interface

implementations

public interface IFileTwo{ void applySecondInterface();}

Page 23: Oop concepts

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!"); }}

Page 24: Oop concepts

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(); }}

Page 25: Oop concepts

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();}

Page 26: Oop concepts

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"); }..}

Page 27: Oop concepts

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)}

Page 28: Oop concepts

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.