Windows Programming Using C# Classes & Interfaces.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Python Objects and Classes
Web Application Development Slides Credit Umair Javed LUMS.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
Programming in Java CSCI-2220 Object Oriented Programming.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Object-Oriented Programming Chapter Chapter
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
ISBN Object-Oriented Programming Chapter Chapter
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Object Oriented Software Development 4. C# data types, objects and references.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Static data members Constructors and Destructors
Andy Wang Object Oriented Programming in C++ COP 3330
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
C# In many ways, C# looks similar to Java
Chapter 9 Inheritance and Polymorphism
Java Programming Language
CIS 199 Final Review.
Java Programming Language
Lecture 10 Concepts of Programming Languages
SPL – PS3 C++ Classes.
Presentation transcript:

Windows Programming Using C# Classes & Interfaces

2 Contents Classes Inheritance Operator Overloading Structs Interfaces

3 C# Classes The class is the main data structure in C# It blends ideas from both C++ and Java  All methods are declared inline, as in Java  No C++ style header files  Supports only single inheritance  Inheritance syntax from C++  Allows multiple classes to be declared in one file like C++  Methods can be used before being declared

4 Class Declaration [attributes][access-modifiers]class ident [:base-class] {class-body}  Attributes & modifiers discussed later  For now, just use public  An optional base class can be used for inheritance

5 Class Declaration using System; public class Person { string name; int age; public Person(string nm, int ag) { name = name; age = ag; } public string getName() { return name; }

6 Access Modifiers ModifierExplanation publicVisible to any method of any class privateVisible only to methods of the declaring class. This is the default access modifier if none is specified. protectedVisible to declaring class and subclasses internalVisible to any method in the same assembly as the declaring class protected internal Visible to declaring class, subclasses, and classes in the same assembly

7 Object Creation Primitive types like int, char, etc. are value types and are created on the stack Objects are reference types and are created on the heap  Declare an object reference variable  Use the new operator to create a class instance on the heap  Assign this new instance to the reference variable

8 Object Creation Person p = new Person(“Ann”, 34);  This creates a new instance on the heap  The constructor for the new instance is invoked  The reference to the new instance is assigned to the variable  The object will be destroyed when there are no more references to it and it is reclaimed by the garbage collector

9 Constructors A constructor is a special method with the same name as the class It is run to initialize the class Before it is run the class is undifferrentiated memory After it is run, the memory is an instance of the class

10 Constructors Constructors never have a return type Constructors are usually public Constructors may be overloaded If no constructor is provided, the CLR will create one which does nothing If any constructor is provided, no default constructor will be created

11 Member Variable Initialization C# allows you to initialize a member variable when it is declared  int day = 30;  This is called an initializer If you do not provide an initializer, a default value is assigned This means that there are no uninitialized variables in C#

12 Default Values for Primitives TypeDefault Value numeric0 boolfalse char‘\0’ enum0 referencenull

13 Copy Constructors A copy constructor is simply a constructor which  accepts an instance of the same class as a parameter and  makes a copy of it You must create a copy constructor if you need one, the compiler will not create one for you public Person(Person p) { name = p.name; age = p.age; }

14 This this is a keyword referencing the current object It can be used in any non-static class It can be used to  Disambiguate parameters public Person(string name) { this.name = name; }  Pass a reference to the current object List.add(this);

15 Static Members Classes have two types of members  Instance members Every class instance has its own copy  Static members The member is associated with the class, not the instance All instances access the same members stored with the class Must be qualified with the class name, NOT the name of an instance

16 Static Methods Static methods are not associated with an instance and have no this reference Static methods cannot refer to non-static members Static methods are useful for  Utility operations associated with the class  Operations which modify the static data of a class

17 Static Constructors Are invoked to initialize static members Are invoked before any other constructors A static constructor cannot have an access modifier (ie. public ) A static constructor cannot access any non-static members

18 Destructors C# has a garbage collector When there are no more references to an object, it is marked for destruction Some time later, the garbage collector will actually destroy the object Unlike in Java, the object is guaranteed to be destroyed

19 Destructors A C# destructor looks like a C++ destructor  ~MyClass() { /* do destructor work here */ } That is the end of the similarities  You only need a destructor if you have to free unmanaged resources like file handles  References to managed objects will be handled by the garbage collector without the need for a destructor  Never call a destructor directly, the garbage collector will call it for you

20 Finalize This syntax is really a shortcut for calling a Finalize() method which will chain up to the base class It is translated to ~MyClass() { try { // work of destructor } finally { base.Finalize(); }

21 Dispose The problem with destructors is that they will not run until the object is reclaimed by the garbage collector This means that precious resources might be held for longer than necessary You can fix this by  Implementing the IDisposable interface  Defining the required Dispose() method  Having clients call Dispose() when they have finished with a class instance

22 Dispose If Dispose() is called and freed the resources, you must not have the destructor do the same thing You can suppress finialization by  GC.SuppressFinalize(this) You should ensure that the resources are only freed once

23 Dispose using System; class Testing : IDisposable { bool is_disposed = false; protected virtual void Dispose() { if (!is_disposed) { // only dispose once! // perform cleanup for this object GC.SuppressFinalize(this); } this.is_disposed = true; } ~Testing( ) { Dispose(false); }

24 The Using Statement Since there is no way to guarantee that client code will call Dispose(), the using statement is provided using ( Font theFont = new Font( "Arial", 10.0f ) ) { // use theFont } // compiler will call Dispose on theFont This guarantees Dispose() is called on theFont when the using block is exited

25 Parameter Passing Normally, value parameters (int, char, etc.) are passed by value  A copy is made and passed to the method  The method might modify the copy  The copy is not returned and the original is left unchanged We can modify this using the ref keyword to use pass by reference

26 Pass By Reference public add3(ref int n) { n = n + 3; } Since a reference is used, the original value in the calling code is modified There is no need to dereference anything as you would do with a C pointer

27 Out Parameters Definite assignment requires that a parameter be initialized before it can be used This means we have to initialize a variable before we pass it to a method, even though the method will set the value and never use it We can use the out keyword to modify this behaviour

28 Out Parameters public void init(out int n) { n = 34; } This code can be passed an uninitialized parameter It can assign a value to the parameter and return it

29 Properties Properties allow code to access the state of an object as if they were accessing a field directly However, a property does not access a field A special class method provides the access and controls what is determined This simplifies the interface while prohibiting unrestricted access to class internals

30 Properties Access to properties is provided accessors  A get accessor to return the value  A set accessor to set the value These accessors are placed inside the property declaration You can omit either the set or get accessor if they are not needed

31 Properties Consider adding a Name property public class Person { string name; int age; public Person(string nm, int ag) {… } public string Name { get { return name; } set { name = value; }

32 Properties The set accessor has no parameter The value passed to it is called value We can use the accessors like this: Person p = new Person(“Jane”, 29); Console.WriteLine(p.Name); p.Name = “Janie”;

33 Readonly Fields Readonly fields can only be assigned  By initialization or  By a constructor Sometimes, you want to create a public static constant but cannot initialize it You can make it readonly so that it can be initialized by one of the constructors

34 Readonly Fields public class Color { public static readonly Color Black = new Color(0, 0, 0); public static readonly Color White = new Color(255, 255, 255); private byte red, green, blue; public Color(byte r, byte g, byte b) { red = r; green = g; blue = b; } } We cannot initialize Black & White as constants because their values cannot be computed at compile time Making them readonly has a similar effect to making them constants

35 Contents Classes Inheritance Operator Overloading Structs Interfaces

36 Inheritance C# supports single inheritance Syntax:  public class ListBox: Window Inherits all base class members It can extend the base class by adding new members It can override any method in the base class with a new method using the keyword new

37 Inheritance public class EmployedPerson: Person { float salary; … public float Salary { get {return salary;} }

38 Base Class Construction The base class must be constructed before the derived class To do this, each constructor for the derived class must invoke the constructor for the base class Constructors cannot be inherited, so each class must implement its own

39 Base Class Construction If the base class has a default constructor which is accessible, it will be called automatically If no default constructor is in the base class, another constructor must be called A base class constructor is invoked with the base keyword

40 Base Class Construction public class EmployedPerson: Person { float salary; … public EmployedPerson(string name, int age, float salary): base(name, age) { this.salary = salary; }

41 New Methods When a derived class declares a method with the same name as a method in the base class it can  Hide the method in the base class  Polymorphically override the method in the base class To hide the method in the base class, you must use the new keyword

42 New Methods When you invoke a new method in the derived class  A reference to the derived type invokes the method in the derived class  If the derived instance is assigned to a base class reference variable, invoking the method with the base reference will invoke the base version of the method  * see Person example

43 New Methods public class PersonBase { … public string ClassName() { return "PersonBase"; } public class EmployedPerson: PersonBase { … public new string ClassName() { return “EmployedPerson"; } … PersonBase pb1, pb2; EmployedPerson ep1; pb1 = new PersonBase("Julie", 34); ep1 = new EmployedPerson("Betty", 28, ); pb2 = ep1; Console.WriteLine("pb1.ClassName() = {0}", pb1.ClassName()); Console.WriteLine("pb2.ClassName() = {0}", pb2.ClassName()); Console.WriteLine("ep1.ClassName() = {0}", ep1.ClassName()); PersonBase EmployedPerson

44 Polymorphism With true polymorphism, we can override methods and the compiler will always invoke the correct method For this to work  The method in the base class must be virtual  The method in the derived class must be declared override

45 Polymorphism System.Object declares the method ToString() as virtual This is provided to convert every object to a string representation We can override it in PersonBase by public override string ToString() { return name + ", " + age; }

46 Calling Base Class Methods We want to override it again in the EmployedPerson class This time, we want to call the version in the parent class using the base keyword public override string ToString() { return base.ToString() + ", " + salary; }

47 Versioning Suppose we have classes  BaseClass  DerivedClass Now, let’s add a virtual method to DerivedClass public class DerivedClass: BaseClass { public virtual void NewMethod() {…} }

48 Versioning All is well Now, pretend the author of BaseClass decides to provide the same method public class BaseClass { public virtual void NewMethod() {…} } When this is compiled, a warning states that NewMethod() in DerivedClass hides the one in BaseClass

49 Versioning The way this works is  To find a virtual method the compiler searches up the class chain for the first method marked virtual  For the derived class, this would be its version of the method  For the base class, its version would be used The benefit of this is  A base class can be modified without breaking anything done in a derived class

50 Versioning The warning states  Declare it new to hide the base method and eliminate the warning  Declare it override to override the base method  Or leave as is to hide with a new virtual method and still get the warning

51 Abstract Classes A class can be declared abstract and not have a body  abstract public void MyMethod(); Any class with an abstract method must be declared abstract  abstract public class MyClass { …} Abstract classes cannot be instantiated Abstract methods must have implementations before a class becomes concrete

52 Sealed Classes A sealed class cannot be extended This is the same as final in Java To use it, place the sealed keyword before the class declaration

53 System.Object This is the root of all objects All classes are derived from it even if they do not derive from any class This class provides methods which are inherited by every object

54 Object Methods MethodPurpose Equals()Compares two objects for equality GetHashCode()Provides a hash code GetType()Returns a type object for the object ToString()Produces a string representation Finalize()Cleans up non-memory resources MemberwiseClone()Creates copies. Do not override! ReferenceEquals()True if two references refer to the same object

55 Boxing Boxing is the conversion of one of the primitive types to an object Any of the primitive types can be  Assigned to an object reference  Passed to a method expecting an object as a parameter In these cases, the compiler will automatically convert the primitive into an object int i = 57; Console.WriteLine(“i={0}”, i); This expects an object, boxes the primitive and then calls ToString() on the resulting object

56 Unboxing This is retrieving the primitive value from a boxed value It requires an explicit conversion since the type stored in the object is unknown to the compiler int i = 57, j; Object o = i; j = (int) o;

57 Nested Classes One class can be nested within another This is often done for helper classes unused outside the outer class Nested classes have access to all members of the outer class Can be declared public if they can be used outside the class

58 Contents Classes Inheritance Operator Overloading Structs Interfaces

59 Operator Overloading C# provides operator overloading similar to C++ All operators must be static, unlike in C++ The goal is to make user-defined classes more like system types Not all.NET languages support operator overloading and equivalent methods should be used if interoperability is an issue * see example Fraction

60 Operator Overloading To overload ==, you should also overload Equals public override bool Equals(object obj) { bool result = false; if (obj is Fraction) { Fraction f = (Fraction)obj; result = numerator == f.Numerator && denominator == f.Denominator; } return result; }

61 Operator Overloading public static bool operator==(Fraction obj1, Fraction obj2) { return obj1.Equals(obj2); }

62 Pairing Many operators must be overloaded in pairs  Overload == and you must overload !=  Overload  Overload == and you must overload getHashCode()

63 Conversion Operators Conversions can be  Implicit Automatically performed by compiler No loss of information  Explicit Requires cast by user Might involve loss of information

64 Conversion Operators public static implicit operator Fraction(int i) { return new Fraction(i, 1); } public static explicit operator int(Fraction f) { return f.numerator / f.denominator; }

65 Contents Classes Inheritance Operator Overloading Structs Interfaces

66 Structs Structs are lightweight substitutes for classes Structs are always value type, not reference types Structs support  Constructors  Properties  Methods  Operators  Indexers  Nested types

67 Structs Structs do not support  Inheritance  Destructors  Being a reference type You cannot extend a struct although it can implement multiple interfaces All structs are derived from object

68 Structs Structs are simpler than classes and usually more efficient They can also be used to interface with libraries written in C++

69 Declaring Structs public struct Location { private int xVal; private int yVal; public Location( int xCoordinate, int yCoordinate ) { xVal = xCoordinate; yVal = yCoordinate; } public int x { get { return xVal; } set { xVal = value; } } public int y { get { return yVal; } set { yVal = value; } } public override string ToString( ) { return ( String.Format( "{0}, {1}", xVal, yVal ) ); }

70 Creating Structs You can create a struct by  Location l1 = new Location(1,2);  Location l1; These both create the Location on the stack as a value type The second one does not invoke a constructor and field values must be assigned before it can be used

71 Passing Structs Since structs are value types, they will be passed by value This means that changes made to a struct in a method will not be passed back unless a ref parameter is used

72 Contents Classes Inheritance Operator Overloading Structs Interfaces

73 Interfaces An interface is a contract which states  All implementers of the interface guarantee to implement all Methods Properties Events And indexers  Of the interface

74 Declaring Interfaces [attributes][access-modifier] interface ident [:base-list] {interface-body} interface IStorable { void Read(); void Write(); }

75 Implementing Interfaces A class can implement an interface  Using the same syntax as inheritance  Implementing the required methods, etc. public class Document: IStorable { public void Read(){…} public void Write(object obj){…} … } A class can implement multiple interfaces

76 Extending Interfaces You can extend an interface using the colon notation This allows you to add new requirements to an existing interface You can extend several interfaces to form the union of all their requirements in a new interface

77 Working with Interfaces Any class which implements an interface can be assigned to the interface type Document doc = new Document(“test”); IStorable stor = (IStorable) doc; Only the methods etc. defined in the IStorable interface can be used on the stor object

78 The is Operator Before you cast an object to an interface, it is good to know if the object implements the interface The is operator can tell you this If( doc is IStorable) { stor = (IStorable) doc; } This avoids exceptions being thrown

79 The as Operator The as operator  Checks to see if an object implements an interface  If it does it casts it  If it does not, it returns null stor = doc as IStorable; If(stor != null) { stor.Read(); }

80 Explicit Interface Implementation What happens if a class implements two interfaces which require the same method? The class must explicitly qualify the method names with the interface names so that the compiler can tell them apart One can be left unqualified since it must belong to the interface which has yet to have the method implemented

81 Explicit Interface Implementation A qualified interface name cannot  Have an access modifier, they are all public  Be abstract, virtual, override or new For example  IStorable has a Read() method  ITalk has a Read() method The a class implementing both interfaces would declare  void Read()  void ITalk,Read()