Difference between revisions of "Java/C3/Polymorphism/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 29: Line 29:
 
* '''Virtual Method Invocation'''
 
* '''Virtual Method Invocation'''
 
* '''Compile-time polymorphism'''
 
* '''Compile-time polymorphism'''
 
 
  
 
|-
 
|-
Line 45: Line 43:
 
* '''JDK 1.7'''  
 
* '''JDK 1.7'''  
 
* '''Eclipse 4.3.1'''  
 
* '''Eclipse 4.3.1'''  
 
 
  
 
|-
 
|-
Line 55: Line 51:
  
  
| style="border:0.05pt solid #000000;padding:0.185cm;"| To follow this tutorial, you must have basic knowledge of,
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| To follow this tutorial, you must have basic knowledge of '''Java and Eclipse IDE.'''
  
* '''Java and Eclipse IDE.'''
 
* You must have knowledge of '''Subclassing '''& '''method overriding and overloading'''
 
  
If not, for relevant tutorials, please visit our website.
+
You must have knowledge of '''Subclassing '''& '''Method overriding and overloading'''
 +
 
 +
If not, for relevant '''Java''' tutorials, please visit our website.
  
 
|-
 
|-
Line 73: Line 69:
  
 
2. Code re-usability  
 
2. Code re-usability  
 
 
 
  
 
|-
 
|-
Line 85: Line 78:
 
|-
 
|-
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Slide 7
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Slide 7
| style="border:0.05pt solid #000000;padding:0.185cm;"| '''Compile-time polymorphism''' is essentially referred as '''method overloading'''.
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| '''Compile-time polymorphism''' is essentially refered as '''Method overloading'''.
  
 
It is also called '''Static Binding'''.  
 
It is also called '''Static Binding'''.  
  
'''Run-time polymorphism''' is essentially referred as '''method overriding'''.
+
 
 +
'''Run-time polymorphism''' is essentially referred as '''Method overriding'''.
  
 
It is also called '''Dynamic Binding.'''
 
It is also called '''Dynamic Binding.'''
Line 95: Line 89:
 
|-
 
|-
 
| style="border:0.05pt solid #000000;padding:0.185cm;"|  
 
| style="border:0.05pt solid #000000;padding:0.185cm;"|  
| style="border:0.05pt solid #000000;padding:0.185cm;"| We have already learnt '''Run-time polymorphism''' i.e. '''method overriding'''.
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| We have already learnt '''Run-time polymorphism''' i.e. '''Method overriding'''.
  
 
|-
 
|-
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| Go to the '''Eclipse IDE '''
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| Go to the '''Eclipse IDE '''
| style="border:0.05pt solid #000000;padding:0.106cm;"| Let us Switch to '''Eclipse IDE.'''
+
| style="border:0.05pt solid #000000;padding:0.106cm;"| Let us switch to '''Eclipse IDE.'''
  
 
I have already created a project named '''MyProject''' in the previous tutorial.
 
I have already created a project named '''MyProject''' in the previous tutorial.
Line 105: Line 99:
 
|-
 
|-
 
| style="border:0.05pt solid #000000;padding:0.106cm;"|  
 
| style="border:0.05pt solid #000000;padding:0.106cm;"|  
| style="border:0.05pt solid #000000;padding:0.106cm;"| Let us take the code files of '''Using final keyword '''tutorial'''.'''
+
| style="border:0.05pt solid #000000;padding:0.106cm;"| Let us take the code files of '''Using final keyword '''tutorial.
  
 
|-
 
|-
Line 118: Line 112:
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| Highlight '''manager '''method '''getDetails()'''
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| Highlight '''manager '''method '''getDetails()'''
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| '''Manager '''class method '''getDetails()''' overrides the '''Employee '''class method '''getDetails().'''
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| '''Manager '''class method '''getDetails()''' overrides the '''Employee '''class method '''getDetails().'''
 +
  
 
We are calling the '''getDetails()''' method by '''Manager '''class object i.e. '''Manager.'''
 
We are calling the '''getDetails()''' method by '''Manager '''class object i.e. '''Manager.'''
  
 +
|-
 +
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.106cm;"| Type '''system.out.println Details of Manager Class'''
  
 
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.106cm;"| In order to print details, type '''system.out.println Details of Manager Class.'''
  
 
|-
 
|-
Line 128: Line 125:
  
 
Highlight '''department'''
 
Highlight '''department'''
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.106cm;"| In order to print details '''system.out.println''' type '''Details of Manager Class.'''
+
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.106cm;"|'''Save '''and run the program.
 
+
'''Save '''and run the program.
+
  
 
So we can see '''department''' variable value in the output.
 
So we can see '''department''' variable value in the output.
  
Therefore subclass method is invoked at runtime.
+
Therefore '''subclass''' method is invoked at '''runtime'''.
  
 
|-
 
|-
Line 140: Line 135:
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Method invocation is determined by the '''JVM,''' not compiler.
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Method invocation is determined by the '''JVM,''' not compiler.
  
Therefore it is known as '''Runtime''' polymorphism or '''method overriding.'''
+
 
 +
Therefore it is known as '''Runtime polymorphism''' or '''method overriding.'''
  
 
|-
 
|-
Line 152: Line 148:
 
|-
 
|-
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Come to '''Eclipse IDE'''
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Come to '''Eclipse IDE'''
| style="border:0.05pt solid #000000;padding:0.185cm;"| Come to '''Employee '''class.
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| Come to '''Employee '''class in '''Eclipse IDE'''.
  
 
|-
 
|-
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Remove the '''static '''and '''final''' keyword for variable '''name.'''
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Remove the '''static '''and '''final''' keyword for variable '''name.'''
| style="border:0.05pt solid #000000;padding:0.185cm;"| Remove the '''static '''and '''final''' keyword for variable '''name.'''
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| Remove the '''static '''and '''final''' keywords for variable '''name.'''
 
+
 
+
 
+
  
 
|-
 
|-
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Uncomment '''setName '''method  
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Uncomment '''setName '''method  
| style="border:0.05pt solid #000000;padding:0.185cm;"| Uncomment '''setName '''method.
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| Uncomment the method '''setName'''.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.185cm;"| Remove the '''static '''block.
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.185cm;"| Remove the '''static '''block.
 +
  
 
'''Save''' the file
 
'''Save''' the file
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.185cm;"| Remove the '''static '''block.
 
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.185cm;"| Remove the '''static '''block.
  
'''Save''' the file
+
 
 +
'''Save''' the file.
  
 
|-
 
|-
Line 180: Line 175:
  
  
| style="border:0.05pt solid #000000;padding:0.185cm;"| Come to '''TestEmployee '''class in IDE.
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| Come to '''TestEmployee '''class.
  
 
Uncomment the value instance,
 
Uncomment the value instance,
Line 186: Line 181:
 
'''manager.setName(“Nikkita Dinesh”);'''
 
'''manager.setName(“Nikkita Dinesh”);'''
  
We uncommented this instance as we have uncommented the method '''setName() '''in '''Empolyee''' class
+
We uncommented this instance, as we have uncommented the method '''setName() '''in '''Empolyee''' class.
  
 
|-
 
|-
Line 192: Line 187:
  
 
'''Employee emp1 = new Employee();'''
 
'''Employee emp1 = new Employee();'''
| style="border:0.05pt solid #000000;padding:0.185cm;"| Now, lets instantiate '''Employee''' object emp1 for '''Employee '''class reference.
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| Now, let's instantiate '''Employee''' object '''emp1''' for '''Employee '''class reference.
  
 
Type,
 
Type,
  
'''Employee emp1 = new Employee();'''
+
'''Employee emp1 = new Employee open and close parenthesis semicolon'''
  
 
|-
 
|-
Line 204: Line 199:
  
 
'''emp1.setEmail("jayesh@gmail.com");'''
 
'''emp1.setEmail("jayesh@gmail.com");'''
| style="border:0.05pt solid #000000;padding:0.185cm;"| Lets initialize the value for '''setEmail '''and '''setName''' for '''Employee '''class.
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| Let's initialize the value for '''setEmail '''and '''setName''' for '''Employee '''class.
  
 
Type,
 
Type,
Line 218: Line 213:
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| In order to print the employee details type,
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| In order to print the employee details type,
  
'''System.out.println("Details of Employee class:" + "\n" + emp1.getDetails());'''
+
'''System.out.println("Details of Employee class:" + "\n" + emp1.getDetails()) semicolon'''
  
 
|-
 
|-
Line 224: Line 219:
  
 
'''Employee emp2 = new Manager();'''
 
'''Employee emp2 = new Manager();'''
| style="border:0.05pt solid #000000;padding:0.185cm;"| Let us instantiate '''Manager '''object '''emp2 '''for '''Employee '''class reference i.e.type
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| Let us instantiate '''Manager '''object '''emp2 '''for '''Employee '''class reference i.e.
  
'''Employee emp2 = new Manager();'''
+
type
  
We are able to do this because''','''
+
'''Employee emp2 = new Manager open and close parenthesis semicolon'''
  
 
|-
 
|-
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| '''Slide 9'''
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| '''Slide 9'''
| style="border:0.05pt solid #000000;padding:0.185cm;"| Any '''Java''' object that pass more than one '''IS-A '''test is polymorphic.
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| We are able to do this because any '''Java''' object that pass more than one '''IS-A '''test, is polymorphic.
  
In '''Java''', all objects are polymorphic since any object will pass the '''IS-A '''test for their own type and for the class Object.
+
 
 +
In '''Java''', all objects are polymorphic, since any object will pass the '''IS-A '''test for their own type and for the class Object.
  
 
* A Manager '''IS-A''' Employee
 
* A Manager '''IS-A''' Employee
 
* A Manager '''IS-A''' Manager
 
* A Manager '''IS-A''' Manager
 
* A Manager '''IS-A''' Object
 
* A Manager '''IS-A''' Object
 
 
  
 
|-
 
|-
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Highlight '''manager, emp1 '''and '''emp2'''
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Highlight '''manager, emp1 '''and '''emp2'''
| style="border:0.05pt solid #000000;padding:0.185cm;"| Only possible way to access an object is through a reference variable.  
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| Only possible way to access an object is through a '''reference variable'''.  
  
Reference variables like '''emp1, emp2 '''and '''manager.'''
+
'''Reference variables''' like '''emp1, emp2 '''and '''manager.'''
  
Here, we instantiated two '''Manager''' objects:
 
  
One which references '''Employee '''class.
+
Here, we instantiated two '''Manager''' objects:
  
Other references '''Manager '''class.
+
*One which references '''Employee '''class.
 +
*And other which references '''Manager '''class.
  
 
|-
 
|-
Line 264: Line 258:
  
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Let’s initialize the values for '''setEmail, setName''' and '''setDepartment '''using '''emp2''' object.
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Let’s initialize the values for '''setEmail, setName''' and '''setDepartment '''using '''emp2''' object.
 +
  
 
Type,
 
Type,
Line 269: Line 264:
 
'''emp2.setName("Ankita");'''
 
'''emp2.setName("Ankita");'''
  
'''emp2.setEmail(“xyz[mailto:ankita@gmail.com @gmail.com]”''');'''
+
'''emp2.setEmail(“xyz@gmail.com”);'''
  
 
'''emp2.setDepartment(“IT”);'''
 
'''emp2.setDepartment(“IT”);'''
Line 285: Line 280:
  
 
'''The method setDepartment(String) is undefined for the type Employee'''
 
'''The method setDepartment(String) is undefined for the type Employee'''
 
 
 
  
 
|-
 
|-
| style="border:0.05pt solid #000000;padding:0.106cm;"|  
+
| style="border:0.05pt solid #000000;padding:0.106cm;"| Switch to TestEmployee
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| This is because, '''setDepartment '''method does not exist for '''Employee '''class.
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| This is because, '''setDepartment '''method does not exist for '''Employee '''class.
  
Line 296: Line 288:
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| Remove the line,
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| Remove the line,
  
'''emp.setDepartment("IT");'''
+
'''emp2.setDepartment("IT");'''
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| So, remove the line,
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| So, remove the line,
  
'''emp.setDepartment("IT");'''
+
'''emp2.setDepartment("IT");'''
  
 
|-
 
|-
Line 305: Line 297:
  
 
'''System.out.println("Details of Manager class:" + "\n" + emp.getDetails());'''
 
'''System.out.println("Details of Manager class:" + "\n" + emp.getDetails());'''
| style="border:0.05pt solid #000000;padding:0.185cm;"| In order to print the employee details type,
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| In order to print the details, type,
  
'''System.out.println("Details of Manager class:" + "\n" + emp2.getDetails());'''
+
'''System.out.println("Details of Manager class:" + "\n" + emp2.getDetails()) semicolon'''
  
 
|-
 
|-
Line 314: Line 306:
  
 
|-
 
|-
| style="border:0.05pt solid #000000;padding:0.106cm;"|  
+
| style="border:0.05pt solid #000000;padding:0.106cm;"| Highlight the output.
| style="border:0.05pt solid #000000;padding:0.106cm;"| Here in the output we get the '''Manager of: '''as blank.
+
  
This is because we have not initialized department in '''Manager '''class using '''emp2''' object .
+
 
 +
Switch to TestEmployee
 +
 
 +
 
 +
| style="border:0.05pt solid #000000;padding:0.106cm;"| Here in the output.
 +
 
 +
We get the '''Manager of: '''as blank.
 +
 
 +
This is because we have not initialized '''department''' in '''Manager '''class using '''emp2'''.
  
 
|-
 
|-
Line 323: Line 322:
  
 
'''public String department="IT";'''
 
'''public String department="IT";'''
| style="border:0.05pt solid #000000;padding:0.185cm;"| For demo purpose let the default department be IT.
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| For demo purpose, let the default '''department''' be '''IT'''.
  
 
So, go to '''Manager '''class and initialize the value for '''department.'''
 
So, go to '''Manager '''class and initialize the value for '''department.'''
 
 
 
  
 
|-
 
|-
Line 358: Line 354:
  
 
'''Manager of: Accounts'''
 
'''Manager of: Accounts'''
 +
 +
| style="border:0.05pt solid #000000;padding:0.185cm;"| We get the output:
  
  
  
| style="border:0.05pt solid #000000;padding:0.185cm;"| We get the output,
 
  
 
'''Employee''' object referring '''Employee''' class,
 
'''Employee''' object referring '''Employee''' class,
Line 371: Line 368:
 
|-
 
|-
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Highlight '''emp2'''
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| Highlight '''emp2'''
| style="border:0.05pt solid #000000;padding:0.185cm;"| Here we see that the '''getDetails() '''method of Manager class is called by '''emp2'''.  
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| Here we see that the '''getDetails() '''method of '''Manager''' class is called by '''emp2'''.  
 +
 
  
 
But when '''emp2''' tried calling '''setDepartment''', we got an error.
 
But when '''emp2''' tried calling '''setDepartment''', we got an error.
 
The reason for that is as follows:
 
  
 
|-
 
|-
 
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #20124d;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.185cm;"| Highlight '''getDetails()'''
 
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #20124d;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.185cm;"| Highlight '''getDetails()'''
| style="border:0.05pt solid #000000;padding:0.185cm;"| The compiler sees the '''getDetails()''' method in the '''Employee''' class during '''emp2.getDetails()'''.
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| The reason for that is as follows:
 +
 
 +
The compiler sees the '''getDetails()''' method in the '''Employee''' class during '''emp2.getDetails()'''.
  
 
So, it does not throw an error and validates the code.
 
So, it does not throw an error and validates the code.
  
At run time, however, the '''JVM''' invokes '''''getDetails()''''' in the '''Manager''' class.
+
At run time, however, the '''JVM''' invokes '''getDetails()''' in the '''Manager''' class.
  
 
As '''getDetails() '''of '''Manager '''class overrides '''getDetails() '''of '''Employee '''class.
 
As '''getDetails() '''of '''Manager '''class overrides '''getDetails() '''of '''Employee '''class.
  
So we get the output as per '''getDetails() '''of '''Manager '''class.
+
So, we get the output as per '''getDetails() '''of '''Manager '''class.
  
But the compiler does not see the '''setDepartment''' method in the '''Employee '''class.
 
 
Therefore, it raises an error in case of '''setDepartment''' call by emp2.
 
  
  
 +
But the compiler does not see the '''setDepartment''' method in the '''Employee '''class.
  
 +
Therefore, it raises an error in case of '''setDepartment''' call by '''emp2'''.
  
 
|-
 
|-
Line 404: Line 401:
 
The compiler references '''Employee''' class for '''getDetails()''' during '''emp1.getDetails().'''
 
The compiler references '''Employee''' class for '''getDetails()''' during '''emp1.getDetails().'''
  
At run time, the '''JVM''' invokes '''''getDetails()''''' in the '''Employee''' class.
+
 
 +
At run time, the '''JVM''' invokes '''getDetails()''' in the '''Employee''' class.
  
 
So we get the output as per '''getDetails() '''of '''Employee '''class.
 
So we get the output as per '''getDetails() '''of '''Employee '''class.
  
Thus JVM calls the appropriate method for the object that is referred to in each variable
+
|-
 +
| style="border:0.05pt solid #20124d;padding:0.185cm;"| Previous screen
 +
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #20124d;border-right:0.05pt solid #000000;padding:0.185cm;"|Thus '''JVM''' calls the appropriate method for the object that is referred to in each variable.
 +
 
  
 
This behavior is referred to as '''Virtual Method Invocation.'''
 
This behavior is referred to as '''Virtual Method Invocation.'''
 +
  
 
The methods are referred to as '''Virtual Methods'''.
 
The methods are referred to as '''Virtual Methods'''.
Line 416: Line 418:
 
|-
 
|-
 
| style="border:0.05pt solid #20124d;padding:0.185cm;"|  
 
| style="border:0.05pt solid #20124d;padding:0.185cm;"|  
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #20124d;border-right:0.05pt solid #000000;padding:0.185cm;"| All methods in Java behave in this manner.
+
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #20124d;border-right:0.05pt solid #000000;padding:0.185cm;"| All methods in '''Java''' behave in this manner.
  
 
|-
 
|-
Line 425: Line 427:
 
| style="border-top:0.05pt solid #20124d;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.185cm;"| Slide 10
 
| style="border-top:0.05pt solid #20124d;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.185cm;"| Slide 10
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| We have already learnt '''Compile-time polymorphism '''i.e. method overloading.
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| We have already learnt '''Compile-time polymorphism '''i.e. method overloading.
 +
  
 
Let us quickly know '''Compile time polymorphism '''in brief.
 
Let us quickly know '''Compile time polymorphism '''in brief.
  
'''Compile time polymorphism''' is nothing but '''method overloading''' in java.
+
*In '''Compile time polymorphism''', class can have more than one method.
 
+
*The methods have same name but with different number of arguments.
Class can have more than one methods.
+
*Compiler is able to figure out the method call at compile-time.
 
+
*That’s the reason it is known as '''compile time polymorphism.'''
The methods have same name but with different number of arguments.
+
 
+
Compiler is able to figure out the method call at compile-time.
+
 
+
That’s the reason it is known as '''compile time polymorphism.'''
+
  
 
|-
 
|-
 
| style="border:0.05pt solid #000000;padding:0.185cm;"|  
 
| style="border:0.05pt solid #000000;padding:0.185cm;"|  
| style="border:0.05pt solid #000000;padding:0.185cm;"| So let us summarize.
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| So, let us summarize.
  
 
|-
 
|-
Line 454: Line 452:
 
* What is '''Polymorphism''' in Java?
 
* What is '''Polymorphism''' in Java?
 
* '''Run-time polymorphism'''
 
* '''Run-time polymorphism'''
 +
* '''Virtual Method Invocation'''
 
* '''Compile-time polymorphism'''
 
* '''Compile-time polymorphism'''
* '''Virtual Method Invocation'''
 
 
 
  
 
|-
 
|-
Line 494: Line 490:
  
  
| style="border:0.05pt solid #000000;padding:0.106cm;"| Spoken Tutorial Project is supported by the NMEICT, MHRD, Government of India.
+
| style="border:0.05pt solid #000000;padding:0.106cm;"| Spoken Tutorial Project is supported by NMEICT, MHRD, Government of India.
  
 
More information on this Mission is available at this link.
 
More information on this Mission is available at this link.

Latest revision as of 11:07, 29 April 2015

Title of script: Polymorphism in Java

Author: Trupti Rajesh Kini

Keywords: Polymorphism, virtual method, run-time polymorphism, Compile-time polymorphism, static binding, dynamic binding, video tutorial


Visual Cue Narration
Slide 1


Welcome to the spoken-tutorial on Polymorphism in Java.
Slide 2


In this tutorial we will learn :
  • About Polymorphism in Java
  • Run-time polymorphism
  • Virtual Method Invocation
  • Compile-time polymorphism
Slide 3

Software Requirements


Here we are using


  • Ubuntu Linux Version 12.04
  • JDK 1.7
  • Eclipse 4.3.1
Slide 4

Prerequisites


To follow this tutorial, you must have basic knowledge of Java and Eclipse IDE.


You must have knowledge of Subclassing & Method overriding and overloading

If not, for relevant Java tutorials, please visit our website.

Slide 5 and slide 6

What is polymorphism?

Polymorphism is an ability of an object to take on many forms.

The major advantages of Polymorphism are:

1. Reduction of complexity &

2. Code re-usability

Slide 7 In Java, there are two types of polymorphism:

Compile-time and Run-time polymorphism.

Slide 7 Compile-time polymorphism is essentially refered as Method overloading.

It is also called Static Binding.


Run-time polymorphism is essentially referred as Method overriding.

It is also called Dynamic Binding.

We have already learnt Run-time polymorphism i.e. Method overriding.
Go to the Eclipse IDE Let us switch to Eclipse IDE.

I have already created a project named MyProject in the previous tutorial.

Let us take the code files of Using final keyword tutorial.
Employee class is the parent class.

Manager class is the subclass.

Manager class contains an additional variable department.

Highlight manager method getDetails() Manager class method getDetails() overrides the Employee class method getDetails().


We are calling the getDetails() method by Manager class object i.e. Manager.

Type system.out.println Details of Manager Class In order to print details, type system.out.println Details of Manager Class.
Save and run the program

Highlight department

Save and run the program.

So we can see department variable value in the output.

Therefore subclass method is invoked at runtime.

Slide 8 Method invocation is determined by the JVM, not compiler.


Therefore it is known as Runtime polymorphism or method overriding.

We learnt what is Run time polymorphism.
Now let us learn Virtual Method Invocation.
Come to Eclipse IDE Come to Employee class in Eclipse IDE.
Remove the static and final keyword for variable name. Remove the static and final keywords for variable name.
Uncomment setName method Uncomment the method setName.
Remove the static block.


Save the file

Remove the static block.


Save the file.

Come to TestEmployee class

Uncomment the line manager.setName(“Nikkita Dinesh”);


Come to TestEmployee class.

Uncomment the value instance,

manager.setName(“Nikkita Dinesh”);

We uncommented this instance, as we have uncommented the method setName() in Empolyee class.

Type,

Employee emp1 = new Employee();

Now, let's instantiate Employee object emp1 for Employee class reference.

Type,

Employee emp1 = new Employee open and close parenthesis semicolon

Type,

emp1.setName("Jayesh");

emp1.setEmail("jayesh@gmail.com");

Let's initialize the value for setEmail and setName for Employee class.

Type,

emp1.setName("Jayesh");

emp1.setEmail("pqr@gmail.com");

Type,

System.out.println("Details of Employee class:" + "\n" + emp1.getDetails());

In order to print the employee details type,

System.out.println("Details of Employee class:" + "\n" + emp1.getDetails()) semicolon

Type,

Employee emp2 = new Manager();

Let us instantiate Manager object emp2 for Employee class reference i.e.

type

Employee emp2 = new Manager open and close parenthesis semicolon

Slide 9 We are able to do this because any Java object that pass more than one IS-A test, is polymorphic.


In Java, all objects are polymorphic, since any object will pass the IS-A test for their own type and for the class Object.

  • A Manager IS-A Employee
  • A Manager IS-A Manager
  • A Manager IS-A Object
Highlight manager, emp1 and emp2 Only possible way to access an object is through a reference variable.

Reference variables like emp1, emp2 and manager.


Here, we instantiated two Manager objects:

  • One which references Employee class.
  • And other which references Manager class.
Type,

emp.setName("Ankita");

emp.setEmail("ankita@gmail.com");


Let’s initialize the values for setEmail, setName and setDepartment using emp2 object.


Type,

emp2.setName("Ankita");

emp2.setEmail(“xyz@gmail.com”);

emp2.setDepartment(“IT”);

Point to the red cross.

Highlight error,

The method setDepartment(String) is undefined for the type Employee


We see that there is an error,

The method setDepartment(String) is undefined for the type Employee

Switch to TestEmployee This is because, setDepartment method does not exist for Employee class.
Remove the line,

emp2.setDepartment("IT");

So, remove the line,

emp2.setDepartment("IT");

Type,

System.out.println("Details of Manager class:" + "\n" + emp.getDetails());

In order to print the details, type,

System.out.println("Details of Manager class:" + "\n" + emp2.getDetails()) semicolon

Save and Run the program. Save and Run the program.
Highlight the output.


Switch to TestEmployee


Here in the output.

We get the Manager of: as blank.

This is because we have not initialized department in Manager class using emp2.

Type,

public String department="IT";

For demo purpose, let the default department be IT.

So, go to Manager class and initialize the value for department.

Save and run the program. Save and run the program.
Highlight output,

Details of Manager class :

Name: Ankita

Email: ankita@gmail.com

Manager of:

Details of Manager class:

Name: Jayesh

Email: jayesh@gmail.com

Details of Manager class:

Name: Nikkita Dinesh

Email: abc@gmail.com

Manager of: Accounts

We get the output:



Employee object referring Employee class,

Manager object referring Employee class &

Manager object referring Manager class,

Highlight emp2 Here we see that the getDetails() method of Manager class is called by emp2.


But when emp2 tried calling setDepartment, we got an error.

Highlight getDetails() The reason for that is as follows:

The compiler sees the getDetails() method in the Employee class during emp2.getDetails().

So, it does not throw an error and validates the code.

At run time, however, the JVM invokes getDetails() in the Manager class.

As getDetails() of Manager class overrides getDetails() of Employee class.

So, we get the output as per getDetails() of Manager class.


But the compiler does not see the setDepartment method in the Employee class.

Therefore, it raises an error in case of setDepartment call by emp2.

Highlight

System.out.println("Details of Employee class:" + "\n" + emp1.getDetails());

Here, Employee method getDetails() is invoked for Employee class.

The compiler references Employee class for getDetails() during emp1.getDetails().


At run time, the JVM invokes getDetails() in the Employee class.

So we get the output as per getDetails() of Employee class.

Previous screen Thus JVM calls the appropriate method for the object that is referred to in each variable.


This behavior is referred to as Virtual Method Invocation.


The methods are referred to as Virtual Methods.

All methods in Java behave in this manner.
We successfully learnt what is Virtual Method Invocation.
Slide 10 We have already learnt Compile-time polymorphism i.e. method overloading.


Let us quickly know Compile time polymorphism in brief.

  • In Compile time polymorphism, class can have more than one method.
  • The methods have same name but with different number of arguments.
  • Compiler is able to figure out the method call at compile-time.
  • That’s the reason it is known as compile time polymorphism.
So, let us summarize.
Slide 11

Summary


In this tutorial we learnt:


  • What is Polymorphism in Java?
  • Run-time polymorphism
  • Virtual Method Invocation
  • Compile-time polymorphism
Slide 12

Assignment

As an assignment,

Override methods for Vehicle and Bike class which we used in previous tutorials.

Slide 13

About Spoken Tutorial Project


The video available at the following link summarizes the Spoken Tutorial project. Please watch it.
Slide 14

About Spoken Tutorial workshops


The Spoken Tutorial Project Team conducts workshops and gives certificates to those who pass an online test.

For more details, please write to us.

Slide 15

Acknowledgement


Spoken Tutorial Project is supported by NMEICT, MHRD, Government of India.

More information on this Mission is available at this link.

This is Trupti Kini from IIT Bombay signing off. Thank you for joining.

Contributors and Content Editors

Nancyvarkey, Trupti