session 04 module 8: abstract classes and interface module 9: properties and indexers

34
Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Upload: penelope-hudson

Post on 05-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Session 04

Module 8: Abstract classes and Interface

Module 9: Properties and Indexers

Page 2: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 2 of 34

Module 6 - Review

Page 3: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 3 of 34

Module 7 - Review

Page 4: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 4 of 34

Module 8 - Objectives

Describe how implement abstract classes Define abstracts methods Describe how implement interface List the differences between abstract class and

Interface

Page 5: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 5 of 34

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 6: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 6 of 34

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 7: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 7 of 34

Abstract Base Classes - Example

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

Page 8: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 8 of 34

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 interface

public interface IFile{ int delFile(); void disFile();}

Page 9: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 9 of 34

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 10: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 10 of 34

Interfaces - Output

class InterfaceDemo{ public static void Main() { MyFile objMyFile = new MyFile(); objMyFile.disFile(); int retValue = objMyFile.delFile(); }}

Page 11: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 11 of 34

Multiple Interfaces

C# allows multiple interface implementations

public interface IFileTwo{ void applySecondInterface();}

Page 12: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 12 of 34

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 13: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 13 of 34

Multiple Interfaces - Output

class MultipleInterfaces{ public static void Main() { MyFile objMyFile = new MyFile(); objMyFile.disFile(); int retValue = objMyFile.delFile(); objMyFile.open(); objMyFile.applySecondInterface(); }}

Page 14: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 14 of 34

Explicit Interface (1) 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 15: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 15 of 34

Explicit Interface (2)

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 16: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 16 of 34

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 17: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 17 of 34

The differences between abstract classes and interface

Page 18: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 18 of 34

Module 8 - Summary

Page 19: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 19 of 34

Module 9 - Objectives

Define properties in C# List and explain the types of properties State and explain indexers

Page 20: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 20 of 34

Properties

Access modifier like public, private, protected, internal are used to control the accessibility of fields and methods in c#

The public field are accessible by other class but private fields are accessible only by the class in which they are declared

C# use feature called properties that allows you to set and retrieve value of fields declared with any access modifier in secured manner.

Page 21: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 21 of 34

Syntax

Page 22: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 22 of 34

Properties

The get accessor is used to read a value The set accessor is use to assign a value

Page 23: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 23 of 34

Read only property

• To retrieve the value of a private field

• Need define the get accessor

Page 24: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 24 of 34

Write only property

• To change the value of a private field

• Need define the set accessor

Page 25: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 25 of 34

Read and Write property

• To allow set and retrieve the value of private field• Need define set and get accessor

Page 26: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 26 of 34

Use of properties

Modifies private field Validates private field Perform required actions Implements abstraction Implements encapsulation

Page 27: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 27 of 34

Properties vs. Fields

Page 28: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 28 of 34

Property vs. Method

Page 29: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 29 of 34

Indexers

Indexers are data members that allow you to access data within objects in a way that is similar to accessing arrays

Page 30: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 30 of 34

Syntax of index declaration

Page 31: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 31 of 34

Parameters

Indexers must have at least one parameter.

The parameter denotes the index. position, using index position the stored value at that position is set or accessed.

Index can also have multiple parameters. Such indexers can be accessed like a multi dimensional array.

Page 32: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 32 of 34

Properties versus Indexers

Page 33: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 33 of 34

Module 9 – Summary (1)

A field is a data member of class that store some information.

Properties enables to you to access the private fields of class.

Methods are data members that define a behavior performed by an object.

Properties protect fields of the class while accessing them.

Page 34: Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers

Abstract classes & Interface & Properties & Indexers / Session 4 / 34 of 34

Module 9 - Summary (2)

Property accessor enable you to read and assign values to fields

Property can be created by using get and set accessors

Indexers treats an object like an array, so providing faster access to data within the object.