260 likes | 267 Views
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.
E N D
14 AdvancedObject-OrientedProgramming 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
Objectives (cont.) • Know how to write constructors with multiple parameter lists • Develop applications that use classes and objects
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.
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.
14-2 Object-Oriented Terminology • A language is considered to be object-oriented if it supports three main features: • Encapsulation • Inheritance • Polymorphism
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.
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.
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.
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.
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.
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-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.
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.
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
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.
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.
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
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
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()
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.
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.
14 AdvancedObject-OrientedProgramming Programming Right from the Start with Visual Basic .NET 1/e