ATS Application Programming: Java Programming

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Advertisements

METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Programming With Java ICS Chapter 8 Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter -6 Polymorphism
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 7.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Classes, Interfaces and Packages
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
BY:- TOPS Technologies
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
EKT472: Object Oriented Programming Overloading and Overriding Method.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Overriding Method.
Inheritance and Polymorphism
Types of Programming Languages
ATS Application Programming: Java Programming
Object Oriented Programming
Chapter 9 Inheritance and Polymorphism
Inheritance Basics Programming with Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Polymorphism and access control
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
CMSC 202 Polymorphism.
METHOD OVERRIDING in JAVA
Polymorphism Polymorphism
Computer Programming with JAVA
Java – Inheritance.
Polymorphism CT1513.
Polymorphism Polymorphism - Greek for “many forms”
Polymorphism.
Chapter 9 Carrano Chapter 10 Small Java
Sampath Kumar S Assistant Professor, SECE
Chapter 11 Inheritance and Polymorphism Part 1
C++ Object Oriented 1.
Chapter 7 Inheritance.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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