Object-Oriented Programming: Classes and Objects

Slides:



Advertisements
Similar presentations
Chapter 12 OOP: Creating Object- Oriented Programs Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Advertisements

12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
OOP-Creating Object-Oriented Programs
Road Map Introduction to object oriented programming. Classes
OOP: Creating Object-Oriented Programs. VB & Object Oriented Programming Objects have properties, methods, and generate events Classes have been predefined.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
110-I 1 Object Terminology Review Object - like a noun, a thing –Buttons, Text Boxes, Labels Properties - like an adjective, characteristics of object.
An Object-Oriented Approach to Programming Logic and Design
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
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)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Chapter 5 Introduction to Defining Classes
Introduction to Object-Oriented Programming Lesson 2.
Object-Based Programming in VB.NET. Must Understand Following: Encapsulation Information hiding Abstract Data Type Class, Instance, Reference Variable.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Object-Oriented Programming: Inheritance and Polymorphism.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Object-Oriented Programming: Classes and Objects.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
These materials where developed by Martin Schray
Introduction to Object-oriented Programming
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Classes and Objects: A Deeper Look
Programming Logic and Design Seventh Edition
IS 350 Application Structure
Object-Oriented Programming: Classes and Objects
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming: Classes and Objects
Chapter 11 – Object-Oriented Programming
Object Based Programming
VISUAL BASIC.
Introduction to Classes
Corresponds with Chapter 7
Programming with Microsoft Visual Basic 2008 Fourth Edition
Tonga Institute of Higher Education
Object-Oriented Programming: Inheritance and Polymorphism
CIS16 Application Development and Programming using Visual Basic.net
Methods.
2.1 Introduction to Object-Oriented Programming
Presentation transcript:

Object-Oriented Programming: Classes and Objects

Multitier Applications Classes are used to create multitier applications. Each of the functions of a multitier application can be coded in a separate component and stored and run on different machines. Goal is to create components that can be combined and replaced. A common practice for writing professional applications is to write independent components that work in multiple “tiers” or layers. If one part of the application needs to change, such as a redesign of the user interface or a new database format, the other components do not need to be replaced.

Three-tier Model Most popular implementation The term “n-tier” application is an expansion of the three-tier model. The middle tier may be written in multiple classes that can be stored and run from multiple locations. The Presentation tier refers to the user interface, which in VB is the form. The Business tier is a class or classes that handle the data. The Data tier includes retrieving and storing the data in a database. It retrieves the data and passes the results to the Business tier, or takes data from the Business tier and writes it in the appropriate location.

"Cookie Analogy" Class = Cookie cutter Instantiate = Making a cookie using the cookie cutter Instance = Newly made cookie Properties of the Instance may have different values. Icing property can be True or False. Flavor property could be Lemon or Chocolate Methods = Eat, Bake, or Crumble Events = Cookie crumbling and informing you Sometimes the distinction between a method and an event is somewhat fuzzy. Generally, anything that an object is told to do is a method; if the object does an action and needs to make it known, that’s an event. The properties of the cookie object are: Cookie1.Flavor = "Lemon" Cookie1.Topping = "Cream Frosting"

Object-Oriented Terminology Encapsulation Inheritance Polymorphism Key features of an object-oriented language are encapsulation, inheritance, and polymorphism. *The next several slides are going to define several object-oriented terms.

Encapsulation Combination of characteristics of an object along with its behavior in "one package" Cannot make object do anything it does not already "know" how to do. Cannot make up new properties, methods, or events. Sometimes referred to as data hiding, an object can expose only those data elements and procedures that it wishes. Think of it being a completed package; all parts of the package are in a capsule. Encapsulation can be witnessed by looking at any program; the form is actually a class; all methods and events that are coded are enclosed within the Class and End Class statements; the variables placed in the code are properties of the specific form class being created. Through use of the Public, Private, Protected, and Friend keywords, an object can “expose” only those data elements and procedures that it wishes to allow the outside world to see.

Inheritance Ability to create a new class from an existing class Original class is called Base Class, Superclass, or Parent Class. Inherited class is called Subclass, Derived Class, or Child Class. For example, each form created is inherited from the existing Form class. Purpose of inheritance is reusability.

Inheritance Example The derived classes inherit from the base class. Base/Parent Class Person Derived/Child Class Employee Customer Student In inheritance, typically the classes go from general to the more specific. The derived classes inherit from the base class.

Polymorphism Methods having identical names, but different implementations Radio button, check boxes, and list boxes all have a Select method—the Select method operates appropriately for its class. Overloading — Several argument lists for calling the method Example: MessageBox.Show method Overriding — Refers to a method that has the same name as its base class Method in subclass takes precedence. The term, polymorphism, actually means the ability to take on many shapes or forms. Polymorphism allows a single class to have more than one method with the same name. When the method is called, the argument type determines which version of the method to use.

Designing Your Own Class Analyze characteristics needed by new objects. Characteristics or properties are defined as variables. Define the properties as variables in the class module. Analyze behaviors needed by new objects. Behaviors are methods. Define the methods as sub procedures or functions.

Creating Properties in a Class Define variables inside the Class module by declaring them as Private — these store the value of the properties of the class. Do not make Public, since that would violate encapsulation (each object should be in charge of its own data). Protected variables behave as private but are available in any class that inherits from this class—as a private or protected variable, the value is available only to procedures within the class, the same way that private module-level variables are available only to procedures within a form’s class code.

Property Procedures Properties in a class are accessed with accessor methods in a property procedure. Name used for property procedure is the name of the property seen by the outside world. Set accessor method. Uses Value keyword to refer to incoming value for property Assigns a value to the property Get Statement uses the value keyword to refer to the incoming value of the property. Must assign a return value to the procedure name or use a Return Statement to return a value. The Set statement uses the value keyword to refer to the incoming value for the property. Property procedures are public by default, so omit the optional Public keyword can be omitted. *The next slide displays the Property Procedure General Form.

Property Procedure General Form {Private | Protected } ClassVariable As DataType [Public] Property PropertyName( ) As DataType Get Return ClassVariable [|PropertyName = ClassVariable] End Get Set (ByVal Value As DataType) [statements, such As validation] ClassVariable = Value End Set End Property Before the End Get, assign a return value to the procedure name or use a Return statement to return a value. The Property Get and Set retrieve the current value and assign a new value to the property. If you do not specify an access modifier, the default is Public.

Read-Only Properties In some instances, a value for a property can be retrieved by an object but not changed. A property can be written to create a read-only property. Create a read-only property by using the ReadOnly modifier. ' The property procedure for a read-only property. ReadOnly Property TotalPay() As Decimal Get Return TotalPayDecimal End Get End Property Use the ReadOnly modifier and write only the Get portion of the property procedure.

Write-Only Properties At times, a property can be assigned by an object, but not retrieved. Create a property block that contains only a Set to create a write-only property. ' Private module-level variable to hold the property value. Private PriceDecimal As Decimal Public WriteOnly Property Price() As Decimal Set(ByVal value As Decimal) If value >= 0 Then PriceDecimal = value End If End Set End Property

Constructors and Destructors Method that automatically executes when an object is instantiated Constructor must be public and is named New. Ideal location for an initialization tasks such as setting the initial values of variable and properties Destructor Method that automatically executes when an object is destroyed Rarely needed in .NET classes (automatic garbage collection) The constructor must be public, because the objects that are created must execute this method. If a class does not contain a constructor, the compiler creates an implicit method called the default constructor and has an empty argument list.

Overloading the Constructor Overloading means that two methods have the same name but a different list of arguments (the signature). Create by giving the same name to multiple procedures in a class module, each with a different argument list.

Parameterized Constructor Constructor that requires arguments Allows arguments to be passed when creating an object Within the class code, use the Me keyword to refer to the current class. When a class has both an empty constructor and a parameterized constructor, the program that creates the object can choose which method to use.

Value Types and Reference Types Data types are divided into two categories—value types and reference types. A variable of a value type (such as Integer) contains a single value of that type. Dim CountInteger as Integer = 7 A reference type variable is initialized by default to the value Nothing if you do not initialize it in its declaration to refer to an object. When you attempt to use a variable that contains Nothing to interact with an object, you’ll receive a NullReferenceException. Dim Employee as Person

Class Scope A class’s instance variables, properties and methods have class scope. Within this scope, a class’s members are accessible to all of the class’s other members and can be referenced simply by name. Outside a class’s scope, class members cannot be referenced directly by name. Those class members that are visible (such as Public members) can be accessed through a variable that refers to an object of the class (for example, time.Hour).

Auto-Implemented Properties For properties that do not have any additional logic in their Set and Get accessors, there’s a feature—called auto-implemented properties—that allows you to write one line of code and have the compiler to generate the property’s code for you. Public Property Hour As Integer The compiler would then generate a Private instance variable of type Integer named _Hour and the following property code Public Property Hour As Integer Get Return _Hour End Get Set(value As Integer) _Hour = value End Set End Property

Garbage Collection Every object you create uses various system resources, including the memory that holds the object itself. These resources must be returned to the system when they’re no longer needed to avoid resource leaks. The Common Language Runtime (CLR) performs automatic garbage collection to reclaim the memory occupied by objects that are no longer in use. When there are no more references to an object, it’s marked for garbage collection by the CLR.

Garbage Collection Resources like memory that are allocated and reclaimed by the CLR are known as managed resources. Other types of resource leaks can occur. For example, an app may open a file on disk to modify the file’s contents. If the app does not close the file, other apps may not be allowed to use the file until the app that opened the file finishes. Resources like files that you must manage are known as unmanaged resources (because they’re not managed by the CLR). Such resources are typically scarce and should be released as soon as they’re no longer needed by a program.

Instance Variables versus Shared Variables Instance variables or properties Separate memory location for each instance of the object Shared variables or properties Single variable that is available for ALL objects of a class Can be accessed without instantiating an object of the class When creating, use the Shared keyword to create. Shared properties can be set to read-only so that their values can be retrieved but not set directly. Terminology varies from one OOP language to another — in some languages, shared members are called class variables or static variables. Microsoft documentation refers to instance members and shared members, which includes both properties and methods. A shared member has one copy for all objects of the class, and an instance member has one copy for each instance or object of the class. Methods also can be declared as shared and are considered shared members.