You are on page 1of 31

YANBU UNIVERSITY COLLEGE

Women’s Campus

MIS 102
INTRODUCTION TO INFORMATICS

MODULE 01
Introduction to Classes and Objects
Lecture

© Yanbu University College


Course Objective

• Objective of this course is to make students


familiar with the concepts of object-oriented
programming

• Concepts will be reinforced by their


implementation in VB.net

© Yanbu University College Slide 2


What is Visual Basic .NET?
• Visual Basic .NET, or VB.NET, is an upgrade to the last version
of VB (version 6.0) that conforms to the .NET platform.
• Visual Basic is an object-oriented language that consists of
two fundamental parts: a visual part and a language part.
• The visual part of the language consists of a set of objects,
while the language part consists of a high-level procedural
programming language.
• Both elements of the language are used together to create
applications.
• An application is simply a VB program that can be run under
the Windows operating system.
© Yanbu University College Slide 3
What is Visual Basic .NET?
• From a user’s standpoint, the visual part of an
application is provided within a window. It is the
graphical user interface (GUI) that allows the user to
see the input and output provided by the application
• For the language element: before GUIs, computer
programs consisted entirely of a sequence of
instructions to which a computer could respond. The
set of instructions and rules that could be used to
construct a program is called a programming
language (code).
© Yanbu University College Slide 4
Procedural Vs. OOP Languages
• Procedural Language:
o It describes a list of instructions for solving a problem.
o The main unit in procedural programming is function
o It concentrates on and starts with creating functions
o It separates the data of the program from the operations
that manipulate the data
o A program has been viewed as a logical procedure that
takes input data, processes it, and produces output data.

© Yanbu University College Slide 5


Procedural Vs. OOP Languages
• Object-Oriented Programming Language:
o OOP is a programming language model organized around
"objects" rather than "actions"
o The fundamental idea behind OO languages is to
combine into a single unit both data and the functions
that operate on that data.
o The main unit in OOP is class
o An object's functions are the only way to access its data.
If you want to read a data item in an object, you will call
one of the object's functions.
© Yanbu University College Slide 6
Procedural Vs. OOP Languages
• Object-Oriented Programming Language:
o It starts from isolating the classes, and then look for the methods
inside them.
o One of the main principle of object oriented
programming language is encapsulation that everything
an object will need must be inside of the object

© Yanbu University College Slide 7


Procedural Vs. OOP Languages
o OO gives data more central position in program and ties
the data more closely to functions that act on it.
Data Data
Data

Functions

Functions Data
Functions Functions Functions

Functions

Functions
Functions Functions

Procedural OOP
© Yanbu University College Slide 8
Concept of Class and Object

• CLASS:
o Refers to a blueprint and
definition of object
o Properties and methods
of objects are defined in class.
o It is a mechanism of creating objects.

© Yanbu University College


Concept of Class and Object
• OBJECT:
Something real, examples:
o Khalid
o House
o Car
o Book

Interactions
o Khalid lives in the house
o Khalid drives the car
o Khalid reads the book
© Yanbu University College Slide 10
Concept of Class and Object
• An object has
o State (attributes)
o Well-defined behavior (operations/methods)
o Unique identity
• Example – Khalid is an Object
o State (Attributes)
⁻ Name
⁻ Age
o Behavior (Operations/Methods)
⁻ Walks
⁻ Eats
o Identity
⁻ His name
© Yanbu University College Slide 11
Concept of Class and Object

• OBJECT:
o OO model consists of several interacting objects
o It is an instance of a class.
o Each object has a class which
defines its data and behavior

© Yanbu University College Slide 12


The Relationship Between Classes and Objects
• Class is a Drawing while Object is a Building
• A class is a template or a blueprint of the entity
that the class represents. To use a class's field or
method or any other member, you need to first
turn the class into an object.
• In OOP, an object is said to be an instance of a
class.
• One class can create many objects.

© Yanbu University College Slide 13


How to create Class and Objects
Class Declaration
• A class in VB.NET is declared using the keyword Class

• Syntax:
Class ClassName
End Class

• Example:
Class Employee
End Class
© Yanbu University College Slide 14
How to create Class and Objects

Class Instantiation Syntax


Dim ObjectName As ClassName
ObjectName = New ClassName()

© Yanbu University College Slide 15


How to create Class and Objects

Class Instantiation
1. Declare an object variable of type Employee. The
variable is called AliEmployee Constructor
2. Instantiate Employee with the keyword New is a special
type of
Dim AliEmployee As Employee method that
is used only
AliEmployee = New Employee() when an
object is
Call to created to
Constructor initialize the
object data.

© Yanbu University College Slide 16


Methods
• A Method is a procedure built into the class. They
are a series of statements that are executed when
called.
• Methods allow us to handle code in a simple and
organized fashion.
• There are two types of methods in VB .NET:
o Functions Procedure : those that return a value
o Sub Procedure: those that do not return a value

© Yanbu University College Slide 17


Methods
• Method Syntax:
Sub CalculateGPA()
' Do something here
End Sub
 
Function GetValue() As Integer
' Do something here
Return Value
End Function

Example: Class Employee with method CalculatePay

Class Employee
Public Sub CalculatePay ()
' Do something here
End Sub
©End Class
Yanbu University College Slide 18
Methods
• Method Variables
o Variables declared within methods are called method
variables.
o They have method scope 

• Calling Methods
o A method is not executed until it is called.
o A method is called by referencing its name along with
any required parameters

© Yanbu University College Slide 19


Methods
• Variables declared in the body of method
o Called Local Variables
o Can only be used within that method

• Variables declared in a class declaration


o Called Instance Variables
o Each object of the class has a separate instance of the
variable

© Yanbu University College Slide 20


Methods
• Variables declared in the body of a particular method are
local variables and can be used only in that method. When
a method terminates, the values of its local variables are
lost.
• When you declare a variable within a class, every time you
instantiate a new object from the class, you get a new copy
of each instance variable of that class, and that copy is
associated with the new object.

© Yanbu University College Slide 21


Methods

• All object instances have their own copies of


instance variables—that is, each object (instance)
of the class has a separate instance of the variable
in memory.
© Yanbu University College Slide 22
Methods

© Yanbu University College Slide 23


VB.NET Language Basic Concepts
1. Source Files
– VB.NET source code is saved in files with a .vb
extension
– Source files are plain-text files that can be created and
edited with any text editor, including Notepad.
– For Visual Studio .NET, source files are listed in the
Solution Explorer window, and all source is included
from these files when the solution is built.

© Yanbu University College Slide 24


VB.NET Language Basic Concepts
2. Identifiers
– are names given to namespaces, types (classes,
standard modules, interfaces, and delegates), type
members (methods, constructors, events, constants,
fields, and properties), and variables.
– Identifiers must begin with either an alphabetic or
underscore character ( _ ), may be of any length, and
after the first character must consist of only
alphanumeric and underscore characters.

© Yanbu University College Slide 25


VB.NET Language Basic Concepts
3. Keywords
– are words with special meaning in a programming
language.
– In VB.NET, keywords are reserved; that is, they cannot
be used as naming variables and subroutines.
4. Literals
– Literals are representations of values within the text of a
program.
– Literals have many types: numeric, string, character,
date, Boolean, and Nothing.

© Yanbu University College Slide 26


VB.NET Language Basic Concepts
5. Types
– Types in Visual Basic .NET are divided into two
categories: value types and reference types
– When a variable holds a value type, it holds the data
within its own memory allocation.
– When a variable holds a reference type, a reference to
the data (also known as a pointer) is stored in the
variable, and the data itself is stored somewhere else.

© Yanbu University College Slide 27


VB.NET Language Basic Concepts
6. Namespace
– Thousands of types are defined in the .NET Framework. In
addition, programmers can define new types for use in their
programs. With so many types, name clashes are inevitable.
– To prevent name clashes, types are considered to reside inside of
namespaces
– Example: Console class, which is defined in the System
namespace
7. Variables
– is an identifier that is declared in a method and that stands for a
value within that method.
– Its value is allowed to change within the method. Each variable is
of a particular type, and that type is indicated in the declaration of
the variable.
© Yanbu University College Slide 28
VB.NET Language Basic Concepts
8. Scope
– refers to the so-called visibility of identifiers within source
code.
– Given an identifier declaration, the scope of the identifier
determines where it is legal to reference that identifier in code.
9. Access Modifiers
– They control the accessibility of types (including classes,
standard modules) and type members (including methods,
constructors, events, and properties) to other program
elements.
– They are part of the declarations of types and type members
such as Public and Private
© Yanbu University College Slide 29
VB.NET Language Basic Concepts
10. Interfaces
– An interface is a description of the actions that an
object can do
– In object-oriented programming, an interface generally
defines the set of methods (or messages) that an
instance of a class that has that interface could respond
to.

© Yanbu University College Slide 30


THE END
Q&A

© Yanbu University College Slide 31

You might also like