1 / 26

Advanced Object- Oriented Programming

14. Advanced Object- Oriented Programming. Programming Right from the Start with Visual Basic .NET 1/e. Objectives. Understand the differences between classes and objects Understand object terminology, including encapsulation, inheritance, and polymorphism Know how to create your own classes.

Download Presentation

Advanced Object- Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 14 AdvancedObject-OrientedProgramming Programming Right from the Start with Visual Basic .NET 1/e

  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. Objectives (cont.) • Know how to write constructors with multiple parameter lists • Develop applications that use classes and objects

  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. 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. 14-2 Object-Oriented Terminology • A language is considered to be object-oriented if it supports three main features: • Encapsulation • Inheritance • Polymorphism

  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. 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. 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. 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. 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. Inheritance (cont.)

  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. 14-3 Creating YourOwn 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. 14-3 Creating YourOwn 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. 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. 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. 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. 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. 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. 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. 14-4 The Complete TMilitaryTime Class Definition

  23. 14-5 The Complete TPerson and TStudent Class Definitions

  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. 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. 14 AdvancedObject-OrientedProgramming Programming Right from the Start with Visual Basic .NET 1/e

More Related