Professional Documents
Culture Documents
Outline Materi
Introduction Polymorphism Derived-Class-Object to Base-Class-Object Conversion Abstract Classes and Methods Operator Overloading
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
Polymorphism
There are two powerful aspects to inheritance. One is code reuse. When you create a ListBox class, youre 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
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.
11
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.
13
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
15