Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.

Similar presentations


Presentation on theme: "Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14."— Presentation transcript:

1 Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14

2 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 2 Objectives Understand the differences between classes and objects Understand object terminology, including encapsulation, inheritance, and polymorphism Know how to create your own classes

3 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 3 Objectives (cont.) Know how to write constructors with multiple parameter lists Develop applications that use classes and objects

4 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 4 14-1 Writing Objects VB.NET is an object-oriented language. Forms are objects. Controls are objects. Controls are objects that have GUI properties and methods.

5 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 5 Classes and Objects An object is a combination of data and actions that can be treated as a unit. A class is the structure of an object, a blueprint that describes the properties (data) and methods (actions) of an object. An object is created from a class.

6 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 6 14-2 Object-Oriented Terminology A language is considered to be object- oriented if it supports three main features: –Encapsulation –Inheritance –Polymorphism

7 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 7 Encapsulation Encapsulation refers to grouping related properties and methods so they can be treated as a single unit or object. Encapsulation also refers to protecting the inner contents of an object from being damaged or incorrectly referenced by external code.

8 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 8 Encapsulation (cont.) One of the basic rules of encapsulation is that class data should be modified or retrieved only through property procedures. Limiting how external code interacts with the object allows for later modification without risk of compatibility problems.

9 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 9 Encapsulation (cont.) Encapsulation allows you to control how the data and procedures are used. You should declare internal details of a class as Private to prevent them from being used outside your class; this technique is called data hiding.

10 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 10 Inheritance Inheritance describes the ability to create new classes based on an existing class. The existing class is called the base class, and the new class derived from the base class is called the derived class.

11 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 11 Inheritance (cont.) The derived class inherits all the properties, methods, and events of the base class and can be customized with additional properties and methods. Inheritance takes code reuse to a whole new level.

12 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 12 Inheritance (cont.)

13 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 13 Polymorphism Polymorphism is the ability for objects from different classes to respond appropriately to identical method names or operators. Polymorphism is essential to object- oriented programming because it allows you to use shared names, and the system will apply the appropriate code for the particular object.

14 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 14 14-3 Creating Your Own Classes A class definition consists of fields, properties, and methods. A field is a variable in the class and is usually private. A property is a programming construct that typically provides the interface to a field in a class.

15 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 15 14-3 Creating Your Own Classes (cont.) A method is a function or a sub procedure within a class. The class definition also may contain constructor methods that are called when a new object is instantiated from the class.

16 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 16 Fields Fields provide storage for the data in an object and are treated just like variables. For this text, all field values will be declared Private and all field names will begin with “F”. –[Public|Private] fieldname As datatype

17 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 17 Properties Private fields of a class cannot be accessed by external code. If you want an object’s field data to be read or changed, you should include property procedures in the class definition. The Get property procedure typically retrieves a Private field.

18 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 18 Properties (cont.) The Set property procedure typically assigns a new value to a Private field. Some fields are intended to be read-only, meaning external code can view the value of the field, but cannot change its value.

19 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 19 Methods Methods are procedures defined within a class. Methods have access to all data within the object – even Private data. [Private|Public] Sub procedurename([parameters]) [statements] End Sub

20 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 20 Constructors A constructor is a special method that executes during the creation of an object. All constructor methods are procedures named New. –Sub New ([parameters]) [Statements] End Sub

21 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 21 Constructors (cont.) When you define a class derived from another class, the first line of a constructor is typically a call to the constructor of the base class. The base class is referenced by using the keyword MyBase. –MyBase.New()

22 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 22 14-4 The Complete TMilitaryTime Class Definition

23 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 23 14-5 The Complete TPerson and TStudent Class Definitions

24 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 24 Chapter Summary VB. NET is an object-oriented language that supports encapsulation, inheritance, and polymorphism. Encapsulation refers to grouping related properties and methods so they can be treated as a single unit or object. Inheritance describes the ability to create new classes based on an existing class.

25 Crews/Murphy – Programming Right From the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 25 Chapter Summary (cont.) Polymorphism is the ability for objects from different classes to respond appropriately to identical method names or operators. A class definition consists of fields, properties, and methods. A constructor is a special method that executes during the creation of an object.

26 Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14


Download ppt "Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14."

Similar presentations


Ads by Google