Java/C3/Polymorphism/English

From Script | Spoken-Tutorial
Jump to: navigation, search

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