Unit 2 – Part 4. Polymorphism(OOP)
Total Page:16
File Type:pdf, Size:1020Kb
Unit 2 – part 4. Polymorphism(OOP) BCIS 222. Object Oriented Programming Instructor: Dr. Carlo A. Mora LMS: http://l earn.nmsu.ed u OOP Principles y Piece of Cake!! A (Abstraction) E P (Encapsulation) (Polymorphism) I (Inheritance) protected Members y Intermediate level of protection between public and private y prottdtected members accessible to y superclass members y subclass members y Clbhlass members in the same namespace y Subclass access superclass member y Keyword base and a dot (.) y E.g. base.toString() y Method overloading vs. Method overriding y Overloading Having several methods with same name but different parameters y Overriding redefining a method in a subclass y How to access and overridden method? super keyword UiUsing protec tdted iitnstance variiblables y Advantages y subclasses can modify values directly y Slight increase in performance y Avoid set/get function call overhead y Disadvantages y No validityyg checking y subclass can assign illegal value y Implementation dependent y subcl ass method s more likliklely depend ent on superclass iilmplemen ta tion y superclass implementation changes may result in subclass modifications y Fragile (brittle) software Wrapping up the Case Study y What advantages do we have by having a class Hierarchy? y No need to declare common members in subclasses (e.g. name) y Simplified version of methods (e.g. toString) y Wha t is the purpose of y GetGraduationDate method? y Rethink the TestClass y What if I need to create an array to hold different types of students? Polymorphism y Dictionary definition : the quality or state of being able to assume different forms y Enables developers to write programs in general fashion y Handle vari ety of exi stin g and yet-to-be-specifi ed cl asses y When a program invokes a method through a superclass variable, the correct subclass version of the method is calle d, base d on the type of the reference stored in the superclass variable y The same method name and signature can cause different actions to occur, depending on the type of object on which the method is invoked Relationship between Superclass Objects and Su bc lass Object s y Subclass object y Can be treated as superclass object y UG is always a Person!! ☺ y Reverse is not true y Person is not always a UG!! y A subclass reference can be aimed at a superclass object only if the object is dow ncasted y Casting operator convert y Between ppyp(rimitive numeric types (int double) y Between related class types (Person UG) Implicit Subclass-Object-to-Superclass- Object Conversi on y Implicit conversion y Subclass reference to superclass reference y Subclass object “is a” superclass object y Four ways to mix and match references y Refer to superclass object with superclass reference y p1= p2 y Refer to subclass object with subclass reference y ug1 = ug2 y Refer to subclass object with superclass reference y Can refer only to superclass members y p1 = ug1 y RfRefer to superclass object with sublbclass reference y ug1 = p1 y Syntax error (if Downcasting is not used) Why is this relevant after all? y Assume we have an array of students ug1 g1 ug2 ug3 g2 ug5 y How to find the common information (e.g. avg gpa, count, etc.)? y Superclass method? each category will have something different y How about calling their own method? ☺ y Done automatically for us ☺ Dynamic Method Binding y Dynamic method binding y Implements polymorphic processing of objects y Use superclass reference to refer to subclass object y Program chooses “correct” method in subclass y New Classes and Dynamic Binding y Also called late binding y Object’s type need not be know at compile time y At run time, call is matched with method of called object y Downcasting y Convert a reference to a superclass to a reference to a subclass y Allowed only if the object has an is-a relationship with the subclass Sealed Methods and Classes y Sealed method y Cannot be overridden in subclass y Sealed methods are resolved at compile time, this is known as static binding y Compilers can optimize by inlining the code y Sealed class y Cannot be superclass (cannot be extended) y Class cannot inherit final classes y private and static methods are implicitly final y A class declared final cannot not be a superclass!.