Object Oriented Programming in C# February 13, 2008 The C# object - - PowerPoint PPT Presentation

object oriented programming in c
SMART_READER_LITE
LIVE PREVIEW

Object Oriented Programming in C# February 13, 2008 The C# object - - PowerPoint PPT Presentation

Object Oriented Programming in C# February 13, 2008 The C# object model 1 Implementing an object oriented language 2 1/27 Goals of the C# object system. polymorphism the ability to write code which operates on many typesrealized by


slide-1
SLIDE 1

Object Oriented Programming in C#

February 13, 2008

slide-2
SLIDE 2

1

The C# object model

2

Implementing an object oriented language

1/27

slide-3
SLIDE 3

Goals of the C# object system.

polymorphism the ability to write code which operates on many types—realized by inheritance, interfaces, and

  • verloading

encapsulation the ability to make separate a class’s behavior from its implementation details—realized with access modifiers extensibility the ability to extend class functionality—realized with inheritance and virtual methods.

2/27

slide-4
SLIDE 4

Terminology

Field: a variable declared in a class. Method: a procedure associated with a class. Member: a field or method. Instance of <class>: an object of type <class>

3/27

slide-5
SLIDE 5

Inheritance

All classes inherit from a base class (default is System.Object). Derived classes automatically include the members of their base classes. Child classes extend base classes by adding new members, and overriding virtual methods. Can treat an instance of a derived class as an instance of its base class.

4/27

slide-6
SLIDE 6

Basic inheritance example

using System ; class BaseSimple { public void P r i n t ( ) { Console . Out . WriteLine ( " BaseSimple " ) ; } } class ChildSimple : BaseSimple { } class Runner { public s t a t i c void Main ( s t r i n g [ ] s ) { (new BaseSimple ( ) ) . P r i n t ( ) ; / / " BaseSimple " (new ChildSimple ( ) ) . P r i n t ( ) ; / / " BaseSimple " } }

5/27

slide-7
SLIDE 7

Static Dispatch: New overloaded methods are called using an object’s compile-time type.

class BaseNew{ public void P r i n t ( ) { Console . Out . WriteLine ( "BaseNew" ) ; } } class ChildNew : BaseNew { new public void P r i n t ( ) { Console . Out . WriteLine ( " ChildNew " ) ; } } class Runner { public s t a t i c void Main ( s t r i n g [ ] s ) { ChildNew c = new ChildNew ( ) ; BaseNew b = c ; c . P r i n t ( ) ; / / " ChildNew " b . P r i n t ( ) ; / / "BaseNew" } }

6/27

slide-8
SLIDE 8

Dynamic Dispatch: Virtual methods called using an

  • bject’s run-time type.

class BaseVirt { public v i r t u a l void P r i n t ( ) { Console . Out . WriteLine ( " BaseVirt " ) ; } } class C h i l d V i r t : BaseVirt { public

  • verride

void P r i n t ( ) { Console . Out . WriteLine ( " C h i l d V i r t " ) ; } } class Runner { public s t a t i c void Main ( s t r i n g [ ] s ) { C h i l d V i r t c = new C h i l d V i r t ( ) ; BaseVirt b = c ; c . P r i n t ( ) ; / / " C h i l d V i r t " b . P r i n t ( ) ; / / " C h i l d V i r t " } }

7/27

slide-9
SLIDE 9

Overriding Rules

Base classes may mark methods with virtual . Such methods are virtual and may be overridden by derived classes. Derived classes must mark methods with override to

  • verride them.

Derived classes can mark methods with sealed prevent subclasses from overriding the methods.

By default methods are sealed. A derived class can seal a virtual method to stop further

  • verriding.

Compiler with raise an error if there’s a chance of ambiguity.

8/27

slide-10
SLIDE 10

Calling base class methods with base

Sometimes we need to call a base class’s methods explicitly. class C h i l d V i r t : BaseVirt { public

  • verride

void P r i n t ( ) { Console . Out . WriteLine ( " C h i l d V i r t says Hi ! " ) Console . Out . WriteLine ( "Base v i r t says : " ) ; / / c a l l s base method base . P r i n t ( ) ; } / / c a l l s base constructor public C h i l d V i r t ( i n t x ) : base ( x ) { } } Without the base keyword, there would be no way to access such methods!

9/27

slide-11
SLIDE 11

Class Modifiers and Static Members

Class modifiers

Marking a class abstract means it can’t be instantiated,

  • nly derived from.

Marking a class sealed means it can’t be derived from, only instantiated. Marking a class static means a class is both sealed and

  • abstract. (Can only contain static members.)

Static members

One copy of member per class (as opposed to per instance). Can be initialized with a zero-argument static constructor.

10/27

slide-12
SLIDE 12

Static class example

using System . Collections . Generic ; public s t a t i c class Logger { p r i v at e s t a t i c List < string > myList ; s t a t i c Logger ( ) { myList = new List < string > ( ) ; } public s t a t i c void Append ( s t r i n g s ) { myList . Add( s ) ; } }

11/27

slide-13
SLIDE 13

Interfaces declare contracts that a class must follow.

Interfaces list methods which much a appear in a class. Methods may use interface names for argument and result types (bounded polymorphism). Classes can implement interfaces in two ways

Implicitly (the normal way), interface methods added directly to class and accessed as usual. Explicitly, interface members are declared with special syntax and accessed through casts. Useful in the case where two interfaces declare methods with the same name.

12/27

slide-14
SLIDE 14

Example: Implicit Interface Implementation

i n t e r f a c e IWindow { void Draw ( ) ; } public class Display : IWindow { / / I m p l i c i t I n t e r f a c e Implementation public void Draw ( ) { Console . Out . WriteLine ( "A" ) ; } } class Runner { s t a t i c void Main ( s t r i n g [ ] args ) { Display c = new Display ( ) ; d . Draw ( ) ; / / "A" } }

13/27

slide-15
SLIDE 15

Multiple interfaces can conflict.

i n t e r f a c e IWindow { / / Implementations should p r i n t to the screen void Draw ( ) ; } i n t e r f a c e ICowboy { / / Implementations should get

  • ut a gun

void Draw ( ) ; } / / Trouble ! public class WesternGame : IWindow , ICowboy { . . . }

14/27

slide-16
SLIDE 16

Example: Explicit Interface Implementation

class WesternGame : IWindow , ICowboy { / / E x p l i c i t I n t e r f a c e Implementations void IWindow . Draw ( ) { Console . Out . WriteLine ( " Drawing Picture " ) ; } void ICowboy . Draw ( ) { Console . Out . WriteLine ( " Drawing Six Shooter " ) ; } } class Runner { s t a t i c void Main ( s t r i n g [ ] args ) { WesternGame w = new WesternGame ( ) ; / / Error : w. Draw ( ) ; ( ( ICowboy ) w) . Draw ( ) ; / / " Drawing Picture " ( ( IWindow ) w) . Draw ( ) ; / / " Drawing Six Shooter " } }

15/27

slide-17
SLIDE 17

Casting

string x = ( string ) someObject Up-casts:

Convert instances of a child class to a parent class or interface. Always succeeds.

Down-casts:

Convert instances of a parent class to a child class. May fail and throw InvalidCastException Use as or is to check if a cast is safe.

Generics provide an elegant way to write (for example) collection classes without casting.

16/27

slide-18
SLIDE 18

Access modifiers protect class implementation details.

Access modifiers may be attached to class, field, and method declarations.

Modifier Meaning public No visibility restrictions. protected1 Visible to classes derived from the defining class internal2 Visible anywhere in the same as- sembly. protected internal1 Visible according to protected. Also, member visible according to internal. private1 Visible only within defining class

1Only applicable to elements defined in a class (i.e. not to classes defined

  • nly in a namespace).

2internal is the default access modifier.

17/27

slide-19
SLIDE 19

1

The C# object model

2

Implementing an object oriented language

18/27

slide-20
SLIDE 20

Methods

C#: Methods, generics, objects, interfaces. . . Machine code:

  • perators: add, subtract, xor. . .

conditionals: if jump take CIS 371 for more details.

Common Intermediate Language

  • bject oriented byte code

.Net equivalent to Java byte code closer to C# than machine code

How do we compile an object oriented program to machine code?

19/27

slide-21
SLIDE 21

Functions and Methods

Functions take arguments, compute, and return a result. have access to arguments and global variables. always “means the same thing” (static dispatch). easy to implement in machine code. Methods take arguments, compute, and return a result. has access to arguments, global variables, and object members. have context dependent meanings (dynamic dispatch). are be implemented in terms of functions.

20/27

slide-22
SLIDE 22

From functions to methods

Translating methods to functions requires emulating two key method behaviors Access to object members: Dynamic dispatch: Will also need simpleClasses (or records) which contain multiple fields but no methods.

21/27

slide-23
SLIDE 23

From functions to methods

Translating methods to functions requires emulating two key method behaviors Access to object members: Represent methods as a functions that takes special argument, this, that contains an object reference. Dynamic dispatch: Will also need simpleClasses (or records) which contain multiple fields but no methods.

21/27

slide-24
SLIDE 24

From functions to methods

Translating methods to functions requires emulating two key method behaviors Access to object members: Represent methods as a functions that takes special argument, this, that contains an object reference. Dynamic dispatch: Lookup the right function to call in a table (the vtable) at runtime. Will also need simpleClasses (or records) which contain multiple fields but no methods.

21/27

slide-25
SLIDE 25

Example: Adding a this argument

class Counter { i n t C; void inc ( i n t x ) {C += x ; } void incTwice ( i n t x ) { inc ( x ) ; inc ( x ) } }

  • simpleClass Counter {

i n t C; } function void Counter_inc ( Counter this , i n t x ) { t h i s .C += x ; } function void Counter_incTwice ( Counter this , i n t x ) { c a l l Counter_inc ( this , x ) ; c a l l Counter_inc ( this , x ) }

22/27

slide-26
SLIDE 26

Example: Virtual methods through v-tables

class Counter { i n t C; v i r t u a l void inc ( i n t x ) {C += x ; } } class FastCounter : Counter {

  • verride

void inc ( i n t x ) {C += 2∗x ; } } class Runner { s t a t i c void Main ( s t r i n g [ ] args ) { Counter c = new FastCounter ( ) ; / / Should c a l l FastCounter method and get 6 c . inc ( 3 ) ; } }

23/27

slide-27
SLIDE 27

Example: Virtual methods through v-tables

class Counter { i n t C; v i r t u a l void inc ( i n t x ) {C += x ; } }

  • simpleClass Counter {

i n t C; / / compiler remembers 0 −> Counter_inc function [ ] vtable = { Counter_inc } ; } function void Counter_inc ( Counter this , i n t x ) { t h i s .C += x ; }

24/27

slide-28
SLIDE 28

Example: Virtual methods through v-tables

class FastCounter : Counter {

  • verride

void inc ( i n t x ) {C += 2∗x ; } }

  • simpleClass FastCounter {

/ / copied from base class i n t C; / / compiler remembers 0 −> FastCounter_inc function [ ] vtable = { FastCounter_inc } ; } function void FastCounter_inc ( Counter this , i n t x ) { C += 2∗x ; }

25/27

slide-29
SLIDE 29

Example: Virtual methods through v-tables

s t a t i c void Main ( s t r i n g [ ] args ) { Counter c = new FastCounter ( ) ; c . inc ( 3 ) ; } / / s t a t i c methods can compile to functions w/ o t h i s function void Runner_Main ( s t r i n g [ ] args ) { / / c a l l FastCounter ’ s d e fa u l t constructor c = c a l l FastCounter_ctor ( ) ; / / do the v i r t u a l c a l l function f = c . vtable [ 0 ] ; c a l l f ( c , 3) }

26/27

slide-30
SLIDE 30

Other features

Interfaces Each interface gets an interface table—analogous to a vtable. Constructors Implemented like static methods—return the this pointer.

27/27