chapter 6 oop: creating object-oriented programs programming in c#.net © 2003 by the mcgraw-hill...

56
Chapter 6 OOP: Creating Object- Oriented Programs Programming in C# .NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

Upload: shona-quinn

Post on 02-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

Chapter 6OOP: Creating

Object-Oriented Programs

Programming in C# .NET

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

Page 2: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 2

Objectives

• Use the object-oriented terminology correctly• Create a two-tier application that separates the user

interface from the business logic• Differentiate between a class and an object• Create a class that has properties and methods• Use property accessor methods to set and retrieve

private properties of a class

Page 3: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 3

Objectives cont.

• Declare object variables and assign values to the properties with a constructor or property accessor method

• Instantiate an object in a project using your class• Differentiate between static members and instance

members• Understand the purpose of the constructor and

destructor methods• Inherit a new class form your own class• Apply visual inheritance by deriving a form from

another form

Page 4: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 4

Object-Oriented Programming

• Objects have properties and methods and generate events

• Classes for all objects are predefined or defined by a programmer

• Object-oriented programming (OOP) is currently the most accepted style of programming

Page 5: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 5

Objects

• Create a new object type by creating a new class with properties and methods– Properties are characteristics– Methods are actions

• To create a new object you instantiate an object of the class

• The new object is an instance of the class• To call a method, refer to Object.Method

Page 6: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 6

Object-Oriented Terminology

• Encapsulation is the combination of characteristics of an object along with its behaviors– One “package” holds the definition of all

properties, methods, and events– Cannot make up new properties or methods– Also called data hiding

Page 7: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 7

Object-Oriented Terminology cont.

• Inheritance is the ability to create a new class from an existing class– Create a new class from an existing class to add

or modify class variables and methods– Original class is the base class, superclass, or

parent class– Inherited class is the subclass, derived class, or

child class

Page 8: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 8

Object-Oriented Terminology cont.

• In the statementpublic class Form1 : System.Windows.Forms.Form

the colon (:) indicates inheritance, Systems.Windows.Forms is the base class and Form1 is the derived class

• The purpose of inheritance is reusability• Create a hierarchy of classes by placing common

code in a base class then creating other derived classes from it

Page 9: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 9

Object-Oriented Terminology cont.

• Polymorphism is the ability to take on many shapes or forms

• Polymorphism allows methods with the same names to have different implementations

• Polymorphism uses overloading and overriding• In overriding, a subclass has a method identically

named in the base class, and the subclass method overrides the method in the base class

Page 10: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 10

Reusable Classes

• A big advantage of object-oriented programming over traditional programming is the ability to reuse classes

• Use a new class in multiple projects

• Similar to working with built-in controls in the toolbox

Page 11: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 11

Multitier Applications

• Each function of a multitier application is coded in a separate component

• Three-tier applications include:– Presentation tier – Present the user interface– Business tier – Handle the data– Data tier – Retrieve and store data in a database

• An “n-tier” application is the expansion of three-tier model

Page 12: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 12

Classes

• Use the new keyword to create an instance of a class which is called instantiating an object

• General form is new className();

• Examples:Font MyFont = new Font(“Arial”,12);

messageLabel.Font = MyFont;

messageLabel.Font = new Font(“Arial”,12);

Page 13: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 13

Specifying a Namespace

• A fully qualified name includes the complete namespace and class name– Example: System.Windows.Forms.TextBox where

System.Windows.Forms is the namespace

• Entire namespace not required for System, System.Windows.Forms, and System.Drawing

• Add a using statement at beginning of code file to specify the namespace to be used then only refer to class name

Page 14: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 14

Designing Your Own Class

• Analyze the characteristics and behaviors that your object needs– Characteristics become properties– Behaviors become methods

Page 15: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 15

Property Blocks

• Define private member variables to store values for the class

• The class controls access to its properties through accessor methods in a property block– get accessor method used to retrieve a property value– set accessor method used to assign a value to a property

• set statement uses value keyword to refer to incoming value for property

• Property block must be public

Page 16: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 16

Property Blocks cont.

• Property Block – General Formprivate DataType MemberVariable;public DataType Property Name{

get{

return MemberVariable;}set{

//statements, such as validationMemberVariable = value;

}}

Page 17: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 17

Read-Only Properties• Write a property block that contains only a

get to create a read-only property

private decimal decExtendedPrice;

public decimal ExtendedPrice{

get{

return decExtendedPrice;}

{

Page 18: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 18

Write-Only Properties

• Write a property block that contains only a set to create a write-only property

private decimal decPrice;

public decimal Price{

set{

decPrice = value;}

{

Page 19: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 19

Class Methods

• Create methods of the new class by coding public methods within the class

• Methods declared with private are available only within the class

• Methods declared with public are available to external objects created from this or other classes

Page 20: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 20

Constructors and Destructors

• A constructor is a method that executes automatically when an object is instantiated

• A destructor is a method that executes automatically when an object is destroyed

• A constructor has the same name as the class

Page 21: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 21

Constructors

• Executes before any other code in the class

• Must be public

• Empty constructor automatically created when you add a new class to project

• If class does not have a constructor, the compiler creates a method called the default constructor with an empty argument list

Page 22: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 22

Overloading the Constructor

• Create overloaded methods in your class by giving the same name to multiple methods, each with a different argument list

• A parameterized constructor requires arguments

• Use the this keyword to refer to the current class in the class code

Page 23: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 23

Creating a New Class – Step-by-Step

In this step-by-step tutorial, you will create a new class to hold book sale information for R ‘n R.

1. Begin the Project2. Begin a New Class3. Define the Class Properties4. Add the Property Blocks5. Add the Method6. Add the Constructor7. Add General Remarks

Page 24: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 24

Creating a New Object Using a Class

• Creating a class defines a new type but does not create any objects

• Two steps to create a new object of a class1. Declare a variable for the new object

2. Instantiate the object using the new keyword

• Use public or private followed by the class name and identifier to refer to the object of the class

Page 25: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 25

Creating a New Object Using a Class cont.

• Declare the variableprivate BookSale bookSaleObject;

• Create the objectbookSaleObject = new BookSale();

• Combine declaration and instantiation of an object in the same statement

BookSale bookSaleObject = new BookSale();

• Should enclose instantiation in try/catch block if converting and passing values from a textbox

Page 26: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 26

Defining and Using a New Object – Step-by-Step

To continue the step-by-step tutorial for the BookSale class, the next step is to design the form for the user interface. The form has text boxes for the user to enter the title, quantity, and price, a menu choice to calculate the sale (the extended price), and another menu item to exit.

Page 27: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 27

Defining and Using a New Object – Step-by-Step cont.

In the Calculate Sale event handler, you will create an instance of the BookSale class and assign the input values for title, quantity, and price to the properties of the BookSale object. The ExtendedPrice property in the BookSale class retrieves the amount of the sale, which appears in a label on the form.

Page 28: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 28

Defining and Using a New Object – Step-by-Step cont.

Placing all calculations in a separate class is a good thing. You are seeing your first example of dividing a program into a Presentation tier and a Business tier.

1. Create the Form2. Add Comments3. Declare the New Object4. Write the Code5. Save Your Work6. Run the Project

Page 29: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 29

Single-Step the Execution

• A quick and easy way to debug is to single-step program execution

• Must be in break time to single-step– Place a breakpoint in code– Run the program– When program stops at breakpoint, press the

F11 key to single-step through

Page 30: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 30

Instance Variables versus Static Variables

• Instance members exist once for each object of a class

• Static members exist once for all objects of a class

• Methods, variables, and properties can be declared as instance or static

• Access static members without instantiating an object of the class

Page 31: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 31

Instance Variables versus Static Variables cont.

• Must reference static members with ClassName.Property or ClassName.Name.Method

• Use the static keyword to create a static member[public|private] static Datatype VariableName;

[public|private] static Datatype MethodName(ArgumentList);

• Must use static keyword on property block if you will retrieve the property without first creating an instance of the class

Page 32: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 32

Adding Static Properties to the Step-by-Step Tutorial

You will now make the BookSale class calculate the total of all sales and a count of the number of sales. You will need static properties for the sales total and sales count in the class. Then on the form, you will add a menu option for Summary that displays the totals in a message box.

Page 33: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 33

Adding Static Properties to the Step-by-Step Tutorial cont.

1. Add Static Properties to the Class

2. Modify the Code to Calculate the Totals

3. Modify the Form

4. Test the Program

5. Save This Version of the Program

Page 34: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 34

Destructors

• Write processing to occur when an object goes out of scope in a destructor

• Destructor is a method with the same name as the class preceded by a tilde (~)

• Destructor method automatically calls the Object.Finalize method from the base class of the object

• Destructor called as part of the garbage collection by the CLR

Page 35: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 35

Garbage Collection

• Garbage collection feature of .NET CLR cleans up unused components

• Garbage collector periodically checks for unreferenced objects and releases all memory and system resources used by the objects

• Microsoft recommends you rely on garbage collection to release resources

Page 36: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 36

Inheritance

• A new class can inherit from a .NET class or one of your own classes

• Inheritance statement format:public class NewClass : BaseClass

• Inheritance clause must follow the class header before any comments

Page 37: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 37

Overriding Methods

• A new method overrides the base class method with the same name

• Derived class will use the new method, not the method in the base class with the same name

• To override a method in C#– Declare the original method with the virtual or abstract

keyword

– Declare the new method with the override keyword

Page 38: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 38

Overriding Methods cont.• Examples:

– Base Classprotected virtual decimal CalculatedExtendedPrice()

– Inherited Classprotected overrride decimal CalculatedExtendedPrice()

• You can use the virtual, abstract, or override keywords on a method that can be overriden– Use virtual when writing a new method that has code– Use abstract for a method header for an empty method– Use override when the method is overriding a method

in its base class

Page 39: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 39

Accessing Properties

• Call the base class constructor from the derived-class constructor

• After assigning values to properties in the base class, you can refer to the properties in the base class from the derived class

• Read-only or write-only properties cannot be accessed by name from a derived class

Page 40: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 40

Adding Inheritance to the Step-by-Step Tutorial

This continuation of the chapter step-by-step tutorial includes a new subclass, overriding a method, and adding a new property.

1. Add the New Class

2. Add the Constructor

3. Add the New Property

4. Add a Constant

5. Override a Method

6. Allow a Method to be Inherited

7. Modify the Form to Use the Inherited Class

Page 41: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 41

Creating a Base Class Strictly for Inheritance• Include the abstract modifier in the class declaration

of a base class that will be inherited and methods that will be overridden

//Base Classpublic abstract class BaseClass{

public abstract void SomeMethod(){

//No code allowed here}

}

Page 42: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 42

Inheriting Form Classes

• Visual inheritance is designing one form then inheriting any other forms from the first

• Base class inherits from System.Windows.Forms.Form and new forms inherit from the base class

• Include design elements and controls, and write methods and declare variables in the base class

• Write methods in base class with virtual or abstract then write method with same name and override keyword in the subclass

Page 43: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 43

Create an Inherited Form Class

1. Select Project/Add Windows Form and enter name for new form; Modify inheritance clause to inherit from base form using project name as namespace

or2. Select Project/Add Inherited Form and

type name of new form; Enter information into dialog box

Page 44: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 44

Coding for Events of an Inherited Class

• You cannot double-click on an inherited control to open the event-handling method

• Instead, you can either copy the method from the base class into the derived class as a start, or use the inherited event handler in the derived class

Page 45: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 45

Managing Multiclass Projects

• Form classes must be in separate files• Multiple classes can be in one file• Each C# file automatically contains a namespace

statement– View and modify namespace in the Project Properties

dialog box– Modify the namespace property at the top of each

existing file• Modify the namespace statement if you move or

copy a class or form file to a new project or include a using statement in the class file

Page 46: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 46

Add an Existing Class File to a Project

• It is best to place the class file in the project folder but not required

• Select Project/Add Existing Item to add an existing file to a project

• Modify the namespace statement in the file to match the project

Page 47: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 47

Displaying Values on a Different Form

• Reference controls on another form using the identifier for the form instance

• Write the form reference as FormInstance.ControlName.Property

• If the form to which you refer is in a different namespace, you must also include a reference to the namespace or include a using statement

Page 48: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 48

Using the Object Browser

• The Object Browser will show names of objects, properties, methods, events and constants for:– C# objects– your own objects– objects available from other applications

• Choose View/Other Windows/Object Browser or drop down the window list from the View toolbar button

• Choose libraries/namespaces in the Browse list• Icons represent properties, methods, events,

constants, classes, and namespaces

Page 49: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 49

Examining C# and Your Own Classes

• You can see what elements are defined in the class, what is the base class, and which properties, methods, and events are inherited

• Double-click on a name in the Members list to jump to the definition of any property or method

• Select a form in the Objects list and double-click on method name to view

Page 50: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 50

Your Hands-On Programming Example

This program must calculate books sales for R ‘n R, with a discount of 15 percent for students. The project will use the BookSale and StudentBookSale classes developed in the chapter step-by-step.

Create a project with multiple forms that have a shared design element. Include a main form, an About form, and a Summary form that displays the sales summary information.

Page 51: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 51

Your Hands-On Programming Example cont.

Design a base form to use for inheritance and make the other three forms inherit from the base form. The About form and Summary form must have an OK button, which closes the form. The main form will have menus and no OK button.

Page 52: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 52

Summary

• Objects have properties and methods and can trigger events.

• Create a new class that can be used to create new objects.

• Creating a new object is instantiating the object. The object is an instance of the class.

• Encapsulation is the combination of the characteristics and behaviors of an item into a single class definition.

• Polymorphism allows different classes of objects to have similarly named methods that behave differently.

Page 53: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 53

Summary cont.

• Inheritance provides a means to derive a new class based on an existing class.

• A big advantage of OOP is that classes can be reused in many applications.

• Multitier applications separate functions into tiers.• Variables inside a class used to store properties

should be private.• Use property blocks with accessor methods (get

and set) to make properties of a class available outside the class.

Page 54: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 54

Summary cont.

• Read-only properties have only a get accessor method. Write-only properties have only a set accessor method.

• Public methods of a class are available to other objects of the class and other classes.

• Use the new keyword to instantiate an object of a class.

• A constructor is a method that executes automatically when an object is created. A destructor executes automatically when the object is destroyed.

Page 55: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 55

Summary cont.

• A constructor method must be named the same as the class and may be overloaded.

• A parameterized constructor requires arguments to create a new object.

• Static members have one copy that is used by all objects of the class. Instance members have one copy for each instance of the object.

• Garbage collection checks for and destroys unreferenced objects, and releases resources.

• A subclass inherits all public and protected properties and methods of its base class.

Page 56: Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.6- 56

Summary cont.

• To override a base class method, it must be declared as virtual or abstract, and the new method must use the override keyword.

• Use visual inheritance to derive new forms from existing forms.

• Each project has a default namespace. Modify the namespace statement at the top of existing files.

• Refer to controls on a different form with form instance name, control name, and property.

• Use Object Browser to view classes, properties, methods, events, and constants in all classes.