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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 6: Line 6:
 
|-
 
|-
 
| 00:01
 
| 00:01
| Welcome to the '''spoken-tutorial '''on '''Polymorphism in Java.'''  
+
| Welcome to the '''spoken-tutorial '''on '''Polymorphism''' in Java.
  
 
|-
 
|-
 
| 00:06
 
| 00:06
| In this tutorial we will learn :  
+
| In this tutorial, we will learn about:
About '''Polymorphism''' in Java
+
'''Run-time polymorphism'''
+
* '''Polymorphism''' in Java
'''Virtual Method Invocation''' and
+
 
'''Compile-time polymorphism'''
+
* '''Run-time polymorphism'''
 +
 
 +
* '''Virtual Method Invocation''' and
 +
 
 +
* '''Compile-time polymorphism'''.
  
 
|-
 
|-
 
| 00:19
 
| 00:19
| Here we are using  
+
| Here we are using:
'''Ubuntu Linux Version 12.04'''
+
'''JDK Version 1.7''' and
+
* '''Ubuntu Linux Version 12.04'''
'''Eclipse 4.3.1'''  
+
 
 +
* '''JDK Version 1.7''' and
 +
 
 +
* '''Eclipse 4.3.1'''  
  
 
|-
 
|-
 
| 00:31
 
| 00:31
| To follow this tutorial, you must have basic knowledge of '''Java and Eclipse IDE.'''  
+
| To follow this tutorial, you must have basic knowledge of '''Java''' and '''Eclipse IDE'''.
  
 
|-
 
|-
 
| 00:37
 
| 00:37
| You must have knowledge of '''Subclassing '''& '''Method overriding and overloading'''
+
| You must have knowledge of '''Subclassing '''& '''Method overriding''' and '''overloading'''.
  
 
|-
 
|-
Line 37: Line 44:
 
|-
 
|-
 
| 00:48
 
| 00:48
| '''Polymorphism''' is an ability of an object to take on many forms.
+
| '''Polymorphism''' is an ability of an '''object''' to take on many forms.
  
 
|-
 
|-
Line 45: Line 52:
 
1. Reduction of complexity &
 
1. Reduction of complexity &
  
2. Code re-usability  
+
2. Code re-usability.
  
 
|-
 
|-
Line 53: Line 60:
 
|-
 
|-
 
| 01:11
 
| 01:11
| '''Compile-time polymorphism''' is essentially refered as '''Method overloading'''.  It is also called '''Static Binding'''.  
+
| '''Compile-time polymorphism''' is essentially referred as '''Method overloading'''.  It is also called '''Static Binding'''.  
  
 
|-
 
|-
Line 73: Line 80:
 
|-
 
|-
 
| 01:49
 
| 01:49
| '''Employee '''class is the parent class.
+
| '''Employee '''class is the '''parent class'''.
  
 
|-
 
|-
 
| 01:52
 
| 01:52
|  '''Manager '''class is the subclass.
+
|  '''Manager '''class is the '''subclass'''.
  
 
|-
 
|-
Line 85: Line 92:
 
|-
 
|-
 
| 02:01
 
| 02:01
| '''Manager '''class method '''getDetails()''' overrides the '''Employee '''class method '''getDetails().'''
+
| '''Manager '''class method '''getDetails()''' '''override'''s the '''Employee '''class method '''getDetails().'''
  
 
|-
 
|-
Line 93: Line 100:
 
|-
 
|-
 
|  02:16
 
|  02:16
|  In order to print the details, type '''system.out.println Details of Manager Class.'''
+
|  In order to print the details, type: '''system.out.println Details of Manager Class.'''
  
 
|-
 
|-
 
| 02:28
 
| 02:28
| '''Save '''and run the program. So we can see '''department''' variable value in the output.
+
| '''Save '''and '''run''' the program. So, we can see '''department''' variable value in the output.
  
 
|-
 
|-

Revision as of 23:28, 17 May 2017

Time Narration
00:01 Welcome to the spoken-tutorial on Polymorphism in Java.
00:06 In this tutorial, we will learn about:
  • Polymorphism in Java
  • Run-time polymorphism
  • Virtual Method Invocation and
  • Compile-time polymorphism.
00:19 Here we are using:
  • Ubuntu Linux Version 12.04
  • JDK Version 1.7 and
  • Eclipse 4.3.1
00:31 To follow this tutorial, you must have basic knowledge of Java and Eclipse IDE.
00:37 You must have knowledge of Subclassing & Method overriding and overloading.
00:43 If not, for relevant Java tutorials, please visit our website.
00:48 Polymorphism is an ability of an object to take on many forms.
00:54 The major advantages of Polymorphism are:

1. Reduction of complexity &

2. Code re-usability.

01:03 In Java, there are two types of polymorphism: Compile-time and Run-time polymorphism.
01:11 Compile-time polymorphism is essentially referred as Method overloading. It is also called Static Binding.
01:20 Run-time polymorphism is essentially referred as Method overriding. It is also called Dynamic Binding.
01:29 We have already learnt Run-time polymorphism i.e. Method overriding.
01:35 Let us switch to Eclipse IDE. I have already created a project named MyProject in the previous tutorial.
01:44 Let us take the code files of Using final keyword tutorial.
01:49 Employee class is the parent class.
01:52 Manager class is the subclass.
01:55 Manager class contains an additional variable department.
02:01 Manager class method getDetails() overrides the Employee class method getDetails().
02:08 We are calling the getDetails() method by Manager class object i.e. Manager.
02:16 In order to print the details, type: system.out.println Details of Manager Class.
02:28 Save and run the program. So, we can see department variable value in the output.
02:37 Therefore subclass method is invoked at runtime.
02:42 Method invocation is determined by the JVM, not compiler.
02:48 Therefore it is known as Runtime polymorphism or method overriding.
02:55 We learnt what is Run time polymorphism.
02:58 Now let us learn Virtual Method Invocation.
03:03 Come to Employee class in Eclipse IDE.
03:07 Remove the static and final keywords for variable name.
03:13 Uncomment the method setName.
03:16 Remove the static block. Save the file.
03:21 Come to TestEmployee class. Uncomment the value instance, manager.setName(“Nikkita Dinesh”);
03:31 We uncommented this instance, as we have uncommented the method setName() in Empolyee class.
03:38 Now, let's instantiate Employee object emp1 for Employee class reference.
03:46 Type, Employee emp1 = new Employee open and close parenthesis semicolon
03:57 Let's initialize the value for setEmail and setName for Employee class.
04:03 Type, emp1.setName("Jayesh"); emp1.setEmail("pqr@gmail.com");
04:16 In order to print the employee details type, System.out.println("Details of Employee class:" emp1.getDetails()) semicolon
04:37 Let us instantiate Manager object emp2 for Employee class reference i.e.

type Employee emp2 = new Manager open and close parenthesis semicolon

04:54 We are able to do this because any Java object that pass more than one IS-A test, is polymorphic.
05:04 In Java, all objects are polymorphic, since any object will pass the IS-A test for their own type and for the class Object.
05:16 A Manager IS-A Employee

A Manager IS-A Manager A Manager IS-A Object

05:23 Only possible way to access an object is through a reference variable.
05:29 Reference variables like emp1, emp2 and manager.
05:36 Here, we instantiated two Manager objects:

One which references Employee class. And other which references Manager class.

05:47 Let’s initialize the values for setEmail, setName and setDepartment using emp2 object.
05:55 Type,

emp2.setName("Ankita");

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

emp2.setDepartment(“IT”);

06:14 We see that there is an error, The method setDepartment(String) is undefined for the type Employee
06:23 This is because, setDepartment method does not exist for Employee class.
06:30 So, remove the line, emp2.setDepartment("IT");
06:37 In order to print the details, type, System.out.println("Details of Manager class:" emp2.getDetails()) semicolon
06:55 Save and Run the program.
06:58 Here in the output. We get the Manager of: as blank.
07:04 This is because we have not initialized department in Manager class using emp2.
07:12 For demo purpose, let the default department be IT.
07:17 So, go to Manager class and initialize the value for department.
07:25 Save and run the program.
07:28 We get the output: Employee object referring Employee class,
07:34 Manager object referring Employee class & Manager object referring Manager class,
07:42 Here we see that the getDetails() method of Manager class is called by emp2.
07:49 But when emp2 tried calling setDepartment, we got an error.
07:54 The reason for that is as follows:

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

08:05 So, it does not throw an error and validates the code.
08:10 At run time, however, the JVM invokes getDetails() in the Manager class as getDetails() of Manager class overrides getDetails() of Employee class.
08:24 So, we get the output as per getDetails() of Manager class. But the compiler does not see the setDepartment method in the Employee class.
08:36 Therefore, it raises an error in case of setDepartment call by emp2.
08:43 Here, Employee method getDetails() is invoked for Employee class.
08:49 The compiler references Employee class for getDetails() during emp1.getDetails().
08:57 At run time, the JVM invokes getDetails() in the Employee class. So we get the output as per getDetails() of Employee class.
09:08 Thus the JVM calls the appropriate method for the object that is referred to in each variable.
09:16 This behavior is referred to as Virtual Method Invocation.
09:21 The methods are referred to as Virtual Methods.
09:26 All methods in Java behave in this manner.
09:31 We successfully learnt what is Virtual Method Invocation.
09:36 We have already learnt Compile-time polymorphism i.e. method overloading.
09:42 Let us quickly know Compile time polymorphism in brief.
09:47 In Compile time polymorphism, class can have more than one method.
09:53 The methods have same name but with different number of arguments.
09:59 Compiler is able to figure out the method call at compile-time. That’s the reason it is known as compile time polymorphism.
10:09 So, let us summarize.
10:11 In this tutorial we learnt:

What is Polymorphism in Java? Run-time polymorphism Virtual Method Invocation and Compile-time polymorphism

10:23 As an assignment,

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

10:32 The video available at the following link summarizes the Spoken Tutorial project. Please watch it.
10:40 The Spoken Tutorial Project Team conducts workshops and gives certificates to those who pass an online test.For more details, please write to us.
10:51 Spoken Tutorial Project is supported by NMEICT, MHRD, Government of India.More information on this Mission is available at this link.
11:03 Thats it for the tutorial. This is Trupti Kini from IIT Bombay signing off. Thank you for joining.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Sandhya.np14