C# Object-Oriented Program

Slides:



Advertisements
Similar presentations
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Advertisements

OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Classes, Encapsulation, Methods and Constructors
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
ASP.NET Programming with C# and SQL Server First Edition
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Writing Classes (Chapter 4)
An Object-Oriented Approach to Programming Logic and Design
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.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Object-Based Programming Implementing Multiple Classes Class Scope Controlling Access to Members Initializing Class Objects: Constructors Using Overloaded.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Jozef Goetz,  2011 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved. © by Pearson Education, Inc.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
 2002 Prentice Hall. All rights reserved. 1 Chapter 8 – Object-Based Programming Outline 8.1 Introduction 8.2 Implementing a Time Abstract Data Type with.
Advanced Programming Class & Objects. Example Create class account to save personal account for a bank. The class has account# and balance data The class.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
 2002 Prentice Hall. All rights reserved. 1 Week 4: OOP Questions from last lecture: –Single-subscript arrays –Multiple-subscript arrays –Homework/Practical.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 12.
Jozef Goetz,  2011 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved. © by Pearson Education, Inc.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
Copyright ©2005  Department of Computer & Information Science Object Oriented Concepts.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Topic: Classes and Objects
Object-Oriented Programming Concepts
Classes (Part 1) Lecture 3
Programming Logic and Design Seventh Edition
Inheritance ITI1121 Nour El Kadri.
Object-Oriented Programming: Classes and Objects
Classes and OOP.
Chapter 16: Classes and Data Abstraction
Final and Abstract Classes
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Anatomy of a Class & Method
Classes A class is a blueprint of an object
Chapter 4: Writing Classes
Object-Oriented Programming: Classes and Objects
Object Based Programming
Encapsulation & Visibility Modifiers
Chapter 6 Methods: A Deeper Look
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Classes and Objects.
Object-Oriented Programming: Classes and Objects
Chapter 8 – Object-Based Programming
Outline Anatomy of a Class Encapsulation Anatomy of a Method
CS360 Client/Server Programming Using Java
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
Object-Oriented Programming
Final and Abstract Classes
Chapter 7 Objects and Classes
Presentation transcript:

C# Object-Oriented Program Lecture 05 C# Object-Oriented Program Dr. Eng. Ibrahim El-Nahry

Learning Objectives Essentials of Object-Oriented Programming Defining Object-Oriented Systems C# and Object Orientation C# Classes and Objects Using Encapsulation

Object – Oriented Programming As hardware and software became increasingly complex, quality was often compromised. Researchers studied ways to maintain software quality and developed object-oriented programming in part to address common problems by strongly emphasizing discrete, reusable units of programming logic. The methodology focuses on data rather than processes, with programs composed of self-sufficient modules (objects) each containing all the information needed to manipulate its own data structure.

Object – Oriented Programming The Object-Oriented methodology focuses on data rather than processes, with programs composed of self-sufficient modules (objects) each containing all the information needed to manipulate its own data structure. This is in contrast to the existing modular programming which had been dominant for many years that focused on the function of a module An object-oriented program may thus be viewed as a collection of cooperating objects, as opposed to the conventional model, in which a program is seen as a list of tasks (subroutines) to perform

OOP Fundamental Concepts Fundamental Concepts in OOP are: Class Object Instance Method Abstraction Encapsulation Inheritance Polymorphism

OOP concepts : Objects An object is a unique programming entity that has attributes to describe it (like adjectives in grammar) and methods to retrieve/set attribute values (like verbs in grammar). Attributes Programmers store an object’s data in attributes, also called properties. Attributes provide us a way to describe an object, similar to adjectives in grammar. We can read property values or change properties, assigning values. Methods Whereas attributes describe an object, methods allow us to access object data. Methods are like verbs in grammar. We can manipulate object data, stored in attributes, using methods.

OOP concepts : Abstraction One of the chief advantages of object-oriented programming is the idea that programmers can essentially focus on the “big picture” and ignore specific details regarding the inner-workings of an object. This concept is called abstraction. Events Object-oriented programming is inherently tied to user interaction. Programs record interaction in the form of events. Events are changes in an object’s environment to which it can react. Classes How do programmers get by implementing abstraction? They use a programming structure called a class. A class presents a blueprint of an object, its properties and its methods.

OOP concepts : Instantiation To create an object based on a class, we create an instance of that class. This process is called instantiation. In C#, we use a special method called a constructor method to create an instance of an object. Encapsulation Abstraction in OOP is closely related to a concept called encapsulation. Data and the ways to get at that data are wrapped in a single package, a class. The only way to access such data is through that package. This idea translates to information hiding.

OOP concepts : Inheritance Another of the main tenets of OOP is inheritance. Inheritance allows programmers to create new classes from existing ones. A child class inherits its properties and attributes from its parents, which programmers can change. Polymorphism Polymorphism describes how programmers write methods to do some general purpose function. Different objects might perform polymorphic methods differently.

C# Classes A C# class plays dual roles: Program module: containing a list of (static) method declarations and (static) data fields Blueprint for generating objects It is the model or pattern from which objects are created Supports two techniques which are essence of object-oriented programming “data encapsulation” (for abstraction) “inheritance” (for code reuse)

User-Defined Class A user-defined class is also called a user-defined type class written by a programmer A class encapsulates (wrap together) data and methods: data members (member variables or instance variables) methods that manipulate data members

C# Objects An object has: state - descriptive characteristics behaviors - what it can do (or be done to it) Note the interactions between state and behaviors the behavior of an object might change its state the behavior of an object might depend on its state

Defining Classes A class contains data declarations and method declarations public int x, y; private char ch; class MyClass Data declarations Method declarations Member (data/method) Access Modifiers public : member is accessible outside the class private : member is accessible only inside the class definition

Class membership Classes have member variables and methods Class members are 1 of 3 types Public Private Protected

Public membership Everyone can access the member The rest of the world The class itself Child classes You should avoid making member variables public, in order to prevent undesired modification

Private membership Only the class itself can access the member It’s not visible to the rest of the world Child classes can’t access it either

Protected membership The middle ground between public and private The outside world can’t access it… but derived classes can

Data Declarations Comparison: Local variables You can define two types of variables in a class but not in any method (called class variables) static class variables nonstatic variables are called instance variables (fields) because each instance (object) of the class has its own copy class variables can be accessed in all methods of the class Comparison: Local variables Variables declared within a method or within a block statement Variables declared as local variables can only be accessed in the method or the block where they are declared

Method Declarations A class can define many types of methods, e.g., Access methods : read or display data Predicate methods : test the truth of conditions

Constructors Constructors initialize objects of the class they have the same name as the class There may be more than one constructor per class (overloaded constructors) can take arguments Constructors enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read. If you do not provide a constructor for your object, C# will create one by default that instantiates the object and sets member variables to the default values they do not return any value it has no return type, not even void Static classes can also have constructors

Destructor Destructors are used to destruct instances of classes. Destructors are only used with classes. A class can only have one destructor. Destructors cannot be inherited or overloaded. Destructors cannot be called. They are invoked automatically. A destructor does not take modifiers or have parameters. The destructor implicitly calls Finalize on the base class of the object.

Example: Time1 class We define the Time1 class to represent time: midnight to 11:59:59 The state of a time object can be represented by: hour, minute, second: integers representing time We might define the following methods: a Time1 constructor, to set up the object a SetTime method, to set time a ToUniversalString method, to convert the internal representation to a string representing the time in 24 hour format a ToStandardString method, to convert the internal representation to a string representing the time in 12 hour format

1 // Time1.cs 2 // Class Time1 maintains time in 24-hour format. 3 4 using System; 5 6 // Time1 class definition 7 public class Time1 8 { 9 private int hour; // 0-23 10 private int minute; // 0-59 11 private int second; // 0-59 12 13 // Time1 constructor initializes instance variables to 14 // zero to set default time to midnight 15 public Time1() 16 { 17 SetTime( 0, 0, 0 ); 18 } 19 20 // Set new time value in 24-hour format. Perform validity 21 // checks on the data. Set invalid values to zero. 22 public void SetTime( 23 int hourValue, int minuteValue, int secondValue ) 24 { 25 hour = ( hourValue >= 0 && hourValue < 24 ) ? 26 hourValue : 0; 27 minute = ( minuteValue >= 0 && minuteValue < 60 ) ? 28 minuteValue : 0; 29 second = ( secondValue >= 0 && secondValue < 60 ) ? 30 secondValue : 0; 31 } 32 Default constructor Private instance variables Method SetTime Validate arguments

33 // convert time to universal-time (24 hour) format string 34 public string ToUniversalString() 35 { 36 return String.Format( 37 "{0:D2}:{1:D2}:{2:D2}", hour, minute, second ); 38 } 39 40 // convert time to standard-time (12 hour) format string 41 public string ToStandardString() 42 { 43 return String.Format( "{0}:{1:D2}:{2:D2} {3}", 44 ( ( hour == 12 || hour == 0 ) ? 12 : hour % 12 ), 45 minute, second, ( hour < 12 ? "AM" : "PM" ) ); 46 } 47 48 } // end class Time1 Output time in universal format Output time in standard format

1 // TimeTest1.cs 2 // Demonstrating class Time1. 3 4 using System; 5 using System.Windows.Forms; 6 7 // TimeTest1 uses creates and uses a Time1 object 8 class TimeTest1 9 { 10 // main entry point for application 11 static void Main( string[] args ) 12 { 13 Time1 timeObj1 = new Time1(); // calls Time1 constructor 14 string output; 15 16 // assign string representation of time to output 17 output = "Initial universal time is: " + 18 timeObj1.ToUniversalString() + 19 "\nInitial standard time is: " + 20 timeObj1.ToStandardString(); 21 22 // attempt valid time settings 23 timeObj1.SetTime( 13, 27, 6 ); 24 25 // append new string representations of time to output 26 output += "\n\nUniversal time after SetTime is: " + 27 timeObj1.ToUniversalString() + 28 "\nStandard time after SetTime is: " + 29 timeObj1.ToStandardString(); 30 31 // attempt invalid time settings 32 timeObj1.SetTime( 99, 99, 99 ); 33 Call default time constructor Call method SetTime to set the time with valid arguments Call method SetTime with invalid arguments

34 output += "\n\nAfter attempting invalid settings: " + 35 "\nUniversal time: " + time.ToUniversalString() + 36 "\nStandard time: " + time.ToStandardString(); 37 38 MessageBox.Show( output, "Testing Class Time1" ); 39 40 } // end method Main 41 42 } // end class TimeTest1 Program Output

const and readonly Members Declare constant members (members whose value will never change) using the keyword const const members are implicitly static const members must be initialized when they are declared Use keyword readonly to declare members who will be initialized in the constructor but not change after that

1 // UsingConstAndReadOnly.cs 2 // Demonstrating constant values with const and readonly. 3 4 using System; 5 using System.Windows.Forms; 6 7 // Constants class definition 8 public class Constants 9 { 10 // PI is constant variable 11 public const double PI = 3.14159; 12 13 // radius is a constant variable 14 // that is uninitialized 15 public readonly int radius; 16 17 public Constants( int radiusValue ) 18 { 19 radius = radiusValue; 20 } 21 22 } // end class Constants 23 24 // UsingConstAndReadOnly class definition 25 public class UsingConstAndReadOnly 26 { 27 // method Main creates Constants 28 // object and displays it's values 29 static void Main( string[] args ) 30 { 31 Random random = new Random(); 32 33 Constants constantValues = 34 new Constants( random.Next( 1, 20 ) ); 35 Constant variable PI Readonly variable radius; must be initialized in constructor Initialize readonly member radius

36 MessageBox.Show( "Radius = " + constantValues.radius + 37 "\nCircumference = " + 38 2 * Constants.PI * constantValues.radius, 39 "Circumference" ); 40 41 } // end method Main 42 43 } // end class UsingConstAndReadOnly Program Output

Using the this reference Every object can reference itself by using the keyword this Often used to distinguish between a method’s variables and the instance variables of an object e.g., in constructors

1 // Time4.cs 2 // Class Time2 provides overloaded constructors. 3 4 using System; 5 6 // Time4 class definition 7 public class Time4 8 { 9 private int hour; // 0-23 10 private int minute; // 0-59 11 private int second; // 0-59 12 13 // constructor 14 public Time4( int hour, int minute, int second ) 15 { 16 this.hour = hour; 17 this.minute = minute; 18 this.second = second; 19 } 20 21 // create string using this and implicit references 22 public string BuildString() 23 { 24 return "this.ToStandardString(): " + 25 this.ToStandardString() + 26 "\nToStandardString(): " + ToStandardString(); 27 } 28 The this reference is used to set the class member variables to the constructor arguments The this reference is used to refer to an instance method

29 // convert time to standard-time (12 hour) format string 30 public string ToStandardString() 31 { 32 return String.Format( "{0}:{1:D2}:{2:D2} {3}", 33 ( ( this.hour == 12 || this.hour == 0 ) ? 12 : 34 this.hour % 12 ), this.minute, this.second, 35 ( this.hour < 12 ? "AM" : "PM" ) ); 36 } 37 38 } // end class Time4 The this reference is used to access member variables

1 // ThisTest.cs 2 // Using the this reference. 3 4 using System; 5 using System.Windows.Forms; 6 7 // ThisTest class definition 8 class Class1 9 { 10 // main entry point for application 11 static void Main( string[] args ) 12 { 13 Time4 time = new Time4( 12, 30, 19 ); 14 15 MessageBox.Show( time.BuildString(), 16 "Demonstrating the \"this\" Reference" ); 17 } 18 }

Encapsulation You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods external - the interaction of the object with other part of the world

Encapsulation: An Object As a Black Box From the external view, an object is an encapsulated entity, providing a set of specific services These services define the interface to the object An encapsulated object can be thought of as a black box The user, or client, of an object can request its services, but it should not have to be aware of how those services are accomplished Methods Client Data Can you think of another way to represent the state of an object of Time1?

Accomplish Encapsulation: Access Modifiers In C#, we accomplish encapsulation through the appropriate use of access modifiers An access modifier is a C# keyword that specifies the accessibility of a method, data field, or class We will discuss two access modifiers: public, private We will discuss the other two modifiers (protected, internal) later For more details, please check C# programmer’s reference (linked on the lecture notes page)

The public and private Access Modifiers Classes (types) and members of a class that are declared with public can be accessed from anywhere Members of a type that are declared with private can only be accessed from inside the class Members of a class declared without an access modifier have default private accessibility

1 // RestrictedAccess.cs 2 // Demonstrate compiler errors from attempt to access 3 // private class members. 4 5 class RestrictedAccess 6 { 7 // main entry point for application 8 static void Main( string[] args ) 9 { 10 Time1 time = new Time1(); 11 12 time.hour = 7; 13 time.minute = 15; 14 time.second = 30; 15 } 16 17 } // end class RestrictedAccess RestrictedAccess Program Output Attempt to access private members

Using Access Modifiers to Implement Encapsulation: Data Fields As a general rule, no object's data (state) should be declared with public accessibility How to internally represent the state of an object should be hidden from others Any changes to the object's state (its variables) should be accomplished by that object's methods We should make it difficult, if not impossible, for one object to "reach in" and directly alter another object's state

Implementing Data Encapsulation using Properties Use C# properties to provide access to data safely data members should be declared private, with public properties that allow safe access to them You access the properties of a class as if you were accessing a data field ( i.e., no () ) Public properties allow clients to: Get (obtain the values of) private data get accessor: controls formatting and retrieval of data Set (assign values to) private data set accessor: ensure that the new value is appropriate for the data member

1 // Time3.cs 2 // Class Time2 provides overloaded constructors. 3 4 using System; 5 6 // Time3 class definition 7 public class Time3 8 { 9 private int hour; // 0-23 10 private int minute; // 0-59 11 private int second; // 0-59 12 13 // Time3 constructor initializes instance variables to 14 // zero to set default time to midnight 15 public Time3() 16 { 17 SetTime( 0, 0, 0 ); 18 } 19 20 // Time3 constructor: hour supplied, minute and second 21 // defaulted to 0 22 public Time3( int hour ) 23 { 24 SetTime( hour, 0, 0 ); 25 } 26 27 // Time3 constructor: hour and minute supplied, second 28 // defaulted to 0 29 public Time3( int hour, int minute ) 30 { 31 SetTime( hour, minute, 0 ); 32 } 33

34 // Time3 constructor: hour, minute and second supplied 35 public Time3( int hour, int minute, int second ) 36 { 37 SetTime( hour, minute, second ); 38 } 39 40 // Time3 constructor: initialize using another Time3 object 41 public Time3( Time3 time ) 42 { 43 SetTime( time.Hour, time.Minute, time.Second ); 44 } 45 46 // Set new time value in 24-hour format. Perform validity 47 // checks on the data. Set invalid values to zero. 48 public void SetTime( 49 int hourValue, int minuteValue, int secondValue ) 50 { 51 Hour = hourValue; 52 Minute = minuteValue; 53 Second = secondValue; 54 } 56 // property Hour 57 public int Hour 58 { 59 get 60 { 61 return hour; 62 } 63 64 set 65 { 66 hour = ( ( value >= 0 && value < 24 ) ? value : 0 ); 67 } } // end property Hour Property Hour Constructor that takes another Time3 object as an argument. New Time3 object is initialized with the values of the argument.

71 // property Minute 72 public int Minute 73 { 74 get 75 { 76 return minute; 77 } 78 79 set 80 { 81 minute = ( ( value >= 0 && value < 60 ) ? value : 0 ); 82 } 83 84 } // end property Minute 85 86 // property Second 87 public int Second 88 { 89 get 90 { 91 return second; 92 } 93 94 set 95 { 96 second = ( ( value >= 0 && value < 60 ) ? value : 0 ); 97 } 98 99 } // end property Second 100 Property Minute Property Second

101 // convert time to universal-time (24 hour) format string 102 public string ToUniversalString() 103 { 104 return String.Format( 105 "{0:D2}:{1:D2}:{2:D2}", Hour, Minute, Second ); 106 } 107 108 // convert time to standard-time (12 hour) format string 109 public string ToStandardString() 110 { 111 return String.Format( "{0}:{1:D2}:{2:D2} {3}", 112 ( ( Hour == 12 || Hour == 0 ) ? 12 : Hour % 12 ), 113 Minute, Second, ( Hour < 12 ? "AM" : "PM" ) ); 114 } 115 116 } // end class Time3

1 // TimeTest3.cs 2 // Demonstrating Time3 properties Hour, Minute and Second. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; 10 11 // TimeTest3 class definition 12 public class TimeTest3 : System.Windows.Forms.Form 13 { 14 private System.Windows.Forms.Label hourLabel; 15 private System.Windows.Forms.TextBox hourTextBox; 16 private System.Windows.Forms.Button hourButton; 17 18 private System.Windows.Forms.Label minuteLabel; 19 private System.Windows.Forms.TextBox minuteTextBox; 20 private System.Windows.Forms.Button minuteButton; 21 22 private System.Windows.Forms.Label secondLabel; 23 private System.Windows.Forms.TextBox secondTextBox; 24 private System.Windows.Forms.Button secondButton; 25 26 private System.Windows.Forms.Button addButton; 27 28 private System.Windows.Forms.Label displayLabel1; 29 private System.Windows.Forms.Label displayLabel2; 30 31 // required designer variable 32 private System.ComponentModel.Container components = null; 33 34 private Time3 time; 35

36 public TimeTest3() 37 { 38 // Required for Windows Form Designer support 39 InitializeComponent(); 40 41 time = new Time3(); 42 UpdateDisplay(); 43 } 44 45 // Visual Studio .NET generated code 46 47 // main entry point for application 48 [STAThread] 49 static void Main() 50 { 51 Application.Run( new TimeTest3() ); 52 } 53 54 // update display labels 55 public void UpdateDisplay() 56 { 57 displayLabel1.Text = "Hour: " + time.Hour + 58 "; Minute: " + time.Minute + 59 "; Second: " + time.Second; 60 displayLabel2.Text = "Standard time: " + 61 time.ToStandardString() + "\nUniversal time: " + 62 time.ToUniversalString(); 63 } 64

65 // set Hour property when hourButton pressed 66 private void hourButton_Click( 67 object sender, System.EventArgs e ) 68 { 69 time.Hour = Int32.Parse( hourTextBox.Text ); 70 hourTextBox.Text = ""; 71 UpdateDisplay(); 72 } 73 74 // set Minute property when minuteButton pressed 75 private void minuteButton_Click( 76 object sender, System.EventArgs e ) 77 { 78 time.Minute = Int32.Parse( minuteTextBox.Text ); 79 minuteTextBox.Text = ""; 80 UpdateDisplay(); 81 } 82 83 // set Second property when secondButton pressed 84 private void secondButton_Click( 85 object sender, System.EventArgs e ) 86 { 87 time.Second = Int32.Parse( secondTextBox.Text ); 88 secondTextBox.Text = ""; 89 UpdateDisplay(); 90 } 91 92 // add one to Second when addButton pressed 93 private void addButton_Click( 94 object sender, System.EventArgs e ) 95 { 96 time.Second = ( time.Second + 1 ) % 60; 97 Set Hour property of Time3 object Set Minute property of Time3 object Set Second property of Time3 object Add 1 second to Time3 object

98 if ( time.Second == 0 ) 99 { 100 time.Minute = ( time.Minute + 1 ) % 60; 101 102 if ( time.Minute == 0 ) 103 time.Hour = ( time.Hour + 1 ) % 24; 104 } 105 106 UpdateDisplay(); 107 } 108 109 } // end class TimeTest3 Program Output

Summary Programming objects are comprised of attributes and methods. Classes provide programmers with blueprints of objects. To create an object from a class, we use constructor methods to create a class instance. Abstraction in OOP is closely related to a concept called encapsulation. Variables declared in a larger scope can be accessed in an enclosed scope, e.g. Variables declared in a class can be accessed by the methods in the class Variables declared in a method can only be accessed in the method Variables declared in a block can only be accessed in the block