OBJECT ORIENTED
CONCEPTS
28-11-2014
11/17/202 ANKIT VERMA 101
0
Object Oriented Concepts
VB.NET is an Object Oriented Programming language
with features:
Abstraction
Encapsulation
Polymorphism
Inheritance
It also support following:
Overloading
Overriding
Structure
Class
11/17/202
0
Class
Class is generally called a Blueprint or User Defined
Data Type.
Class is collection of objects of similar type.
Class consists of Attributes (Data Members) and
Behaviors (Member Functions), that are used by
Instances of that class.
Class is an Abstract entity.
Members of Class:
Data Members, Properties, Methods & Events
Class Access Specifiers:
Public, Private, Protected, Friend & Protected Friend
11/17/202
0
Object
Objects are Real Time entity.
They may present as Person, Place, Bank Account, Table of
Data, Item etc.
It is Instance of class.
Each object contain data & code to manipulate data.
Two types of data members:
Static or Class Member
Members that apply to Class and called by their class names.
Shared keyword is used with members.
Instance or Object Member
Members that apply to object.
They referred by object name.
11/17/202
0
Overloading & Overriding
Overloading
Provide different versions of a method that has same name &
different parameters.
Parameters can be different from name, data type or ordering of
parameters.
Overriding
Replace inherited property or method.
Overridden methods in subclass must be same as base class
method including name and parameters.
11/17/202
0
Class Object Example
Example:
11/17/202
0
Constructor
Special methods that allow control over initialization
of objects.
Constructors are Procedures with name Sub New.
Run when Instance of class is created.
Explicitly called only from other Constructors.
Parameterized Constructors:
Parameters can be passed to constructors also.
Shared Constructors can’t have parameters.
11/17/202
0
Constructor Types
Two types of Constructors:
Shared Constructors
Used to initialize Shared variables.
Created using Shared keyword & shared with all instance of class.
Not run more than one, during single execution of program.
Explicitly Public access & we can’t write explicitly.
We can’t initialize Instance variable in Constructor.
Instance Constructors
Initialize
variable that are declared with Dim, Public, Private,
Friend, Protected etc.
You can also access Shared variable within this.
11/17/202
0
Constructor Example
11/17/202
0
Destructor
Special method used to release Objects from memory.
Two types of Destructors:
Finalize Method
Called from the class or derived class, to which it belong.
Called after last reference to an Object is released from Memory.
.NET automatically runs Finalize Destructor.
We can’t determine it’s execution, when an Object loses its scope.
CLR calls this using system call called Garbage Collection.
CLR periodically check for Object, if Object not used, this Destructor
is called automatically and Garbage Collection Starts.
Finalize method is Overridden when we write our code Explicitly.
Dispose
11/17/202
0
Destructor
Dispose Method
Problem with Finalize Method is non deterministic nature.
We can’t determine Finalize Method call explicitly.
Dispose Method is called to release resource such as Database
Connection, File Handler or System Lock.
Unlike Finalize Method, this is not called Automatically.
Through code we have to call this Explicitly.
Example:
Protected Overrides Sub Finalize ( )
MsgBox (“Object Out Of Scope”)
EndSub
11/17/202
0
Structure
It is User Defined Data Type.
Structure combines Variables & Methods.
Single Variable of Structure can hold different types of data.
Structure can be declared Inside Class, Namespace, Module
or Another Structure.
Access Specifiers are allowed for Structure & their members.
Only Shared Members & Constants can be Initialized.
Like Class, Structure have Constructors but only
parameterized.
If we have Default Constructor, that must be declared as Shared one.
11/17/202
0
Structure Example
Example:
11/17/202
0
Structure With Constructor Example
Example:
11/17/202
0
Class VS Structure
Similarities:
Both are User Defined Data Types.
Both support members like Constructor, Variables, Constants,
Methods, Events etc.
Both have Shared Constructors with or without parameters.
Both can implement Interfaces.
11/17/202
0
Class vs Structure
Differences:
Class can be Inherited from other Class, but Structure not.
Class can have Instance Constructors with and without
parameters, but Structure only have parameterized.
Class is Reference type, but Structure is Value type.
Class allow initialization of members inside Class, but Structure
not allow inside initialization.
Class have Private as default Access Specifier for all variables &
constants, but Structure have Public by default.
Class Procedures can handle Events, but not in Structure.
11/17/202
0
Method Overloaing
28-11-2014
11/17/202 ANKIT VERMA 117
0
Method Overloading
Overloading Methods have same name, but different
argument lists.
Argument may differ in Type, Number or Order Of
The Parameter.
Return type of Overloading Method can be Same or
Different.
Method invoked depends upon the type of argument
passed.
Overloading is example of Polymorphism.
11/17/202 118
0
Method Overloading : Example
11/17/202 119
0
Shared Members
28-11-2014
11/17/202 ANKIT VERMA 120
0
Shared Members
Shared keyword can be used with Constructor,
Methods & Variables.
Shared methods can be overloaded like regular
methods.
Shared variables create only one copy for all instances
of class.
11/17/202 121
0
Shared Members : Example
Example:
11/17/202 122
0
Inheritance
28-11-2014
11/17/202 ANKIT VERMA 123
0
Inheritance
Inheritance refers to deriving new Class from existing Class.
Derived Class is known as Sub Class and Parent Class is
known as Base Class.
Derived Class inherits all Public & Protected Data Members,
Methods, Events & Constants.
Constructor can’t be inherited.
Types of Inheritance:
Single
Multi Level
Hierarchical
Multiple Inheritance only supported by using Interface
11/17/202 124
0
Inheritance : Keywords
Inherits
Inherits keyword is used to inherit Class.
MustInherit
Indicate that Class must be inherited by other Class.
It act as Base Class and similar to Abstract Class of Java.
NonInheritable
If class is marked NonInheritable, we can’t create its Sub Class
MyBase
Call parent Class Constructor and Parent Class Overridden Methods.
It always refers the immediate Super Class.
We can’t navigate to more than one level from the Child Class.
MyBase.MethodName is not allowed in .NET
11/17/202 125
0
Inheritance : Keywords
Overloads
Indicate that methods are Overloading.
If Methods are in same class, no necessary to write this keyword.
Overridable
Allow method to be Overridden.
Overrides keyword is used to override the Overridable Method.
NotOverridable
Prevent the method being Overridden.
MustOverride
Methods must be declared in MultiInherit Class.
MustInherit
Enable Abstract Class creation. Child Class must implement all Abstract
Methods, otherwise Child Class also must declared as Abstract Class.
11/17/202 126
0
Inheritance : Example
Example:
11/17/202 127
0
Inheritance : Example
11/17/202 128
0
Inheritance : Example
11/17/202 129
0
Method Overriding
28-11-2014
11/17/202 ANKIT VERMA 130
0
Method Overriding
Sub Class is not only Inheriting but it can also give its
own implementation (override) for methods.
All the methods can’t be Overridden.
To allow methods to Overridden, Parent Class method
must use Overridable keyword.
11/17/202 131
0
Method Overriding : Example
11/17/202 132
0
Abstract
Base Class
28-11-2014
11/17/202 ANKIT VERMA 133
0
Abstract Base Class
MustInherit keyword is used to create Abstract Class.
Contain Signature of method, that Derived Class can
implement in its own Class.
We can’t create Object of it.
Method with MustOverride keyword should not
contain any implementation.
Class with even one MustOverride method, should be
declared as MustInherit.
To implement method, Overrides keyword use in Sub
Class.
11/17/202 134
0
Abstract Base Class
If Sub Class is not implementing any one of Abstract
methods from Abstract Class, then Child Class must
be declared as Abstract one.
Abstract Class can contain Non-Abstract Method also.
11/17/202 135
0
Abstract Base Class : Example
Example:
11/17/202 136
0
Interface
28-11-2014
11/17/202 ANKIT VERMA 137
0
Interface
Interface defines Properties, Method and Events, called
members of Interface.
Interface consist of only Declaration of Members but not
the Implementation.
Only Class and Structure implement these data
members.
Variables can’t be declared in the Interface.
Single Class can inherit from only one Base Class, but
can implement any number of Interfaces.
It support Multiple Inheritance.
Implements keyword is used for their implementation.
11/17/202 138
0
Interface Vs Class
Interface can’t provide any implementation, whereas
Class can provide implementation to Methods.
Interface can’t have Constructors, but Class can define
Constructors.
A Class inherits from only one Class, but implements
multiple Interface.
11/17/202 139
0
Interface : Example
Example:
11/17/202 140
0