Software Engineering Module Core of Java Topic Polymorphism

  • Slides: 18
Download presentation
Software Engineering Module: Core of Java Topic: Polymorphism TALENTSPRINT | © Copyright 2012

Software Engineering Module: Core of Java Topic: Polymorphism TALENTSPRINT | © Copyright 2012

Polymorphism The content in this presentation is aimed at teaching learners to: • Define

Polymorphism The content in this presentation is aimed at teaching learners to: • Define Polymorphism and Overriding • Explain the need and importance of Polymorphism • Use the advantage of Polymorphism in java application • Understand dynamic method dispatch • Define overloading • Use overloaded methods TALENTSPRINT | © Copyright 2012 2

Polymorphism Exercise 1 Create the following Employee, Manager, Clerk, Sales. Person classes in java.

Polymorphism Exercise 1 Create the following Employee, Manager, Clerk, Sales. Person classes in java. - Employee • Instance variables : name, salary. Basic, HRAPer, DAPer, salary. EPF, salary. PT. • Methods : compute. Salary() - Manager ( a sub class of Employee) • Instance variable : HRAPer , project. Allowance • Methods : compute. Salary() Exercise 1 - Clerk ( a sub class of Employee) • Instance variable : int speed , int accuracy • Methods : compute. Salary() - Sales. Person ( a sub class of Employee) • Instance variable : target, completed • Methods : compute. Salary() • Create appropriate constructors for all the classes. • Create a class with main and add a display. Salary() method which take one parameter of type employee and complete the application by using the compute. Salary() method in display. Salary method(). TALENTSPRINT | © Copyright 2012 3

Polymorphism Is a concept where a single name may denote objects of different classes

Polymorphism Is a concept where a single name may denote objects of different classes that are related by some common base class. [Booch] Is the ability to create an attribute, a method, or an object that has more than one form. TALENTSPRINT | © Copyright 2012 4

Polymorphism A polymorphic reference variable can refer to different types of objects at different

Polymorphism A polymorphic reference variable can refer to different types of objects at different times. In java every reference can be polymorphic except of references to base types and final classes. It is the type of the object being referenced, not the reference type, that determines which method is invoked. Polymorphic references are therefore resolved at run-time, not during compilation; this is called dynamic binding. Careful use of polymorphic references can lead to elegant and robust software designs. TALENTSPRINT | © Copyright 2012 5

Polymorphism Example: Overriding Methods class A { int i, j; A(int a, int b)

Polymorphism Example: Overriding Methods class A { int i, j; A(int a, int b) { i = a; j = b; } void show() { System. out. println("i and j: " + i + " " + j); } } class B extends A { int k; B(int a, int b, int c) { super(a, b); k = c; } void show() { System. out. println(" i, jand k: " +i+ " " + j+" "+k); } } class Main. Class{ public static void main(String args[]){ A oa = new A(10, 20); B ob = new B(10, 20, 30); oa. show(); I and j: 10 20 ob. show(); } I , j and k: 10 20 30 } TALENTSPRINT | © Copyright 2012 6

Polymorphism How does it work? What happened to the show() method of super class

Polymorphism How does it work? What happened to the show() method of super class while calling ob. show(). Can we access the show() method of the super class A from sub class B ? TALENTSPRINT | © Copyright 2012 7

Polymorphism Overriding Program class One{ int x 1=10; public int m 1(int value){ return

Polymorphism Overriding Program class One{ int x 1=10; public int m 1(int value){ return value*value; } } class Two{ int x 2=20; public int m 2(){ return x 2; } } class Main. Class{ pubic static void main(String args[]){ One one = new One(); Two two = new Two(); } } TALENTSPRINT | © Copyright 2012 JVM Memory Stack memory one two Heap memory x 1 10 m 1(int) x 2 20 m 2() 8

Polymorphism Overriding Program class One{ int x 1=10; public int m 1(int value){ return

Polymorphism Overriding Program class One{ int x 1=10; public int m 1(int value){ return value*value; } } class Two extends One{ int x 2=20; public int m 2(){ return x 2; } } class Main. Class{ pubic static void main(String args[]){ One one = new One(); Two two = new Two(); } } TALENTSPRINT | © Copyright 2012 JVM Memory Stack memory one Heap memory x 1 m 1(int) x 1 two 10 10 m 1(int) x 2 20 m 2() 9

Polymorphism Overriding Overridden JVM Memory Program class One{ int x 1=10; public int m

Polymorphism Overriding Overridden JVM Memory Program class One{ int x 1=10; public int m 1(){ return x 1*x 1; } } class Two extends One{ int x 1=20; public int m 2(){ return x 1; } } class Main. Class{ pubic static void main(String args[]){ Two two = new Two(); System. out. println(two. x 1); } } Stack memory Heap memory x 1 two 10 m 1() x 1 20 m 2() Output: 20 TALENTSPRINT | © Copyright 2012 10

Polymorphism Overriding Program class One{ int x 1=10; public int m 1(){ return x

Polymorphism Overriding Program class One{ int x 1=10; public int m 1(){ return x 1*x 1; } } class Two extends One{ int x 1=20; public int m 1(){ return x 1; } } class Main. Class{ pubic static void main(String args[]){ Two two = new Two(); System. out. println(two. x 1); System. out. println(two. m 1()); } } Output: 20 20 TALENTSPRINT | © Copyright 2012 JVM Memory Stack memory Heap memory x 1 two 10 m 1() x 1 20 m 1() 11

Polymorphism Overriding Methods When a method of a sub-class has the same name and

Polymorphism Overriding Methods When a method of a sub-class has the same name and type as a method of the super-class, we say that this method is overridden. Overriding method has the same name, number and type of parameters, and return type as the method it overrides. Overriding method can also return a subtype of the type returned by the overridden method. This is called a covariant return type. TALENTSPRINT | © Copyright 2012 12

Polymorphism Overriding Program class One{ int x 1=10; public int m 1(){ return x

Polymorphism Overriding Program class One{ int x 1=10; public int m 1(){ return x 1*x 1; } } class Two extends One{ int x 1=20; public int m 1(){ return x 1; } } class Main. Class{ pubic static void main(String args[]){ Two two = new Two(); System. out. println(two. x 1); System. out. println(two. m 1()); } Output: } 20 20 TALENTSPRINT | © Copyright 2012 JVM Memory Stack memory Heap memory x 1 two 10 m 1() x 1 20 m 1() 13

Polymorphism Dynamic method dispatch Program class One{ int x 1=10; public int m 1(){

Polymorphism Dynamic method dispatch Program class One{ int x 1=10; public int m 1(){ return x 1; } } class Two extends One{ int x 1=20; public int m 1(){ return x 1; } } class Main. Class{ pubic static void main(String args[]){ One one = new One(); Two two = new Two(); System. out. println(one. m 1()); System. out. println(two. m 1()); Output: one = two; 10 System. out. println(one. m 1()); 20 } TALENTSPRINT | © Copyright 2012 JVM Memory Stack memory Heap memory x 1 two 10 m 1() x 1 20 m 1() x 1 one 10 m 1() 14

Polymorphism Introduction to Overloading The ability to allow different methods or constructors of a

Polymorphism Introduction to Overloading The ability to allow different methods or constructors of a class to share the same name Always remember that overloaded methods have the following properties: • The same method name • Different parameters or different number of parameters • Return types can be different or same A method can be overloaded in the same class or in a subclass. Access modifier CAN be different. Code deals with different argument types rather than forcing the caller to do conversions prior to invoking your method. TALENTSPRINT | © Copyright 2012 15

Polymorphism Overloading a method name Same Method Name Means (does) different things in different

Polymorphism Overloading a method name Same Method Name Means (does) different things in different circumstances TALENTSPRINT | © Copyright 2012 16

Polymorphism Method Overloading class Example { // same name, three different methods int sum

Polymorphism Method Overloading class Example { // same name, three different methods int sum (int a) { return a; } int sum (int a, int b) { return a + b; } int sum (int a, int b, int c) { return a + b + c; } } TALENTSPRINT | © Copyright 2012 17

Polymorphism TALENTSPRINT | © Copyright 2012 18

Polymorphism TALENTSPRINT | © Copyright 2012 18