Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

850

Part X

APPENDIXES

 

 

 

Visual Basic .NET, like any other programming language, provides abstraction through classes and objects. Attributes and behavior shared by similar objects are defined as class. The instance of the class is an object. Each object consists of characteristics and attributes that are the properties of the object. In addition, a set of actions can be performed by each object. The actions that are performed are known as methods. In Visual Basic .NET, you can specify the various properties and methods that are used for objects while creating classes. Abstraction is mainly used to reduce the complexity of an object by exposing only the essential features and methods of an object. Additionally, abstraction helps you generalize an object as a data type. By declaring classes, you can also generalize objects as data types.

Encapsulation

Information hiding or encapsulation means that the nonessential details of an object are hidden. Consider an example: When you switch your television on, it starts functioning. Needless to say that the internal functioning process remains hidden. In other words, the functioning of the television is hidden or encapsulated.

The method of implementing abstraction is encapsulation. As mentioned earlier, abstraction refers mainly to concentrating on the necessary and essential details of an object while ignoring the unnecessary and nonessential ones. Encapsulation achieves this.

The internal implementation of the classes is hidden from the user by encapsulation.Therefore, encapsulation is displaying only the properties and methods of an object. It helps the developers in hiding the complexity of an object and also uses different implementations of the same object.

Inheritance

The earlier versions of Visual Basic supported interface inheritance but not implementation inheritance. However, Visual Basic .NET supports both implementation inheritance and interface inheritance.

Implementation inheritance means that a class is derived from an existing class. The derived class is called subclass, and the class from which it is derived is called base class.

INTRODUCTION TO VISUAL BASIC .NET Appendix B 851

NOTE

The classes that are created in Visual Basic .NET are derived from the Object class, which is a part of the System namespace.

The properties and methods of the base class are inherited by the subclass. In addition, methods and properties can be added to the subclass in order to extend the functionality of the base class. In the derived class, the methods of the base class can also be overridden.

Inheritance also helps you create hierarchies of objects. For example, you can consider a class named animals. The cats class is derived from the animals class, and the lions class is derived from the cats class.

In the preceding example, the class lions inherits the properties and methods of the class cats, which in turn inherits all the properties and methods of the class animals. Therefore, all the properties and methods of the lions class and the cats class are inherited by the animals class.

All the classes that are created in Visual Basic .NET can be inherited by default. Inheritance helps you create complex objects from simpler ones and reuse the code. After a class is created in Visual Basic .NET, it can also be used as a base class in order to create a derived class.

Polymorphism

The ability of an object to exist in different forms is known as polymorphism. Consider an example to have a proper understanding of the term.

If you decide to buy a television set, you either contact a dealer or call the manufacturing company. If you contact a dealer, the dealer first takes the order and then contacts the company. However, if you contact the company directly, the company contacts the dealers of your region and makes the necessary arrangements to deliver the television set. In this case, the dealer and the company are two different classes.The dealer and the company respond differently to the same order. In object-oriented programming, this is known as polymorphism.