Presentation is loading. Please wait.

Presentation is loading. Please wait.

ATS Application Programming: Java Programming

Similar presentations


Presentation on theme: "ATS Application Programming: Java Programming"— Presentation transcript:

1 ATS Application Programming: Java Programming
Polymorphism 3.3 Polymorphism Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism © Accenture 2005 All Rights Reserved Course Code #Z16325

2 ATS Application Programming: Java Programming
Objectives 3.3 Polymorphism Explain the concept of polymorphism Describe and distinguish method overloading Describe and distinguish method overriding Explain the concepts of early binding and late binding Demonstrate when it is appropriate to implement polymorphism © Accenture 2005 All Rights Reserved Course Code #Z16325

3 Defining Polymorphism
ATS Application Programming: Java Programming Defining Polymorphism 3.3 Polymorphism Polymorphism is the ability of different objects to respond to the same message in different ways. This means that different objects can have very different method implementations for the same message. Polymorphism is the ability of a new object to implement the base functionality of a parent object in a new way. Polymorphism is an object's ability to behave differently depending on its type. Polymorphism is the ability of objects belonging to different types to respond to methods of the same name, each one according to the appropriate type-specific behavior. Notes: We provided several definitions just to be comprehensive. In object-oriented programming, polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes. © Accenture 2005 All Rights Reserved Course Code #Z16325

4 ATS Application Programming: Java Programming
Method Overloading 3.3 Polymorphism Method Overloading is the process of declaring methods with the same name but different parameter types. A method can be overloaded in the same class or in a subclass. Which overloaded method to call is based on reference type and decided at compile time. Which overridden method to call is based on object type and decided during runtime. Talking Points Since polymorphism is very dependent on object behavior, methods have several characteristics to support polymorphism. One of them is method overloading Overloading is the process of reusing a method name but attached to a different signature In Java, a method is identified uniquely by the combination of its fully qualified class name, its method name and the exact sequence of its argument types So long as at least one of the arguments of the new method is different from the original method, it is overloaded. A difference only in the return type does not constitute overloading, and is illegal Overloading is simply a reassignment of the method’s name Overloading is primarily useful if you have several very closely related methods that do almost the same job but with slightly different arguments, as it saves the need to come up with many different names and aids readability As overloading should be restricted to closely related functions, the return type will usually be the same The chief exception to this is when the return type is a function of the argument types Overloaded methods can actually call one another by using appropriate arguments Object O = new String(“Hello”); The reference type is whatever the type of “O” is. In this case, Object. The Object type is the actual type of the object created. In this case, String. © Accenture 2005 All Rights Reserved Course Code #Z16325

5 Rules of Method Overloading
ATS Application Programming: Java Programming Rules of Method Overloading 3.3 Polymorphism Overloaded methods must change the argument list. Overloaded methods can change the return type. Overloaded methods can change the access modifier. Overloaded methods can declare new or broader checked exceptions. © Accenture 2005 All Rights Reserved Course Code #Z16325

6 Implementing Method Overloading
ATS Application Programming: Java Programming Implementing Method Overloading 3.3 Polymorphism public static void main(String[] args) { Sales s = new Sales(); System.out.println(s.computeSales(100)); System.out.println(s.computeSales(100,2)); System.out.println(s.computeSales(100,2,30)); } class Sales { double computeSales(double price) { double sales; sales = price; return sales; double computeSales(double price, int qty) { sales = price * qty; double computeSales(double price, int qty, double discount) { sales = (price * qty) - discount; Faculty Notes: Java dynamically loads a class into memory during runtime as needed. Thus it is possible for the behavior of a class to vary according to what was actually loaded by the JVM. The technical reason for this is Java’s use of Late Binding (also known as Virtual Method Invocation), a characteristic of OOP languages. Since the code that will be executed is a function of an object’s true class (as recorded in memory when it is created) rather than the type of the variable referencing it, the code cannot properly be evaluated until runtime (when the object actually exists to be queried). The JVM was designed from the beginning for OOP use, and so is able to determine the true class of an object at runtime, irrespective of the type of the reference variable. 100.0 200.0 170.0 © Accenture 2005 All Rights Reserved Course Code #Z16325

7 ATS Application Programming: Java Programming
Method Overriding 3.3 Polymorphism Method Overriding allows a subclass to redefine methods of the same name from the superclass. The key benefit of overriding is the ability to define/defer behavior specific to subclasses. Which overridden method to call is based on object type and decided at runtime. Another characteristic of methods mainly used in polymorphism is method overriding © Accenture 2005 All Rights Reserved Course Code #Z16325

8 Rules of Method Overriding
ATS Application Programming: Java Programming Rules of Method Overriding 3.3 Polymorphism An overridden method must have the same name the same number of parameters and types the same return type as the overridden method. Overriding a method cannot narrow the method access level defined in the overridden method. Overriding a method cannot widen checked exceptions defined in the overridden method. Methods declared as private, static, or final cannot be overridden. A static method cannot override an instance method. The overriding method should have a less restrictive access modifier. © Accenture 2005 All Rights Reserved Course Code #Z16325

9 Implementing Method Overriding
ATS Application Programming: Java Programming Implementing Method Overriding 3.3 Polymorphism public static void main(String[] args) { Sales s = new Sales(); Sales st = new SalesTax(); System.out.println(s.computeSales(100)); System.out.println(s.computeSales(100,2)); System.out.println(s.computeSales(100,2,30)); System.out.println(st.computeSales(100)); } class Sales { double computeSales(double price) { double sales = price; return sales; double computeSales(double price, int qty) { double sales = price * qty; double computeSales(double price, int qty, double discount) { double sales = (price * qty) - discount; class SalesTax extends Sales { double sales = price * 1.10; // add tax Define Sales reference variable containing SalesTax object It determined which method to run based on object type (SalesTax) instead of reference type (Sales) Faculty Notes: 100.0 200.0 170.0 110.0 © Accenture 2005 All Rights Reserved Course Code #Z16325

10 Overloading vs Overriding
ATS Application Programming: Java Programming Overloading vs Overriding 3.3 Polymorphism Criteria Overloaded Method Overridden Method Argument list Different Same Return type Can change Exceptions Cannot be wider Access level Cannot be narrower Invocation Based on reference type and decided at compile time Based on object type and decided at runtime Key differences between overloading and overriding: Overloaded methods supplement each other; an overridden method (mostly) replaces its parent Any number of overloaded methods can exist within the same class; a method in a parent class can only be overridden once per subclass Overloaded methods must have different argument lists; overridden methods must have identical argument lists (in terms of type and order) An overloaded method may have any return type; an overridden method must have an identical return type to the method it overrides © Accenture 2005 All Rights Reserved Course Code #Z16325

11 Early Binding and Late Binding
ATS Application Programming: Java Programming Early Binding and Late Binding 3.3 Polymorphism Early binding means translating operations or associating identifiers during compile time. Late binding means delaying translation of an operation or associating identifiers at runtime (also known as dynamic method lookup or virtual method invocation ). It is used in polymorphism to determine the actual method invoked (depending on the type of the actual object). © Accenture 2005 All Rights Reserved Course Code #Z16325

12 Implementing Polymorphism
ATS Application Programming: Java Programming Implementing Polymorphism 3.3 Polymorphism class Animal { public void eat() { System.out.println("Animal eating...");} } class Snake extends Animal { public void eat() { System.out.println("Snake eating...");} class Horse extends Animal { public void eat() { System.out.println("Horse eating...");} public void eat(String s) { System.out.println("Horse eating " + s); } public static void main(String[] args) { Animal pig = new Animal(); Snake viper = new Snake(); Horse stallion = new Horse(); Animal animalViper = new Snake(); Animal animalStallion = new Horse(); pig.eat(); // will this compile? Output? pig.eat("left overs"); //will this compile? Output? viper.eat(); // will this compile? Output? viper.eat("chicken"); //will this compile? Output? stallion.eat(); // will this compile? Output? stallion.eat("grass"); // will this compile? Output? animalViper.eat(); // will this compile? Output? animalViper.eat("rat"); //will this compile? Output? animalStallion.eat(); // will this compile? Output? animalStallion.eat("Carrots"); //will this compile? Output? Animal eating... Snake eating... Horse eating... Horse eating grass Faculty Notes: © Accenture 2005 All Rights Reserved Course Code #Z16325

13 ATS Application Programming: Java Programming
Key Points 3.3 Polymorphism Polymorphism is an object's ability to behave differently depending on its type Method Overloading is the process of declaring methods with the same name but different parameter types Method Overriding allows a subclass to redefine methods of the same name from a superclass Early binding translates identifiers at compile-time while late binding translates identifiers during runtime Late binding is used in polymorphism to determine the actual method invoked at runtime © Accenture 2005 All Rights Reserved Course Code #Z16325


Download ppt "ATS Application Programming: Java Programming"

Similar presentations


Ads by Google