Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.

Slides:



Advertisements
Similar presentations
Generics, Lists, Interfaces
Advertisements

Lecture 5: Interfaces.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Generic programming in Java
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Creating Classes from Other Classes Chapter 2. 2 Chapter Contents Composition Adapters Inheritance Invoking constructors from within constructors Private.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
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.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
C#.Net Development Version 1.0. Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright.
More About Classes Ranga Rodrigo. Information hiding. Copying objects.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
E FFECTIVE C# 50 Specific Ways to Improve Your C# Second Edition Bill Wagner محمد حسین سلطانی.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Object Oriented Software Development
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Advanced C# Types Tom Roeder CS fa. From last time out parameters difference is that the callee is required to assign it before returning not the.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Object Oriented Programming
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Introduction to Object-Oriented Programming Lesson 2.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Classes (Part 1) Lecture 3
Comp 249 Programming Methodology
Object Based Programming
Lecture 4: Interface Design
Lecture 22 Inheritance Richard Gesick.
CIS 199 Final Review.
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Presentation transcript:

Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction

Modern Software Development Using C#.NET by Richard Wiener2 Overview Some common pitfalls associated with class construction are examined and solutions found to the problems. We look at constructor pitfalls, redefining the Equals method from class Object, cloning objects, using StringBuilder instead of String objects, the important and new feature of generic classes and the new and important feature of enumerators that enable the use of the foreach iteration for the instances of a class.

Modern Software Development Using C#.NET by Richard Wiener3 Avoiding protected fields Fields earmarked as protected in a base class are directly accessible in all descendent classes. Assignment to a base-class field made in a remote descendent class, many levels down in a class hierarchy, may not be easy to decipher and understand unless the programmer making this field assignment has intimate knowledge of the base class that contains the field whose value is being modified.

Modern Software Development Using C#.NET by Richard Wiener4 Avoiding protected fields continued Another consequence of making direct assignments to base-class fields in a descendent class is that if in the future the information structure of the base class is changed and the base-class field is removed, the descendent classes that have made direct assignments to this base-class field will be broken.

Modern Software Development Using C#.NET by Richard Wiener5 Constructor pitfall Never invoke a method within a base- class constructor that can be redefined in some descendent class.

Modern Software Development Using C#.NET by Richard Wiener6 Overriding method Equals from class Object When designing classes for potential reuse in many applications, it is important to consider whether it is appropriate to redefine the Equals method inherited by default from class Object. The default Equals method from Object considers two objects to be equal in value if and only if they have the same memory reference. Often this default meaning of Equals is incorrect.

Modern Software Development Using C#.NET by Richard Wiener7 Cloning objects Cloning an object involves producing a new instance of a class, the target, and copying the features of the source instance to the target instance. An important issue is whether the target instance continues to have any dependence on the source object. In principle, it should not. In practice, it often does which can be a major problem.

Modern Software Development Using C#.NET by Richard Wiener8 Cloning objects continued There are two types of copying: shallow and deep copying. Shallow copying copies references but not the content of the references. As a result the target reference becomes another name for the source reference. Any change made to the source or target after the copy affects the other. With deep copying, an independent autonomous target is created with the content of the source copied to the target and with no residual dependency between the source and the target.

Modern Software Development Using C#.NET by Richard Wiener9 Cloning objects continued When one reference-type object, say object1 is assigned to another reference-type object, say object2, as in, object2 = object1; a shallow copy results. Any subsequent change to object1 appears to affect object2 since they are both names for the same storage.

Modern Software Development Using C#.NET by Richard Wiener10 Cloning objects continued Any C# class that defines a Clone method must implement the interface IClonable. This interface is given as follows: public interface IClonable { Object Clone(); }

Modern Software Development Using C#.NET by Richard Wiener11 Using StringBuilder instead of String A common and potentially expensive pitfall is using objects of type String instead of StringBuilder when repeatedly concatenating string objects. Objects of type String are immutable. That means that once a String object is constructed its contents cannot be changed. String objects are of value type. Standard class StringBuilder presents a mutable reference type that may be used as an alternative to String when appropriate.

Modern Software Development Using C#.NET by Richard Wiener12 Generics A generic container class contains a set of operations that are independent of the type of objects being held in the container. With the advent of object-oriented languages and an omnipresent class Object that all classes are descendents of, the temptation to achieve generic behavior through polymorphism has been present. In a landmark paper “Genericity versus Inheritance” published in the ACM SIGPLAN Notices, Vol 21, No 11 in 1986, Bertrand Meyer presents a clear case that using polymorphism through inheritance to achieve generic behavior is the wrong way to achieve generic behavior.

Modern Software Development Using C#.NET by Richard Wiener13 Generics continued A class is generic if it contains one or more generic parameters. When an instance of such a class is created, the specific type for each generic parameter must be specified. Consider the signature of the following generic class: public class MyGenericClass { // Details not shown } An instance of this class may be created as follows: MyGenericClass myObject = new MyGenericClass (); Here the generic parameter T is replaced by String.

Modern Software Development Using C#.NET by Richard Wiener14 Constrained generic parameters The parameters specified in a generic class may be constrained. A generic parameter may be constrained by specifying that it must implement a particular interface or extend a particular class.

Modern Software Development Using C#.NET by Richard Wiener15 Constrained generic parameters continued Consider the following class declaration: public class MyGenericClass : IComparable where T : IComparable { // Details not shown } This declaration implies that MyGenericClass contains a generic parameter T that must implement the IComparable interface. In addition, the class itself must implement this interface (define a method CompareTo).

Modern Software Development Using C#.NET by Richard Wiener16 Generic methods Methods may be defined that contain one or more generic parameters even though the class that they are defined within contains no generic parameters. Consider the static method SelectionSort that contains the constrained generic parameter T. Its signature is the following: public static void SelectionSort (T [] data, int size) where T : IComparable { // Details not shown }

Modern Software Development Using C#.NET by Richard Wiener17 Generic methods continued The constraint on the generic parameter T is that it must be an instance of a class that implements the IComparable interface. This makes sense because to sort an array of values, the values must be comparable otherwise there would be no way to determine the smallest, next smallest, etc.

Modern Software Development Using C#.NET by Richard Wiener18 Summary of the benefits of generic classes Generic container classes move type checking from the runtime system to the compiler, thus making code safer. They also provide for a mechanism for self- documentation since when a generic container instance is created, the type of objects that are intended to be placed inside the container is specified in the code itself. Finally, generic classes remove the need for cluttering one’s code with downcasts. This makes the code easier to read and maintain.

Modern Software Development Using C#.NET by Richard Wiener19 Enumerators Iteration through a container class allows each object in the container to be visited exactly once. Any class that is to support an enumerator must implement the IEnumerable interface. Such a class must meet the contract of implementing the GetEnumerator method that returns an instance of IEnumerator.

Modern Software Development Using C#.NET by Richard Wiener20 Enumerators continued A class that implements IEnumerator must meet the contract of implementing: The read-only property Current that returns the element in the collection that the cursor is currently pointing to. The method MoveNext that returns a value true if the enumerator has successfully advanced to the next element in the collection and false if there is no next element in the collection. The method Reset that restores the cursor so that it points to the first element in the collection.

Modern Software Development Using C#.NET by Richard Wiener21 Enumerators continued The foreach construct provides a simple mechanism for iterating through a collection object that is an instance of a class that implements IEnumerable.

Modern Software Development Using C#.NET by Richard Wiener22 Enumerators continued We illustrate the use of the foreach construct on the standard ArrayList collection class in the following segment of code. An ArrayList containing four objects of type String is created. Using the foreach iteration construct, the names are output.

Modern Software Development Using C#.NET by Richard Wiener23 Enumerators continued using System; using System.Collections; namespace Iteration { public class IterationApplication { static void Main() { ArrayList myList = new ArrayList(); myList.Add("Erik"); myList.Add("Anna"); myList.Add("Marc"); myList.Add("Daniel"); foreach (String name in myList) { Console.WriteLine(name); } Console.ReadLine(); }

Modern Software Development Using C#.NET by Richard Wiener24 Enumerators continued An alternative way to iterate through the ArrayList would be: IEnumerator iterator = myList.GetEnumerator(); bool result = iterator.MoveNext(); while (result && iterator.Current != null) { String name = (String)iterator.Current; Console.WriteLine(name); result = iterator.MoveNext(); } The foreach construct clearly provides a simpler mechanism for accomplishing the iteration through the ArrayList container.

Modern Software Development Using C#.NET by Richard Wiener25 Partial classes C# allows a class definition to be distributed over several source files. Integrated development environments such as Visual Studio take advantage of this capability by embedding initialization code in a separate file from the code the user develops even though both are part of the same class.

Modern Software Development Using C#.NET by Richard Wiener26 Partial classes continued The keyword partial is used to achieve this. So, for example, MyClass might be written in several parts by using, public partial class MyClass { // Details not shown }

Modern Software Development Using C#.NET by Richard Wiener27 Summary Fields earmarked as protected in a base class are directly accessible in all descendent classes. As a consequence, an assignment to a base- class field made in a remote descendent class, many levels down in a class hierarchy, may not be easy to decipher and understand unless the programmer making this field assignment has intimate knowledge of the base class that contains the field whose value is being modified.

Modern Software Development Using C#.NET by Richard Wiener28 Summary continued Never invoke a method within a base-class constructor that can be redefined in some descendent class. When designing classes for potential reuse, it is important to consider whether it is appropriate to redefine the Equals method inherited by default from class Object. The default Equals method from Object considers two objects to be equal in value if and only if they have the same memory reference. Often this default meaning of Equals is incorrect.

Modern Software Development Using C#.NET by Richard Wiener29 Summary continued  There are two types of copying: shallow and deep copying.  The former copies references but not the content of the references.  As a result, the target reference becomes another name for the source reference. Any change made to the source or target after the copy affects the other.  With deep copying, an independent autonomous target is created with the content of the source copied to the target and with no residual dependency between the source and the target.

Modern Software Development Using C#.NET by Richard Wiener30 Summary continued Never implement a Clone method by invoking the constructor of the containing class. A common and potentially expensive pitfall is using objects of type String instead of StringBuilder when repeatedly concatenating string objects. Objects of type String are immutable. That means that once a String object is constructed, its contents cannot be changed. String objects are of value type. Standard class StringBuilder presents a mutable reference type that may be used as an alternative to String when appropriate.

Modern Software Development Using C#.NET by Richard Wiener31 Summary continued  In a landmark paper “Genericity versus Inheritance” published in the ACM SIGPLAN Notices, Vol 21, No 11 in 1986, Bertrand Meyer presents a clear case that using polymorphism through inheritance to achieve generic behavior is the wrong way to achieve generic behavior.

Modern Software Development Using C#.NET by Richard Wiener32 Summary continued  The use of generic container classes provides for more readable code.  When a generic instance is created, the programmer’s intentions are documented in the code itself.  Any attempt on the part of the programmer to insert an object whose type differs from the type specified when the instance of the generic container is created produces a compile-time error.  This is better than a runtime error that would occur when using the older approach to constructing generic containers.  Discovering errors at compile time is always superior to discovering errors at runtime.

Modern Software Development Using C#.NET by Richard Wiener33 Summary continued The parameters specified in a generic class may be constrained. A generic parameter may be constrained by specifying that it must implement a particular interface or extend a particular class. Methods may be defined that contain one or more generic parameters even though the class that they are defined within contains no generic parameters.

Modern Software Development Using C#.NET by Richard Wiener34 Summary continued Generic container classes move type-checking from the runtime system to the compiler thus making code safer. Generic classes provide for a mechanism for self- documentation since when a generic container instance is created, the type of objects that are intended to be placed inside the container is specified in the code itself. Generic classes remove the need for cluttering one’s code with downcasts. This makes the code easier to read and maintain.

Modern Software Development Using C#.NET by Richard Wiener35 Summary continued Iteration through a container class allows each object in the container to be visited exactly once. Any class that is to support an enumerator must implement the IEnumerable interface. Such a class must meet the contract of implementing the GetEnumerator method that returns an instance of IEnumerator.

Modern Software Development Using C#.NET by Richard Wiener36 Summary continued A class that implements IEnumerator must meet the contract of implementing: The read-only property Current that returns the element in the collection to which the cursor is currently pointing. The method MoveNext that returns a value true if the enumerator has successfully advanced to the next element in the collection and false if there is no next element in the collection. The method Reset that restores the cursor so that it points to the first element in the collection.