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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of script''':''' Polymorphism in Java''' '''Author: Trupti Rajesh Kini''' '''Keywords: Polymorphism, virtual method, run-time polymorphism, Compile-time polymorphis...")
 
Line 58: Line 58:
  
 
* '''Java and Eclipse IDE.'''  
 
* '''Java and Eclipse IDE.'''  
* '''Subclassing '''& '''method overriding and overloading'''
+
* You must have knowledge of '''Subclassing '''& '''method overriding and overloading'''
  
 
If not, for relevant tutorials, please visit our website.
 
If not, for relevant tutorials, please visit our website.
Line 84: Line 84:
  
 
|-
 
|-
| style="border:0.05pt solid #000000;padding:0.185cm;"|  
+
| 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 referred as '''method overloading'''.
  
Line 113: Line 113:
 
'''Manager '''class is the subclass.
 
'''Manager '''class is the subclass.
  
'''Manager''' class contains an additional variable department.
+
'''Manager''' class contains an additional variable '''department.'''
 
+
|-
+
| style="border:0.05pt solid #000000;padding:0.106cm;"|
+
| style="border:0.05pt solid #000000;padding:0.106cm;"|
+
  
 
|-
 
|-
Line 125: Line 121:
 
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.'''
  
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
 +
 
 +
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.'''
  
 
'''Save '''and run the program.
 
'''Save '''and run the program.
Line 148: Line 151:
  
 
|-
 
|-
| style="border:0.05pt solid #000000;padding:0.185cm;"|  
+
| 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.
  
 
|-
 
|-
 
| 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.'''
 
Remove the '''static '''block.
 
 
| 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.'''
  
Remove the '''static '''block.
+
 
 +
 
  
 
|-
 
|-
 
| 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 '''setName '''method.
 +
 +
|-
 +
| 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
 +
| 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
  
 
|-
 
|-
Line 176: Line 186:
 
'''manager.setName(“Nikkita Dinesh”);'''
 
'''manager.setName(“Nikkita Dinesh”);'''
  
We uncommented this instance as we have uncommented the method '''setName().'''
+
We uncommented this instance as we have uncommented the method '''setName() '''in '''Empolyee''' class
  
 
|-
 
|-
Line 214: Line 224:
  
 
'''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.
+
| style="border:0.05pt solid #000000;padding:0.185cm;"| Let us instantiate '''Manager '''object '''emp2 '''for '''Employee '''class reference i.e.type
  
 
'''Employee emp2 = new Manager();'''
 
'''Employee emp2 = new Manager();'''
Line 253: Line 263:
  
  
| style="border:0.05pt solid #000000;padding:0.185cm;"| Let’s initialize the values for '''setEmail, setName''' and '''setDepartment '''using '''emp''' 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,
  
'''emp.setName("Ankita");'''
+
'''emp2.setName("Ankita");'''
  
'''emp.setEmail(“xyz[mailto:ankita@gmail.com @gmail.com]”''');'''
+
'''emp2.setEmail(“xyz[mailto:ankita@gmail.com @gmail.com]”''');'''
  
'''emp.setDepartment(“IT”);'''
+
'''emp2.setDepartment(“IT”);'''
  
 
|-
 
|-
Line 287: Line 297:
  
 
'''emp.setDepartment("IT");'''
 
'''emp.setDepartment("IT");'''
| style="border:0.05pt solid #000000;padding:0.106cm;"| Remove the line,
+
| style="border:0.05pt solid #000000;padding:0.106cm;"| So, remove the line,
  
 
'''emp.setDepartment("IT");'''
 
'''emp.setDepartment("IT");'''
Line 297: Line 307:
 
| 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 Manager class:" + "\n" + emp.getDetails());'''
+
'''System.out.println("Details of Manager class:" + "\n" + emp2.getDetails());'''
  
 
|-
 
|-
 
| 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.106cm;"| '''Save '''and '''Run '''the program.
 
| 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.106cm;"| '''Save '''and '''Run '''the program.
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| '''Save '''and '''Run '''the program.
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| '''Save '''and '''Run '''the program.
 
|-
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| 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'''
 
 
 
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| 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'''
 
  
 
|-
 
|-
Line 354: Line 317:
 
| style="border:0.05pt solid #000000;padding:0.106cm;"| Here in the output we get the '''Manager of: '''as blank.
 
| 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 .
+
This is because we have not initialized department in '''Manager '''class using '''emp2''' object .
  
 
|-
 
|-
Line 400: Line 363:
 
| 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,
  
Manager object referring Employee class &
+
'''Manager''' object referring '''Employee''' class &
  
Manager object referring Manager class,
+
'''Manager '''object referring '''Manager '''class,
  
 
|-
 
|-
| style="border:0.05pt solid #000000;padding:0.185cm;"|  
+
| 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'''.  
  
Line 415: Line 378:
  
 
|-
 
|-
| 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;"|  
+
| 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 compiler sees the '''getDetails()''' method in the '''Employee''' class during '''emp2.getDetails()'''.
  
Line 454: Line 417:
 
| 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.
 
|-
 
| 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;"|
 
| style="border:0.05pt solid #000000;padding:0.185cm;"| *
 
 
 
  
 
|-
 
|-

Revision as of 15:04, 30 March 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 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 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.



Save and run the program

Highlight department

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



Uncomment setName method Uncomment setName method.
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 in IDE.

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

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



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

emp.setDepartment("IT");

So, 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" + emp2.getDetails());

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

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.

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().

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