Title: Advanced Interactive Programming Object Oriented Programming Cont'
1Advanced Interactive ProgrammingObject Oriented
Programming (Cont.)
2Review
- 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
3Outline
- 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
4Inheritance
- 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.
5Relationship 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
6Method Overriding Example
In the superclass
Public Function Addition(num1 As Integer, num2 As
Integer) As Integer Addition num1 num2 End
Function
7Comparison 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
8Relationship 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.
9Generalization
- 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.
10Specialization
- 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
11Class 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.
12Class Hierarchy Example
CShape
CTwoDimensionalShape
CThreeDimensionalShape
CSphere
CCube
CCylinder
CTriangle
CSquare
CCircle
A portion of a CShape class hierarchy
13Class Hierarchy in C
14(No Transcript)
15Class Hierarchy in Java
Class Hierarchy in Java
16Classes in VB
17The 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
18References
- 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
19Any Questions?
20Interface
21Interface
- 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.
22Interface
- 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.
23Examplesin Java
24Interface 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
25Creating 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.
26Using 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.
27The CCircle Class
28Accessing 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
29Accessing 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
30Using Interfaces and Classes
31Implementation 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
32Interface 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
33Any Questions?
34Collection
35Collection 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
36Forms 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
37Example Forms Collections
4 - 1
38The 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
39Generic 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
40A 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
41Collection 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
42References
- See MSDN library
- Collections in Visual Basic
- The Visual Basic Collection Object
43Polymorphism
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
45Traditional 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
46Polymorphism
- 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
47Polymorphism
- 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 Â
48Polymorphism 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
49Creating 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
50The CCylinder Class
51Using Polymorphism
52Supporting 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
53Delegation
- 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
54Delegation 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
55Example
shell methods
implemented methods
56Example (cont.)
Class definition
Implements interface
Create an interface object
Delegation
57Discussion
- 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
58Summary
- Inheritance
- Interface
- Collection
- Polymorphism
- Programming in VB