Java/C3/Polymorphism/English
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 :
|
Slide 3
Software Requirements
|
Here we are using
|
Slide 4
Prerequisites
|
To follow this tutorial, you must have basic knowledge of Java and Eclipse IDE.
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.
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().
|
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.
|
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.
|
Remove the static block.
|
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.
|
Highlight manager, emp1 and emp2 | Only possible way to access an object is through a reference variable.
Reference variables like emp1, emp2 and manager.
|
Type,
emp.setName("Ankita"); emp.setEmail("ankita@gmail.com");
|
Let’s initialize the values for setEmail, setName and setDepartment using emp2 object.
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 |
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. |
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.
The reason for that is as follows: |
Highlight getDetails() | 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().
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. 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.
|
So, let us summarize. | |
Slide 11
Summary
|
In this tutorial we learnt:
|
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. |