Document 15063148

advertisement
Mata kuliah : M0874 – Programming II
Tahun
: 2010
Object-Oriented Programming Polymorphism
Session 07
Outline Materi
•Introduction
•Polymorphism
•Derived-Class-Object to Base-Class-Object
Conversion
•Abstract Classes and Methods
•Operator Overloading
Bina Nusantara University
3
Introduction
•Polymorphism enables us to “program in the general” rather
than “program in the specific”.
•Enables us to write applications that process objects that
share the same base class in a class hierarchy as if they
were all objects of the base class.
•Polymorphism can design and implement systems that are
easily extensible-new classes can be added with little or no
modification to the general portions of the application, as
long as the new classes are part of the inheritance hierarchy
that the application processes generically.
Bina Nusantara University
4
Polymorphism
•There are two powerful aspects to inheritance. One is code
reuse. When you create a ListBox class, you’re able to reuse
some of the logic in the base (Window) class.
•What is arguably more powerful, however, is the second
aspect of inheritance: polymorphism. Poly means many and
morph means form. Thus, polymorphism refers to being
able to use many forms of a type without regard to the
details.
•Polymorphism enables us to write programs in a general
fashions to handle wide variety of existing and future related
classes.
Bina Nusantara University
5
6
7
Derived-Class-Object to Base-Class-Object Conversion
•A Base Class With a Virtual Method: DrawingObject.cs
•This will be the base class for other objects to inherit
from.
•It has a single method named Draw(). The Draw()
method has a virtual modifier. The virtual modifier
indicates to derived classes that they can override this
method. The Draw() method of the DrawingObject
class performs a single action of printing the statement,
"I'm just a generic drawing object.", to the console.
8
Derived Classes With Override Methods: Line.cs, Circle.cs,
and Square.cs
•These classes inherit the DrawingObject
class.
•Each class has a Draw() method and each
Draw() method has an override modifier.
•The override modifier allows a method to
override the virtual method of its base class at
run-time.
•The override will happen only if the class is
referenced through a base class reference.
Overriding methods must have the same
signature, name and parameters, as the
virtual base class method it is overriding.
Bina Nusantara University
9
Program Implementing Polymorphism:
DrawDemo.cs
•This program implements
polymorphism. In the Main() method
of the DrawDemo class, there is an
array
being created.
•The type of object in this array is
the DrawingObject class.
•The array is named dObj and is
being initialized to hold four
objects of type DrawingObject.
Bina Nusantara University
10
•Next the dObj array is initialized. Because of their
inheritance relationship with the DrawingObject class, the
Line, Circle, and Square classes can be assigned to the
dObj array. Without this capability, you would have to create
an array for each type. Inheritance allows derived objects to
act like their base class, which saves work.
Output:
I'm a Line.
I'm a Circle.
I'm a Square.
I'm just a generic drawing object.
Bina Nusantara University
11
Abstract Classes and Methods
•When we think of a class as a type, we assume that
programs will create objects of that type.
•However, there are cases in which it is useful to define
classes for which the programmer never intends to
instantiate any objects. Such classes are called abstract
classes.
•The purpose of an abstract class is to provide an
appropriate base class from which other classes may inherit.
•Classes from which objects can be instantiated are called
concrete classes.
Bina Nusantara University
12
Operator Overloading
•Manipulation on class objects are accomplished by sending
messages (in the form of method calls) to the objects.
•This method-call notation is cumbersome for certain kinds
of classes, especially mathematical classes.
•For these classes, it would be convenient to use C#’s rich
set of built-in operators to specify object manipulations.
•Use operator overloading when it makes a program clearer
than accomplishing the same operations with explicit method
calls.
Bina Nusantara University
13
Bina Nusantara University
14
References
•http://www.aspfree.com/c/a/CSharp/Polymorphism-in-C-Sharp/
•http://www.codeproject.com/KB/cs/csharpintro01.a
spx
•http://books.google.co.id/books?id=bG_Aqb6iOUY
C&dq=control+properties+and+layout+c%23&q=cha
r+methods#v=onepage&q=&f=false
Bina Nusantara University
15
Download