150 likes | 311 Views
Object-Oriented Programming Polymorphism Session 07. Mata kuliah : M0874 – Programming II Tahun : 2010. Outline Materi. Introduction Polymorphism Derived-Class-Object to Base-Class-Object Conversion Abstract Classes and Methods Operator Overloading. Introduction.
E N D
Object-Oriented Programming PolymorphismSession 07 Mata kuliah : M0874 – Programming II Tahun : 2010
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.
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.
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.
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.
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.
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.
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.
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.
References http://www.aspfree.com/c/a/C-Sharp/Polymorphism-in-C-Sharp/ http://www.codeproject.com/KB/cs/csharpintro01.aspx http://books.google.co.id/books?id=bG_Aqb6iOUYC&dq=control+properties+and+layout+c%23&q=char+methods#v=onepage&q=&f=false