Java/C3/Polymorphism/English

From Script | Spoken-Tutorial
Revision as of 14:44, 30 March 2015 by Trupti (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.
  • Subclassing & method overriding and overloading

If not, for relevant 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.

Compile-time polymorphism is essentially referred 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.

In order to print details system.out.println type Details of Manager Class.

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 Employee class.
Remove the static and final keyword for variable name.

Remove the static block.

Remove the static and final keyword for variable name.

Remove the static block.

Uncomment setName method Uncomment setName method.
Come to TestEmployee class

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


Come to TestEmployee class in IDE.

Uncomment the value instance,

manager.setName(“Nikkita Dinesh”);

We uncommented this instance as we have uncommented the method setName().

Type,

Employee emp1 = new Employee();

Now, lets instantiate Employee object emp1 for Employee class reference.

Type,

Employee emp1 = new Employee();

Type,

emp1.setName("Jayesh");

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

Lets 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());

Type,

Employee emp2 = new Manager();

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

Employee emp2 = new Manager();

We are able to do this because,

Slide 9 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.

Other references Manager class.

Type,

emp.setName("Ankita");

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


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

Type,

emp.setName("Ankita");

emp.setEmail(“xyz@gmail.com);

emp.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,

emp.setDepartment("IT");

Remove the line,

emp.setDepartment("IT");

Type,

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

In order to print the employee details type,

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

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

Details of Employee class:

Name: Jayesh

Email: pqr@gmail.com

Details of Manager class:

Name: Ankita

Email: xyz@gmail.com

Manager of:

Name: Nikkita Dinesh

Email: abc@gmail.com

Manager of: Accounts


We get the output,

Details of Employee class:

Name: Jayesh

Email: pqr@gmail.com

Details of Manager class:

Name: Ankita

Email: xyz@gmail.com

Manager of:

Name: Nikkita Dinesh

Email: abc@gmail.com

Manager of: Accounts

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 .

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,

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

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

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.

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.

Compile time polymorphism is nothing but method overloading in java.

Class can have more than one methods.

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
  • Compile-time polymorphism
  • Virtual Method Invocation


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 the 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