Professional Documents
Culture Documents
NET PROGRAMMING
Topic 4.0
Topic 4.1 : Use Classes, Object and Methods Topic 4.2 : Apply Inheritance And Polymorphism Topic 4.3 : Construct Program Using String Methods Topic 4.4 : Understand the concepts of Exception Handling
OOP Features
Emphasis on data rather than procedure Programs are divided into Objects Data is hidden and cannot be accessed by external functions Objects can communicate with each other through functions New data and functions can be easily added whenever necessary
Benefits of OOP
Easy Maintenance
Modularity is inherent in OOP. Entities are represented by classes and classes with the same functionality are located in the same namespace (package). You can add a class into a namespace without affecting other members of the namespace.
Benefits of OOP
Extensibility.
OOP naturally supports extensibility. Having a class with certain functionality, you can quickly extend that class to create a different class with extended functionality.
Benefits of OOP
Code Reuse.
Since functionality is encapsulated into a class and each class is an independent entity, providing a library is very easy. In fact, programmers of any .NET Framework language can and should use the .NET Framework class library, an extensive library that provides rich functionality. Better still, you can extend the functionality of the class library to provide classes that suit your needs.
Concepts of OOP
Objects Classes Polymorphism Inheritance Encapsulation Data Abstraction
Classes
A class is a data type that provides some functionality. A class in VB.NET is declared using the keyword Class For example, Listing 1 presents a class called Employee. Listing 1: Class Employee Class Employee . End Class
Note:
Microsoft recommends the use of Pascal-case naming for classes. This means the first character of a class name is uppercase, and so is the first letter of any subsequent concatenated words. Good class names according to this convention include GeneralManager, SmallDictionary, StringUtil, and so on.
A class may contain any combination of encapsulated data (fields or member variables),operations that can be performed on the data (methods) and accessors to data (properties).
Class
End Class
The above syntax created a class named Test. Fields, Properties, Methods, and Events are members of the class. They can be declared as Public, Private, Protected, Friend or Protected Friend.
Example
Module Module1 Imports System.Console
Sub Main() Dim obj As New Test() 'creating a object obj for Test class obj.disp() 'calling the disp method using obj Read() End Sub
End Module Public Class Test 'creating a class named Test Sub disp() 'a method named disp in the class Write("Welcome to OOP") End Sub
End Class
Output:
Objects
Objects are instance of the class. Classes and Objects are very much related to each other. Without objects you can't use a class. To create a object for this class we use the new keyword and that looks like this: Dim obj as new Test().
Objects
Objects are the basic run-time entities in an object-oriented system. Programming problem is analyzed in terms of objects and nature of communication between them. When a program is executed, objects interact with each other by sending messages.
Cont..Objects
Different objects can also interact with each other without knowing the details of their data or code.
Class Members
Methods, Fields, Properties and Events are members of the class. They can be declared as Public, Private, Protected, Friend or Protected Friend.
Method
Methods are members that contain code. They are either subroutines (which don't have a return value)or functions (which do have a return value). For example, Listing 2 presents the Employee class with a subroutine called Work Listing 2: Class Employee with method Work
Class Employee Public Sub Work () ' Do something here . End Sub End Class.
Methods
Methods represent the objects built-in procedures. For example, a Class named Country may have methods named Area and Population. You define methods by adding procedures, Sub routines or functions to your class.
Example of Methods
For example, implementation of the Area and Population methods:
Public Class Country Public Sub Area() Write("--------") End Sub Public Sub population() Write("---------") End Sub End Class
Example of Fields
For example, if you have an object named House, you can store the numbers of rooms in it in a field named Rooms. It looks like this: Public Class House Public Rooms as Integer End Class
Example of Field
Examples of good field names are salary, quarterlyBonus, etc.
Adding two fields called salary and yearlyBonus to the Employee class in Listing 2 will result in the following code.
Class Employee class name Dim salary As Decimal = 40000 field Dim yearlyBonus As Decimal = 4000 field Public Sub PrintSalary() method ' print the salary to the Console System.Console.Write(salary) . End Sub End Class
Properties
Properties are retrieved and set like fields but are implemented using Property Get and Property Set procedures which provide more control on how values are set or returned Example:
Property GET End GET Property SET End SET
Events
Events allow objects to perform actions whenever a specific occurrence takes place. For example when we click a button a click event occurs and we can handle that event in an event handler.