 2006 Pearson Education, Inc. All rights reserved. 1 9 9 Classes and Objects: A Deeper Look.

Slides:



Advertisements
Similar presentations
 Pearson Education, Inc. All rights reserved static Class Members static fields – Also known as class variables – Represents class-wide.
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Classes: A Deeper Look Systems Programming.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look - modified by Eileen Kraemer.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
ASP.NET Programming with C# and SQL Server First Edition
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
 2006 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Dale Roberts Object Oriented Programming using Java - Packages Dale Roberts, Lecturer Computer Science, IUPUI Department.
 2009 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 10 Classes and Objects: A Deeper Look Visual C# 2010 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2009 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Object Oriented Programming
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
 2009 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Classes and Objects: A Deeper Look
Classes and Objects: A Deeper Look
Polymorphism, Interfaces & Operator Overloading
Object-Oriented Programming: Inheritance
Object Oriented Programming using Java - Class Instance Variables
Introduction to Classes
Object Based Programming
Introduction to Classes
Classes and Objects: A Deeper Look
Chapter 6 Methods: A Deeper Look
Lecture 22 Inheritance Richard Gesick.
10.2 const (Constant) Objects and const Member Functions
Object Oriented Programming in java
Classes: A Deeper Look, Part 1
Presentation transcript:

 2006 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look

 2006 Pearson Education, Inc. All rights reserved. 2 Instead of this absurd division into sexes, they ought to class people as static and dynamic. — Evelyn Waugh Is it a world to hide virtues in? — William Shakespeare But what, to serve our private ends, Forbids the cheating of our friends? — Charles Churchill

 2006 Pearson Education, Inc. All rights reserved. 3 Don’t be “consistent,” but be simply true. — Oliver Wendell Holmes, Jr. This above all: to thine own self be true. — William Shakespeare

 2006 Pearson Education, Inc. All rights reserved. 4 OBJECTIVES In this chapter you will learn:  Encapsulation and data hiding.  The concepts of data abstraction and abstract data types (ADTs).  To use keyword this.  To use indexers to access members of a class.  To use static variables and methods.  To use readonly fields.  To take advantage of C# ’ s memory management features.  How to create a class library.  When to use the internal access modifier.

 2006 Pearson Education, Inc. All rights reserved Introduction 9.2 Time Class Case Study 9.3 Controlling Access to Members 9.4 Referring to the Current Object’s Members with the this Reference 9.5 Indexers 9.6 Time Class Case Study: Overloaded Constructors 9.7 Default and Parameterless Constructors 9.8 Composition 9.9 Garbage Collection and Destructors 9.10 static Class Members 9.11 readonly Instance Variables 9.12 Software Reusability 9.13 Data Abstraction and Encapsulation 9.14 Time Class Case Study: Creating Class Libraries 9.15 internal Access 9.16 Class View and Object Browser 9.17 (Optional) Software Engineering Case Study: Starting to Program the Classes of the ATM System 9.18 Wrap-Up

 2006 Pearson Education, Inc. All rights reserved. 6 Outline Time1.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 7 Outline Time1.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 8 Software Engineering Observation 9.1 Methods and properties that modify the values of private variables should verify that the intended new values are proper. If they are not, they should place the private variables in an appropriate consistent state.

 2006 Pearson Education, Inc. All rights reserved. 9 Outline Time1Test.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 10 Outline Time1Test.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 11 Software Engineering Observation 9.2 Classes simplify programming because the client can use only the public members exposed by the class. Such members are usually client oriented rather than implementation oriented. Clients are neither aware of, nor involved in, a class’s implementation. Clients generally care about what the class does but not how the class does it. (Clients do, of course, care that the class operates correctly and efficiently.)

 2006 Pearson Education, Inc. All rights reserved. 12 Software Engineering Observation 9.3 Interfaces change less frequently than implementations. When an implementation changes, implementation-dependent code must change accordingly. Hiding the implementation reduces the possibility that other application parts will become dependent on class-implementation details.

 2006 Pearson Education, Inc. All rights reserved. 13 Common Programming Error 9.1 An attempt by a method that is not a member of a class to access a private member of that class is a compilation error.

 2006 Pearson Education, Inc. All rights reserved. 14 Outline MemberAccessTest.cs

 2006 Pearson Education, Inc. All rights reserved. 15 Outline ThisTest.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 16 Outline ThisTest.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 17 Common Programming Error 9.2 It is often a logic error when a method contains a parameter or local variable that has the same name as an instance variable of the class. In such a case, use reference this if you wish to access the instance variable of the class—otherwise, the method parameter or local variable will be referenced.

 2006 Pearson Education, Inc. All rights reserved. 18 Error-Prevention Tip 9.1 Avoid method parameter names or local variable names that conflict with field names. This helps prevent subtle, hard-to-locate bugs.

 2006 Pearson Education, Inc. All rights reserved. 19 Performance Tip 9.1 C# conserves memory by maintaining only one copy of each method per class—this method is invoked by every object of the class. Each object, on the other hand, has its own copy of the class’s instance variables (i.e., non- static variables). Each method of the class implicitly uses the this reference to determine the specific object of the class to manipulate.

 2006 Pearson Education, Inc. All rights reserved. 20 Common Programming Error 9.3 Declaring indexers as static is a syntax error.

 2006 Pearson Education, Inc. All rights reserved. 21 Outline Box.cs (1 of 3)

 2006 Pearson Education, Inc. All rights reserved. 22 Outline Box.cs (2 of 3)

 2006 Pearson Education, Inc. All rights reserved. 23 Outline Box.cs (3 of 3)

 2006 Pearson Education, Inc. All rights reserved. 24 Outline BoxTest.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 25 Outline BoxTest.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 26 Outline Time2.cs (1 of 4)

 2006 Pearson Education, Inc. All rights reserved. 27 Outline Time2.cs (2 of 4)

 2006 Pearson Education, Inc. All rights reserved. 28 Outline Time2.cs (3 of 4)

 2006 Pearson Education, Inc. All rights reserved. 29 Outline Time2.cs (4 of 4)

 2006 Pearson Education, Inc. All rights reserved. 30 Common Programming Error 9.4 A constructor can call methods of the class. Be aware that the instance variables might not yet be in a consistent state, because the constructor is in the process of initializing the object. Using instance variables before they have been initialized properly is a logic error.

 2006 Pearson Education, Inc. All rights reserved. 31 Software Engineering Observation 9.4 When one object of a class has a reference to another object of the same class, the first object can access all the second object’s data and methods (including those that are private ).

 2006 Pearson Education, Inc. All rights reserved. 32 Software Engineering Observation 9.5 When implementing a method of a class, use the class’s properties to access the class’s private data. This simplifies code maintenance and reduces the likelihood of errors.

 2006 Pearson Education, Inc. All rights reserved. 33 Outline Time2Test.cs (1 of 3)

 2006 Pearson Education, Inc. All rights reserved. 34 Outline Time2Test.cs (2 of 3)

 2006 Pearson Education, Inc. All rights reserved. 35 Outline Time2Test.cs (3 of 3)

 2006 Pearson Education, Inc. All rights reserved. 36 Common Programming Error 9.5 If a class has constructors, but none of the public constructors are parameterless constructors, and an application attempts to call a parameterless constructor to initialize an object of the class, a compilation error occurs. A constructor can be called with no arguments only if the class does not have any constructors (in which case the default constructor is called) or if the class has a public parameterless constructor.

 2006 Pearson Education, Inc. All rights reserved. 37 Common Programming Error 9.6 Only constructors can have the same name as the class. Declaring a method, property or field with the same name as the class is a compilation error.

 2006 Pearson Education, Inc. All rights reserved. 38 Software Engineering Observation 9.6 One form of software reuse is composition, in which a class has as members references to objects of other classes.

 2006 Pearson Education, Inc. All rights reserved. 39 Outline Date.cs (1 of 5)

 2006 Pearson Education, Inc. All rights reserved. 40 Outline Date.cs (2 of 5)

 2006 Pearson Education, Inc. All rights reserved. 41 Outline Date.cs (3 of 5)

 2006 Pearson Education, Inc. All rights reserved. 42 Outline Date.cs (4 of 5)

 2006 Pearson Education, Inc. All rights reserved. 43 Outline Date.cs (5 of 5)

 2006 Pearson Education, Inc. All rights reserved. 44 Outline Employee.cs

 2006 Pearson Education, Inc. All rights reserved. 45 Outline EmployeeTest.cs

 2006 Pearson Education, Inc. All rights reserved. 46 Software Engineering Observation 9.7 A class that uses system resources, such as files on disk, should provide a method to eventually release the resources. Many FCL classes provide Close or Dispose methods for this purpose.

 2006 Pearson Education, Inc. All rights reserved. 47 Software Engineering Observation 9.8 Use a static variable when all objects of a class must use the same copy of the variable.

 2006 Pearson Education, Inc. All rights reserved. 48 Common Programming Error 9.7 It is a compilation error to access or invoke a static member by referencing it through an instance of the class, like a non- static member.

 2006 Pearson Education, Inc. All rights reserved. 49 Software Engineering Observation 9.9 Static variables and methods exist, and can be used, even if no objects of that class have been instantiated.

 2006 Pearson Education, Inc. All rights reserved. 50 Outline Employee.cs (1 of 3)

 2006 Pearson Education, Inc. All rights reserved. 51 Outline Employee.cs (2 of 3)

 2006 Pearson Education, Inc. All rights reserved. 52 Outline Employee.cs (3 of 3)

 2006 Pearson Education, Inc. All rights reserved. 53 Outline EmployeeTest.cs (1 of 3)

 2006 Pearson Education, Inc. All rights reserved. 54 Outline EmployeeTest.cs (2 of 3)

 2006 Pearson Education, Inc. All rights reserved. 55 Outline EmployeeTest.cs (3 of 3)

 2006 Pearson Education, Inc. All rights reserved. 56 Common Programming Error 9.8 A compilation error occurs if a static method calls an instance (non- static ) method in the same class by using only the method name. Similarly, a compilation error occurs if a static method attempts to access an instance variable in the same class by using only the variable name.

 2006 Pearson Education, Inc. All rights reserved. 57 Common Programming Error 9.9 Referring to the this reference in a static method is a syntax error.

 2006 Pearson Education, Inc. All rights reserved. 58 Software Engineering Observation 9.10 Declaring an instance variable as readonly helps enforce the principle of least privilege. If an instance variable should not be modified after the object is constructed, declare it to be readonly to prevent modification.

 2006 Pearson Education, Inc. All rights reserved. 59 Common Programming Error 9.10 Attempting to modify a readonly instance variable anywhere but its declaration or the object’s constructors is a compilation error.

 2006 Pearson Education, Inc. All rights reserved. 60 Error-Prevention Tip 9.2 Attempts to modify a readonly instance variable are caught at compilation time rather than causing execution-time errors. It is always preferable to get bugs out at compile time, if possible, rather than allowing them to slip through to execution time (where studies have found that repairing is often many times more costly).

 2006 Pearson Education, Inc. All rights reserved. 61 Outline Increment.cs

 2006 Pearson Education, Inc. All rights reserved. 62 Outline IncrementTest.cs

 2006 Pearson Education, Inc. All rights reserved. 63 Software Engineering Observation 9.11 If a readonly instance variable is initialized to a constant only in its declaration, it is not necessary to have a separate copy of the instance variable for every object of the class. The variable should be declared const instead. Constants declared with const are implicitly static, so there will only be one copy for the entire class.

 2006 Pearson Education, Inc. All rights reserved. 64 Good Programming Practice 9.1 Avoid reinventing the wheel. Study the capabilities of the FCL. If the FCL contains a class that meets your application’s requirements, use that class rather than create your own.

 2006 Pearson Education, Inc. All rights reserved. 65 Software Engineering Observation 9.12 Programmers create types through the class mechanism. New types can be designed to be as convenient to use as the simple types. This marks C# as an extensible language. Although the language is easy to extend via new types, the programmer cannot alter the base language itself.

 2006 Pearson Education, Inc. All rights reserved. 66 Outline Time1.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 67 Outline Time1.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 68 Fig | Creating a Class Library Project.

 2006 Pearson Education, Inc. All rights reserved. 69 Fig | Adding a Reference.

 2006 Pearson Education, Inc. All rights reserved. 70 Outline Time1NamespaceTest.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 71 Outline Time1NamespaceTest.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 72 Outline InternalAccessTest.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 73 Outline InternalAccessTest.cs (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 74 Fig | Class View of class Time1 (Fig. 9.1) and class TimeTest (Fig. 9.2).

 2006 Pearson Education, Inc. All rights reserved. 75 Fig | Object Browser for class Math.

 2006 Pearson Education, Inc. All rights reserved. 76 Fig | Class diagram with visibility markers.

 2006 Pearson Education, Inc. All rights reserved. 77 Fig | Class diagram with navigability arrows.

 2006 Pearson Education, Inc. All rights reserved. 78 Outline Withdrawal.cs

 2006 Pearson Education, Inc. All rights reserved. 79 Outline Withdrawal.cs

 2006 Pearson Education, Inc. All rights reserved. 80 Outline Withdrawal.cs

 2006 Pearson Education, Inc. All rights reserved. 81 Outline Account.cs

 2006 Pearson Education, Inc. All rights reserved. 82 Software Engineering Observation 9.13 Many UML modeling tools can convert UML- based designs into C# code, considerably speeding up the implementation process. For more information on these “automatic” code generators, refer to the Web resources listed at the end of Section 3.10.

 2006 Pearson Education, Inc. All rights reserved. 83 Outline Account.cs (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 84 Outline Account.cs (2 of 2)