Advanced Interactive Programming Object Oriented Programming Cont' - PowerPoint PPT Presentation

1 / 58
About This Presentation
Title:

Advanced Interactive Programming Object Oriented Programming Cont'

Description:

Microsoft online MSDN library. http://msdn2.microsoft.com/en-us/library/527aztek.aspx ... See MSDN library. Collections in Visual Basic. The Visual Basic ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 59
Provided by: ChrisN83
Category:

less

Transcript and Presenter's Notes

Title: Advanced Interactive Programming Object Oriented Programming Cont'


1
Advanced Interactive ProgrammingObject Oriented
Programming (Cont.)
2
Review
  • Concepts and terms in OOP
  • Object
  • Class
  • Message
  • Information hiding
  • Data abstraction
  • Encapsulation
  • Object oriented programming in VB IDE
  • Create and manipulate classes, objects, methods
    and properties
  • Use classes and object in applications

3
Outline
  • Concepts and terms in OOP
  • Inheritance
  • Interface
  • Collection
  • Polymorphism
  • Delegation
  • Object oriented programming in VB IDE
  • Create and implement interfaces
  • Use interface inheritance and polymorphism in
    applications

4
Inheritance
  • A mechanism for creating new classes using
    classes that have already been defined.
  • The new class is referred to as a derived class
    or a child class or a subclass.
  • The existing class is referred to as a base
    class, a parent class or a superclass.

5
Relationship between a Subclass and a Superclass
  • A subclass inherits all instance attributes and
    behaviors of the superclass from which it is
    derived.
  • A subclass can override the definition of
    existing methods by providing its own
    implementation method overriding
  • Overridden members must accept the same data type
    and number of arguments
  • The subclass can be subclassed further
  • Derived classes inherit overridden members

6
Method Overriding Example
In the superclass
Public Function Addition(num1 As Integer, num2 As
Integer) As Integer Addition num1 num2 End
Function
7
Comparison Method Overloading
  • A class method can have several signatures
  • Each signature provides a different version of
    the method, which have the same name, but accept
    different number of arguments, or arguments with
    different data types.

Public Function Addition(num1 As Integer, num2 As
Integer) As Integer Addition num1 num2 End
Function
Public Function Addition(num1 As Integer, s As
String, num2 As Integer) As Integer Addition
num18 (num2 3) CInt(s) End Function
8
Relationship between a Subclass and a Superclass
  • A subclass can define its own attributes and
    behaviors in terms of application requirements -
    extension.
  • A subclass consists only of the code for the
    changes and additions to the superclass.

9
Generalization
  • Generalization refers to the relationship between
    a class and one or more refined versions of it.
  • Example a fruit is a generalization of apple,
    orange, mango and many others.
  • Generalization implies the process of abstracting
    common properties and behaviors and creating
    shared control code for objects with sufficiently
    similar interface.
  • Generalization usually leads to a superclass in
    OOP.

10
Specialization
  • Specialization also refers to the relationship
    between a class and one or more refined versions
    of it.
  • Example an apple is a specialization of fruit.
  • If class A is a generalization of class B, then
    class B is a specialization of class A
  • Specialization implies the process of defining a
    new object based on a (typically) more narrow
    definition of an existing object.
  • Specialization, also known as subtyping, leads to
    a subclass through extension and method overriding

11
Class Hierarchy
  • Inheritance relationships form a treelike
    structure called a class hierarchy.
  • A class can become (or have) a superclass and a
    subclass when it is used to form a hierarchy.
  • There is a is a relationship between a subclass
    and a superclass.
  • Every object of a subclass is an object of that
    subclass superclass but this does not hold
    conversely.

12
Class Hierarchy Example
CShape
CTwoDimensionalShape
CThreeDimensionalShape
CSphere
CCube
CCylinder
CTriangle
CSquare
CCircle
A portion of a CShape class hierarchy
13
Class Hierarchy in C
14
(No Transcript)
15
Class Hierarchy in Java
Class Hierarchy in Java
16
Classes in VB
17
The Power of Inheritance
  • No need to write new classes from scratch but
    reuse ones from libraries through inheritance.
  • No need to know the implementation details, just
    the interfaces.
  • Meet your requirements by overriding and
    extension.
  • Substantial and useful class libraries are
    available for reuse.
  • Programming is subclassing and composition

18
References
  • Encapsulation and Inheritance in Object-Orlented
    Programming Languages by Alan Snyder, Software
    Technology Laboratory, Hewlett-Packard
    Laboratories at http//delivery.acm.org/10.1145/30
    000/28702/p38-snyder.pdf?key128702key2531245041
    1collGUIDEdlGUIDECFID65582496CFTOKEN317688
    93
  • The online free encyclopedia
  • http//en.wikipedia.org/wiki/Object-oriented_progr
    amming
  • Microsoft online MSDN library
  • http//msdn2.microsoft.com/en-us/library/527aztek
    .aspx
  • Sun MicroSystem online documentation
  • http//java.sun.com/docs/books/tutorial/java/conc
    epts/object.html

19
Any Questions?
20
Interface
21
Interface
  • An interface, like a class, defines a set of
    properties, methods, and events. But unlike a
    class, an interface does not provide
    implementation.
  • Interfaces are usually not used to create
    objects.
  • interfaces are implemented by classes.
  • An interface is a contract.

22
Interface
  • A class can implement many interfaces as it
    needs.
  • A subclass will automatically inherit the
    implemented interfaces of the class from which it
    is derived.
  • In VB, interfaces are treated the same as
    classes. Some other OOP languages treats
    interfaces as separated entities from classes.

23
Examplesin Java
24
Interface Example Scenario
  • Consider a shape, point and circle class
    hierarchy
  • Develop an interface class called IShape
  • Develop class CPoint and CCircle that implement
    IShape interface
  • Use a form to experience the use of classes and
    the interface

25
Creating an Interface
  • Any class can serve as an interface.
  • An interface is created in the same way as any
    other class.
  • IShape defines three public methods - Area, Name
    and ToString, which are all empty.

26
Using an Interface
  • Use keyword Implements to implement an interface
    in a class.
  • Every concrete class implementing an interface
    must implement all methods within the class.
  • Implemented methods could still be empty, but you
    need to put the shell method there.
  • Naming conventions
  • Classes can define its own instance variable and
    methods.

27
The CCircle Class
28
Accessing an Interfaces Methods
  • Interfaces define methods as Public
  • Classes implementing interfaces code methods, but
    as Private
  • Objects of classes cannot access interface
    methods directly

CCircle Class
IShape Interface
29
Accessing an Interfaces Methods
  • Use an interface object to access its implemented
    methods
  • Interface objects refer to the objects of a class
    that implements the interface.
  • Create an interface reference and assign it an
    object of a class
  • Classs own defined instance variable and methods
    can still be accessed in terms of its access
    specifier

30
Using Interfaces and Classes
31
Implementation Inheritance
  • The ability that a subclass can inherit the
    public interfaces and their implementations from
    its superclass.
  • For example, we develop a class CCircle with all
    methods as public. We create a new class
    CRoundPlate by inheriting CCircle. Then we create
    an object of CRoundPlate called myRoundPlat. Then
    I can use myRoundPlat .Area() to calculate the
    area directly. No need to rewrite the method
    implementation
  • VB does not support this feature, only
    illustration

32
Interface Inheritance
  • The ability to inherit the public interface but
    not the implementation of a class.
  • VB supports interface inheritance.
  • Use keyword Implements.

For example, a class Implements an interface such
as IShape2 class will need to re-implement all
methods
33
Any Questions?
34
Collection
35
Collection Concept
  • A way of grouping a set of related items
  • A collection usually consists of a group of
    objects of different types, known as its elements
  • Example collections include set, array and list
  • Some are ordered and others unordered
  • Iteration and enumeration utility classes
  • Provide mechanism to walk through all objects in
    a collection

36
Forms and Controls Collections
  • VB provides default form and control collection
    objects for managing and accessing several of
    them easier
  • Collection members have index values
  • These collections are 0-based, i.e. the index
    value starts from 0
  • Collections have a Count property

37
Example Forms Collections
4 - 1
38
The Generic Collection Class
  • Unlike the default Forms and Controls collections
  • Used for grouping programmers own objects
  • Generic collections are 1-based
  • The generic collection has a single property
    Count
  • It must first be declared and bound to a variable
    name

39
Generic Collection Methods
  • The Add method is used to add members to the
    collection
  • The Remove method is used to remove members from
    the collection
  • The Item method is used to return single members
    of the collection when furnished an index value

40
A Generic Collection Example
Set Queue New Collection Set Account New
AccountObj Account.AssignOwner (Fred) Queue.Add
(Account) Set Account New AccountObj Account.Ass
ignOwner (Mary) Queue.Add (Account) Print
Queue.Count 2 Print Queue(1).Owner Fred Print
Queue(2).Owner Mary
41
Collection in OOP
  • Very common because we need to manage a set of
    objects they might all have the same class or
    derived from different subclasses of the same
    superclass.
  • More functionalities, for example, performing
    type or existence testing when adding an object
    or retrieving objects in terms of other
    attributes keys, the textual index.
  • Extensive use of Iterator or Enumerator class to
    manipulate individual objects

42
References
  • See MSDN library
  • Collections in Visual Basic
  • The Visual Basic Collection Object

43
Polymorphism
44
Motivating Programming Problems
  • CShape
  • CTwoDimensionalShape
  • CCircle
  • CSquare
  • CTriangle, etc.
  • Common methods
  • Calculate Area, perimeter, etc.
  • Draw the shape
  • Each has a different way to do these

CShape
CTwoDimensionalShape
CTriangle
CSquare
CCircle
45
Traditional Solutions
  • Type fields and Select Case statements
  • Select Case Statements
  • Use type fields to distinguish among object types
  • Call upon an appropriate action of the object
  • Problems
  • Forget type test
  • Forget all possible cases
  • Every addition or deletion of a class require
    inserting new cases in all relevant Select Case
    statements

46
Polymorphism
  • Methods are defined in higher level classes but
    coded in various ways in subclasses as long as
    the class interface is unchanged.
  • Method names, parameter names and types must stay
    the same.
  • This is polymorphism, the many shapes of a
    method.

CShape
CTwoDimensionalShape
CTriangle
CSquare
CCircle
47
Polymorphism
  • Polymorphism refers to the ability to define
    multiple classes with functionally different, yet
    identically named methods or properties that can
    be used interchangeably by client code at run
    time.
  • Inheritance-Based Polymorphism
  • Defines methods in a superclass
  • Overrides them with new implementations in
    subclasses
  •  Interface-Based Polymorphism
  • Defines methods in an interface
  • Implements them differently in classes  

48
Polymorphism Extending the Interface Example
  • Consider a shape, point and circle hierarchy
  • Extend it with a method Volume and a new class
    cylinder
  • Develop an interface class called IShape
  • Create a new interface IShape2
  • Develop class CPoint and CCircle that implement
    IShape interface
  • Add a CCylinder class
  • Use a form to experience the use of classes and
    the interface
  • Use an array as a collection

49
Creating Interfaces and Classes
  • The same way as we did before
  • Add a new shell method in the IShape2
  • Implement the Volume method in all classes as
    below in CCircle class
  • Create the CCylinder class

50
The CCylinder Class
51
Using Polymorphism
52
Supporting Polymorphism
  • Name binding refers to the association of values
    with identifiers
  • Early binding
  • Associate an object to a specific reference type
  • Late binding or dynamic binding
  • Associate an object to a reference of Object
    class. The validity of object reference is
    resolved at run time
  • In late binding, the programmer does not have to
    know the exact type of the object in advance

53
Delegation
  • Common sense understanding to hand over a task
    to another person, usually a subordinate.
  • In OOP, it means the handing of a task over to an
    implemented object
  • Delegation refers to the mechanism that allows a
    method to be invoked indirectly by way of a
    reference to the method

54
Delegation in VB
  • Interfaces could contain
  • only shell methods, so-called abstract class
  • a combination of shell methods and
    implemented methods
  • Classes implementing an interface
  • must implement all interface methods no matter
    whether they are implemented or not
  • Could create an object for the partially
    implemented interface, usually called inner
    object
  • Delegation
  • Subclasses provides implementation by delegating
    responsibility to the inner object for
    implementation
  • The object of a subclass is usually called outer
    object

55
Example
shell methods
implemented methods
56
Example (cont.)
Class definition
Implements interface
Create an interface object
Delegation
57
Discussion
  • Delegation and interface inheritance can be used
    to simulate implementation inheritance in VB, but
    does not make much sense
  • Delegation could be selective
  • In one method you might delegate directly to the
    inner object, passing the arguments unchanged
  • while in another method you might execute some
    code of your own before calling the inner object
  • in a third method you might execute only your own
    code, ignoring the inner object altogether

58
Summary
  • Inheritance
  • Interface
  • Collection
  • Polymorphism
  • Programming in VB
Write a Comment
User Comments (0)
About PowerShow.com