Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding."— Presentation transcript:

1 Object Based Programming

2 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

3 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

4 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

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

6 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 7 Inheritance  Inheritance relationships are often shown graphically in a class diagram, with the arrow pointing to the parent class

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

9 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 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 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 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.

13 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

14 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

15 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 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 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

18 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

19 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

20 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

21 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)

22 Example of a New Class

23 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

24 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

25 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

26 Example of a Constructor

27 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.

28 Example of a Property Definition

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

30 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

31 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.

32 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

33 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


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

Similar presentations


Ads by Google