Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.

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

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
OOP: Creating Object-Oriented Programs. VB & Object Oriented Programming Objects have properties, methods, and generate events Classes have been predefined.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
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.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 12: Adding Functionality to Your Classes.
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.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 8 More Object Concepts
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.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
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.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Object-Oriented Programming Chapter Chapter
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
ISBN Object-Oriented Programming Chapter Chapter
Introduction to Object-Oriented Programming Lesson 2.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Object-Based Programming in VB.NET. Must Understand Following: Encapsulation Information hiding Abstract Data Type Class, Instance, Reference Variable.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Object-Oriented Programming: Inheritance and Polymorphism.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Object-Oriented Programming: Classes and Objects.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming: Classes and Objects
Microsoft Visual Basic 2005: Reloaded Second Edition
Object-Oriented Programming: Classes and Objects
Object Based Programming
Lecture 22 Inheritance Richard Gesick.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Inheritance and Polymorphism
Presentation transcript:

Object Based Programming

Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding  Implementing a Time Abstract Data type with a Class –Controlling Access to Members –Initializing Class Objects: Constructors –Properties –Composition –Shared Class Members –Const and ReadOnly Members  Garbage Collection

Object Terminology Review  Object - like a noun, a thing –Buttons, Text Boxes, Labels  Properties - like an adjective, characteristics of object –Text, ForeColor, Checked, Visible, Enabled  Methods - like a verb, an action or behavior, something the object can do or have done to it –ShowDialog, Focus, Clear, ToUpper, ToLower  Events - object response to user action or other events –Click, Enter, Activate

Instantiating An Object  Creating a new object based on a class  Create an instance of the class by using the New keyword and specify the class

Encapsulation Sometimes referred to as data hiding;an object can expose only those data elements and procedures that it wishes

6 Inheritance  Inheritance is a form of reusability in which classes are created by absorbing an existing class’s data and behaviors and improving by adding new capabilities.  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or superclass, or base class  The derived class is called the child class or subclass.  As the name implies, the child inherits characteristics of the parent

7 Inheritance  Inheritance relationships are often shown graphically in a class diagram, with the arrow pointing to the parent class

8 Deriving Subclasses  In VB.NET, we use the reserved word Inherits to establish an inheritance relationship

9 Controlling Inheritance  Visibility modifiers determine which class members get inherited and which do not  Variables and methods declared with public visibility are inherited, and those with private visibility are not  But public variables violate our goal of encapsulation  There are two more visibility modifiers that helps in inheritance situations: Protected and Friend

10 The Protected Modifier  The Protected visibility modifier allows a member of a base class to be inherited into the child  But Protected visibility provides more encapsulation than public does  However, Protected visibility is not as tightly encapsulated as Private visibility

11 The Friend Modifier  The Friend visibility modifier allows a member of a base class to be inherited into the child only if the the derived class is in the same assembly

12 The MyBase Reference  Constructors are not inherited, even though they have public visibility  Yet we often want to use the parent's constructor to set up the "parent's part" of the object  The MyBase reference can be used to refer to the parent class, and is often used to invoke the parent's constructor  Also, when a derived-class method overrides a base-class member, the base-class member can be accessed from the derived class b using the MyBase reference.

Single vs. Multiple Inheritance  VB.NET supports single inheritance, meaning that a derived class can have only one parent class  Multiple inheritance allows a class to be derived from two or more classes, inheriting the members of all parents  Collisions, such as the same variable name in two parents, have to be resolved  In most cases, the use of interfaces gives us the best aspects of multiple inheritance without the overhead

Polymorphism  Different classes of objects may have behaviors that are named the same but are implemented differently  Programmers can request an action without knowing exactly what kind of object they have or exactly how it will carry out the action

Polymorphism Implemented  Overloading –Argument type determines which version of a method is used –Example: MessageBox.Show method  Overriding –Refers to a class that has the same method name as its base class –Method in subclass takes precedence

16 Overriding Methods  A child class can override the definition of an inherited method in favor of its own  That is, a child can redefine a method that it inherits from its parent  The new method must have the same signature as the parent's method, but can have different code in the body  The type of the object executing the method determines which version of the method is invoked  In VB.NET, a base-class method must be declared Overridable if that method is to overriden in a derived class.

17 Overloading vs. Overriding  Don't confuse the concepts of overloading and overriding  Overloading deals with multiple methods in the same class with the same name but different signatures  Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature

Reusability  The main purpose behind OOOP and Inheritance in particular  New classes created with Class Module can be used in multiple projects  Each object created from the class can have its own properties

Multitier Applications  Common use of classes is 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

Three-tier Model  Most common implementation of multitier Presentation TierBusiness TierData Tier User Interface Forms Controls Menus Business Objects Validation Calculations Business Logic Business Rates Data Retrieval Data Storage

Implementing a Time Abstract Data type with a Class  VB programmers concentrate on creating their own user- defined types called classes (also referred as programmer defined types)  Classes in VB facilitate the creation of special data types, called abstract data types (ADT)

Example of a New Class

Class Scope  A class’s instance variables and methods belong to the class’s scope.  Class members that are visible can be accessed only through a “handle” (ObjectReferenceName.memberName)  Variables within methods –Only methods can access that variable  Keyword Me is a hidden instance variable can be accessed in a method by preceding its name with the keyword Me and dot operator

Controlling Access to Members  The member access modifiers Public, Private, Protected, and Friend control access to a class’s instance variables and methods.  Control access to a class’s instance variables and methods –Public: Serves primarily to present interfaces of a class –Private: Holds clients private data safely –Get and set functions have ability to access private data

Initializing Class Objects: Constructors  A constructor method initializes its class’s members  When appropriate, provide a default constructor to ensure that every object is initialized with meaningful values  Parametized constructors have arguments  If a class does not have a defined constructor, the compiler will create an empty constructor.  If an instance variable is not initialized the compiler will assign a default value  Overloaded Constructors must have different numbers and/or types and/or orders of parameters

Example of a Constructor

Properties  Methods in a class can manipulate the class’s Private instance variables. Public methods allow other object to change a class’s properties.  Get accessor –In Visual Basic instance variables as private does not guarantee data integrity  Set accessor –Cannot return values indicating a failed attempt to assign invalid data to objects of the class –Control the setting of instance variables to valid values  Get and Set accessors are not required  A property with only Get accessor is called ReadOnly  A property with only Set accessor is called WriteOnly  After we define a property, we can use it in the same way as we use a variable.

Example of a Property Definition

Composition  Composition is the use of objects of preexisting classes as members of new objects.  A form of composition is software reuse

Using the Me Reference  Every object can access a reference to itself using a Me reference.  Me explicitly  Me implicitly  The explicit use of the Me reference can increase program clarity where Me is optional

Shared Class Members  Contains only one copy of this variable in memory  When a single copy of the data will suffice, use Shared class variables to save storage.  Shared class variables are not the same as global variables because Shared class variables have class scope  A class’s shared class members are available as soon as the class is loaded into memory at execution time  Shared method has no Me reference  A shared method cannot access non-shared class members.

Const and ReadOnly Members  Const –A data member must be initialized in its declaration –Cannot be modified once initialized  ReadOnly –A data member can be initialized either in the class structure or in its declaration –Cannot be modified once initialized

Garbage Collection  Resource leaks –Objects must have an efficient way to return memory and release resources when the program no longer uses those objects  Memory leaks –In Visual Basic memory is reclaimed automatically, hence it experiences rare memory leaks as compared to C and C++  Finalization –Finalizer method performs termination housekeeping on that object just before the garbage collector reclaims the object's memory.  Feature of.NET Common Language Runtime (CLR) that cleans up unused components  Periodically checks for unreferenced objects and releases all memory and system resources used by the objects  Microsoft recommends depending on Garbage Collection rather than Finalize procedures