Unformatted text preview:

Slide 1OutlineSlide 3PolymorphismPolymorphism in JavaExamples of polymorphism in JavaSlide 7Slide 8Why polymorphism?Slide 10Slide 11Slide 12Slide 13Slide 14Slide 1510.4 Abstract Classes and MethodsSlide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 2310.5 Payroll System Using PolymorphismSlide 25Slide 26Slide 2710.6 final Methods and ClassesSlide 2910.7 Creating and Using InterfacesJava InterfaceSlide 32Slide 33Slide 34Slide 35Example program with interfaceUML diagram for InterfaceInterfaces vs abstract classesInterfaces vs Abstract Classes10.7.1 Developing a Payable HierarchyInterface HierarchyNo Multiple Inheritance in Java!Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Slide 50Slide 51Slide 52Slide 53Slide 5410.7.7 Common Interfaces of the Java APISlide 56Slide 57Polymorphism1-Based on slides from Deitel & Associates, Inc.- Revised by T. A. YangOutline2•Introduction•Inheritance and polymorphism•Implementing polymorphism in Java:1) Operator /method Overloading2) Method Overriding3) Abstract and Concrete Classes4) Interfaces34Polymorphism•Meaning of polymorphism–“the quality or state of existing in or assuming different forms” (http://www.merriam-webster.com/dictionary/polymorphism)–“a principle in biology in which an organism or species can have many different forms or stages” (http://download.oracle.com/javase/tutorial/java/IandI/polymorphism.html)5Polymorphism in Javao“The capability of an action or method to do different things based on the object that it is acting upon” (http://www.xyzws.com/Javafaq/what-is-runtime-polymorphism-in-java/16) –Polymorphism allows you define one interface and have multiple implementations.o“Polymorphism enables you to write programs that process objects that share the same superclass as if they’re all objects of that superclass.” (Deitel & Deitel)o“The ability of a reference variable to change behavior according to what object instance it is holding.” (http://www.javapassion.com/javase/javapolymorphism.pdf)6Examples of polymorphism in Java•Operator Overloading⁻An operator, like ‘+’, can handle different types of operands (e.g., int, float, String).•Method Overloading⁻Methods with the same name (but different signatures) are defined within a class. e.g., System.out.print( ) is overloaded in PrintStream.•Method Overriding⁻A method defined in a superclass is re-defined/specialized in a subclass by a method with the same signature.•Abstract and Concrete Classes⁻The virtual/abstract methods defined in an abstract class are implemented in its subclasses.•Interfaces ⁻A Java interface defines a set of methods but does not implement them. ⁻A class that implements the interface agrees to implement all of the methods defined in that interface, thereby agreeing to certain behavior. (http://www.iam.ubc.ca/guides/javatut99/java/more/interfaceDef.html)7•Method OverridingA method defined in a superclass is re-defined or specialized in a subclass by a method with the same signature.−A reference of a subclass may be assigned to a variable of the superclass. (This is allowed because each subclass object is an object of its superclass.)−A method defined in the superclass is overridden in the subclass....SuperClass obj1 = new SuperClass();SuperClass obj2 = new SubClass();...obj1.someMethod(); // SuperClass version is calledobj2.someMethod(); // SubClass version is called....(source: http://geekexplains.blogspot.com/2008/06/dynamic-binding-vs-static-binding-in.html)8•An example program: InheritanceAndPolymorphism // a simple application that illustrates the basic idea of polymorphismpackage Animals;public class Animal { public void whoAmI() { System.out.println("I am a generic Animal."); }}//Driver programimport Animals.*;class PolymorphismDemo { public static void main(String[] args) { Animal ref1 = new Animal(); Animal ref2 = new Dog(); Animal ref3 = new Cat(); Animal ref4 = new Bird(); ref1.whoAmI(); ref2.whoAmI(); ref3.whoAmI(); ref4.whoAmI(); }}package Animals;public class Dog extends Animal { @Override public void whoAmI() { System.out.println("I am a Dog."); }}package Animals;public class Cat extends Animal { @Override public void whoAmI() { System.out.println("I am a Cat."); }}package Animals;public class Bird extends Animal { @Override public void whoAmI() { System.out.println("I am a Bird."); }}9•Extensibility: With polymorphism, we 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 program, as long as the new classes are part of the inheritance hierarchy that the program processes generically. –The only parts of a program that must be altered to accommodate new classes are those that require direct knowledge of the new classes that we add to the hierarchy. •Easy maintenance of changes: When new classes are added, existing client codes do not need to be changed.Why polymorphism?1011•When a superclass variable contains a reference to a subclass object, and that reference is used to call a method, the subclass version of the method is called. –The Java compiler allows this “crossover” because an object of a subclass is an object of its superclass (but not vice versa). •When the compiler encounters a method call made through a variable, the compiler determines if the method can be called by checking the variable’s class type. –If that class contains the proper method declaration (or inherits one), the call is compiled. •At execution time, the type of the object to which the variable refers determines the actual method to use. –This process is called dynamic binding.Dynamic binding12•“In programming languages, name binding is the association of objects (data and/or code) with identifiers.” (http://en.wikipedia.org/wiki/Name_binding)•Invoking a method on a subclass object via a superclass reference invokes the subclass functionality at run time.  dynamic binding•The type of the referenced object, not the type of the variable, determines which method is called.•c.f., Static methods and instance variables are bound at compilation time.  static bindingDynamic (or Run-time) binding versus Static (or compilation-time) binding13Fig. 10.1: Another example of method overriding14151610.4AbstractClassesandMethods•Abstract classes–Sometimes it’s useful to declare classes


View Full Document

UHCL CSCI 3134 - Polymorphism

Download Polymorphism
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Polymorphism and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Polymorphism 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?