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

1 / 46
About This Presentation
Title:

Advanced Interactive Programming Object Oriented Programming Cont'

Description:

A subclass inherits all instance attributes and behaviors of the superclass from ... We create a new class CRoundPlate by inheriting CCircle. ... – PowerPoint PPT presentation

Number of Views:126
Avg rating:3.0/5.0
Slides: 47
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 objects in applications

3
Outline
  • Concepts and terms in OOP
  • Inheritance
  • Interface
  • Polymorphism
  • Object oriented programming in VB IDE
  • Program interfaces
  • Program polymorphism
  • 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.
  • For example, car manufacturing

5
Relationship between a Subclass and a Superclass
  • A subclass inherits all instance attributes and
    behaviors of the superclass from which it is
    derived.

In the superclass - CClassA
Public Function Addition(num1 As Integer, num2 As
Integer) As Integer Addition num1 num2 End
Function
In the subclass - CClassB
Dim objB As CClassB Set objB New CClassB Print
objB.Addition(4, 5)
6
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

7
Method Overriding Example
In the superclass
Public Function Addition(num1 As Integer, num2 As
Integer) As Integer Addition num1 num2 End
Function
8
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
9
Relationship between a Subclass and a Superclass
  • A subclass can define its own attributes and
    behaviors in terms of application requirements
    referred to as extension.
  • A subclass consists only of the code for the
    changes and additions to the superclass.
  • Overriding implication
  • Which method is called
  • Which method can be overridden
  • For example, bank account, interest calculation

10
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.
  • In OOP, generalization implies the process of
    abstracting common properties and behaviors, and
    creating an object at high level abstraction for
    objects with sufficiently similar interface.
  • Generalization usually leads to a superclass.
  • Other examples Student, University staff,
    Account, etc.

11
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

12
Class Hierarchy
  • Inheritance relationships form a tree like
    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.

13
Class Hierarchy Example
CShape
CTwoDimensionalShape
CThreeDimensionalShape
CSphere
CCube
CCylinder
CTriangle
CSquare
CCircle
A portion of a CShape class hierarchy
14
Class Hierarchy in C
15
(No Transcript)
16
Class Hierarchy in Java
Class Hierarchy in Java
17
Classes in VB
18
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

19
When and Where to Use Inheritance
  • Inheritance hierarchy represents an "is-a"
    relationship and not a "has-a" relationship
  • Reuse code from the base classes
  • Need to apply the same class and methods to
    different data types
  • The class hierarchy is reasonably shallow, and
    other developers are not likely to add many more
    levels
  • Want to make global changes to derived classes by
    changing a base class

20
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

21
Any Questions?
22
Interface
23
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.
  • An interface is a contract or protocol.
  • Interfaces are implemented by classes, i.e. a
    class that implements an interface must implement
    every aspect of that interface exactly as it is
    defined.

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

25
Examplesin Java
26
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

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

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

29
The CCircle Class
30
Programming 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
31
Accessing an Interfaces Methods
  • Use an interface object to access its implemented
    methods
  • Interface objects are created by declaring an
    interface reference and assign it an object of a
    class
  • Classs own defined instance variable and methods
    can still be accessed using its instance in terms
    of its access specifier

32
Using Interfaces and Classes
33
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

34
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
35
Any Questions?
36
Polymorphism
37
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
38
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

39
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
40
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  

41
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

42
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

43
The CCylinder Class
44
Using Polymorphism
45
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

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