Compunet Corporation1 Programming with Visual Basic.NET Creating and Using Classes Lecture # 8 Tariq Ibn Aziz.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

1 Classes and Objects in Java Basics of Classes in Java.
OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Python Objects and Classes
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Reusable Classes.  Motivation: Write less code!
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Inheritance Lecture 9 from Chapter 8. 2 Review Command line arguments Basic Inheritance Member access and inheritance Using super Creating a multi-level.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
More about classes and objects Classes in Visual Basic.NET.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Compunet Corporation1 Programming with Visual Basic.NET Inheritance Lecture # 10 Tariq Ibn Aziz.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 13: Object-Oriented Programming
C++ fundamentals.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Chapter 8 More Object Concepts
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Module 7: Object-Oriented Programming in Visual Basic .NET
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Inheritance in the Java programming language J. W. Rider.
Prepared by: Elsy Torres Shajida Berry Siobhan Westby.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Introduction to Defining Classes
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Classes, Interfaces and Packages
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Object-Oriented Programming: Classes and Objects.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Object-Oriented Concepts
Objects as a programming concept
Classes (Part 1) Lecture 3
Microsoft Visual Basic 2005: Reloaded Second Edition
Java Programming Language
Java Inheritance.
C++ Programming ㅎㅎ String OOP Class Constructor & Destructor.
CIS16 Application Development and Programming using Visual Basic.net
C++ Object Oriented 1.
Creating and Using Classes
Presentation transcript:

Compunet Corporation1 Programming with Visual Basic.NET Creating and Using Classes Lecture # 8 Tariq Ibn Aziz

Compunet Corporation2 Classes A class may contain three type of items: –Variables (state) –methods (behavior) –constructors variable store the values methods provides the logic, that constitutes the behavior of class Constructor initialize the state of a new instance of a class Class Members

Compunet Corporation3 Method A method is essentially a set of program statement Each method exist as part of a class Visual Basic program contain one or more classes, each may contain one or more method Method may call other methods in the same or a different class No program code can exist outside a method and no method can exist outside a class Note: Main() is also a method

Compunet Corporation4 Shared and Instance Members There are two ways that an item can be a member of a class –Shared member –Instance member Shared methods are accessed through the class. Non Shared methods, also called instance methods, are accessed through instances of the class.

Compunet Corporation5 Shared Method and Variable A Shared method is associated with a class, so no need to create an instance of that class. To call Shared method, use the following syntax: className.mthName(args) Shared variable is associated with a class, so no need to create an instance of that class. To call Shared variable, use the following syntax: className.variable1

Compunet Corporation6 Object-Oriented Programming In OOP classes are Types Objects are Instances of classes Objects may have member data or member methods or both The mechanism to create a new Object is called instantiation

Compunet Corporation7 Three Principles of OOP Three language features are fundamental to object-oriented programming –Encapsulation is a combination of Data and Behavior in one package and hiding the implementation of data from the user or –Encapsulation is the mechanism that associate data with code that manipulate it

Compunet Corporation8 Three Principles of OOP –Inheritance, The original class is often called the base class or the super class, and the new class is often called the derived class or the subclass. Inheritance is often referred to as extending the base class or super class.

Compunet Corporation9 Three Principles of OOP –Polymorphism (from the Greek, meaning "many forms", or something similar) is the quality that allows one name to be used for more than one (hopefully related) but technically different purpose –Method overriding occurs when a class declare a method having same types of signature as a method by one of its super class

Compunet Corporation10 Class in Visual Basic The following class is a valid class although its empty Public Class Person End Class Empty Class, i.e. no members

Compunet Corporation11 Data in a Class A class containing Data (state) class Students Dim ID As Integer Dim Name As String Dim As String End class Class with member data

Compunet Corporation12 Data and Method in a Class A class containing Data (state) and Method (Behavior) class Students Dim ID As Integer Dim Name As String Dim As String Sub print() System.Console.Write (ID) System.Console.Write (Name) System.Console.Write ( ) End Sub End class Class with member data and member method

Compunet Corporation13 Creating and Using Class class Person Public Dim Name As String Sub print() System.Console.Write (Name) End Sub End class Public Module M1 Sub Main() Dim v As New Person v.Name = "Tariq Aziz" v.print() End Sub End Module Class with member data and member method Using class in MODULE program

Compunet Corporation14 Creating and Using Class class Person Public Dim Name As String Sub print() System.Console.Write (Name) End Sub End class Class M1 Shared Sub Main() Dim v As New Person v.Name = "Tariq Aziz" v.print() End Sub End Class Class with member data and member method Using class in another class

Compunet Corporation15 Instance Variables in a Class Each object has its own copy of all instance variables defined by its class Dim v As New Person Public Dim Name As String Sub print() System.Console.Write (Name) End Sub v Person class Object Reference to Object Instance variable & method

Compunet Corporation16 Instance Variables in a Class Instance variables are initialized to defaults values during the creation of object. Variable of boolean set to false. Numbers are set to zeros. Any variable that acts as an object reference is set to null class Person Public ID As Integer Public Name As String Public Married As Boolean End class Dim As New Person Public ID As Integer=0 Public Name As String=null Public Married As Boolean=False v Person class Object

Compunet Corporation17 Instance Variables Example class A Public Dim x As Double Public Dim y As Boolean Public Dim s As String End Class class B shared Sub Main() Dim p As New A System.Console.WriteLine(" p.x = " & p.x) System.Console.WriteLine(" p.y = " & p.y) System.Console.WriteLine(" p.s = " & p.s) End Sub End class Output: p.x = 0 p.y = False p.s =

Compunet Corporation18 Instance method Example (Contd.) Class A Public Dim x As Double Public Dim y As Boolean Public Dim s As String Sub Display() System.Console.WriteLine( "x=" & x) System.Console.WriteLine( "y=" & y) System.Console.WriteLine( "s=" & s) End Sub End Class Class B shared Sub main() Dim p As New A p.Display() End Sub End class Output: x = 0 y = False s =

Compunet Corporation19 Shared Variables in a Class A Shared variable is shared by all objects of its class and thus relates to the class itself Shared var1 As Type Shared variables are initialized to defaults values when the class is loaded into memory. Variable of boolean set to false. Numbers are set to zeros. Any variable that acts as an object reference is set to null

Compunet Corporation20 Shared Variables Example class A Public Shared Dim x As Double Public Shared Dim y As Boolean Public Shared Dim s As String = "T" End Class class B shared Sub main() System.Console.WriteLine ("x = " & A.x) System.Console.WriteLine ("y = " & A.y) System.Console.WriteLine ("s = " & A.s) End Sub End class Output: x = 0 y = False s = T

Compunet Corporation21 Shared Method Example class A Public Shared Dim x As Integer = 10 Public Shared Dim y As Boolean = True Public Shared Dim s As String ="CA121" Shared Sub Display() System.Console.WriteLine(" x = " & x) System.Console.WriteLine(" y = " & y) System.Console.WriteLine(" s = " & s) End Sub End Class class B shared Sub main() A.Display() End Sub End class Output: x = 10 y = True s = CA121

Compunet Corporation22 Exercise (Classes) Write an application that declares a class named Person. It should have instance variable to record name, age, and salary. These should be of types String, integer, and Single. Use the new operator to create a Person object. Set and display its instance variable. Write an application that declares a class named Sphere. It should have instance variables to record its radius and the coordinates of its center. The should be of type Double. Use the new operator to create a Sphere object. Set and display its instance variable.

Compunet Corporation23 Polymorphism A class may have several Method with same name, it is known as Method overloading/Polymorphism Each Method can have same name, but parameters list must be different The println() is a good example of this concept. It has several overloaded forms, Each of these accepts one argument of a different type. The type may be Boolean, Char, Integer, Long, Single, Double, String or Object. It is much convenient for programmer to remember one method name rather than several different ones Method overloading provides an easy way to handle default parameter values

Compunet Corporation24 Polymorphism in Instance Method Example 1 Class Point3D Public Dim x, y As Integer Sub move(a As Integer) x = a End Sub Sub move(a As Integer, b As Integer) x = a y = b End Sub End Class class Point3DConstructor Shared Sub Main() Dim p As new Point3D p.move(100) System.Console.WriteLine(p.x & " " & p.y) p.move(200,300) System.Console.WriteLine(p.x & " " & p.y) End Sub End Class OUTPUT:

Compunet Corporation25 Polymorphism in Instance Method Example 2 class Point3D Public Dim x, y As Double sub Point3D(a as Double, b as Double) x=a y=b System.Console.WriteLine ("x=" & x & "y=" & y) End Sub sub Point3D(a as Double) x=a y=200 System.Console.WriteLine ("x=" & x & "y=" & y) End Sub Polymorphism Example

Compunet Corporation26 Polymorphism in Instance Method Example 2 Shared Sub main() Dim p As New Point3D p.point3d(1.0,2.0) p.point3d(100.0) p.x = 1.1 p.y = 3.4 System.Console.WriteLine(" p.x = " & p.x) System.Console.WriteLine(" p.y = " & p.y) End Sub End class

Compunet Corporation27 Polymorphism in Constructor Method class Point3D Public Dim x, y As Integer Sub New() x = 100 End Sub Sub New( a As Integer, b As Integer ) x = a y = b End Sub End Class class Point3DConstructor Shared Sub Main() Dim p As new Point3D System.Console.WriteLine( p.x & " " & p.y) Dim q As New Point3D(200,300) System.Console.WriteLine( q.x & " " & q.y) End Sub End Class

Compunet Corporation28 Exercise Write an application that defines a Circle class with two Method. The first form accepts a double value that represents the radius of the circle. This method assumes that the circle is centered at the origin. The second form accepts three double values. The first two arguments defines the coordinates of the center, and the third argument defines the radius