Difference between revisions of "Java/C3/Subclassing-and-Method-Overriding/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 6: Line 6:
 
|-
 
|-
 
| 00:01
 
| 00:01
| Welcome to the spoken-tutorial on''' Subclassing''' and '''Method overriding.'''
+
| Welcome to the '''spoken-tutorial''' on''' Subclassing''' and '''Method overriding.'''
  
 
|-
 
|-
 
| 00:06
 
| 00:06
| In this tutorial we will learn about :
+
| In this tutorial, we will learn about:
'''subclassing'''
+
* '''subclassing'''
'''extends '''keyword and
+
* '''extends '''keyword and
'''method overriding'''
+
* '''method overriding'''.
  
 
|-
 
|-
 
| 00:15
 
| 00:15
|Here we are using
+
|Here we are using:
'''Ubuntu''' '''Linux '''version 12.04
+
* '''Ubuntu Linux '''version 12.04
'''JDK '''1.7
+
* '''JDK '''1.7
'''Eclipse '''4.3.1
+
* '''Eclipse '''4.3.1
  
 
|-
 
|-
Line 36: Line 36:
 
|-
 
|-
 
| 00:41
 
| 00:41
| It is a way to create a new '''class''' from an existing '''class.'''
+
| It is a way to create a new '''class''' from an existing '''class.'''
  
 
|-
 
|-
Line 48: Line 48:
 
|-
 
|-
 
| 01:00
 
| 01:00
| Now, let me show you how to create a subclass. I have already created a project named '''MyProject'''.
+
| Now, let me show you how to create a '''subclass'''. I have already created a '''project''' named '''MyProject'''.
  
 
|-
 
|-
Line 60: Line 60:
 
|-
 
|-
 
| 01:19
 
| 01:19
| It also has the '''setter '''and '''getter methods '''for the '''class.'''
+
| It also has the '''setter '''and '''getter''' '''method'''s for the '''class.'''
  
 
|-
 
|-
 
| 01:24
 
| 01:24
| It has a '''method getDetails().''' This '''method '''returns  '''name '''and''' email_address.'''
+
| It has a '''method''' "getDetails()". This '''method '''returns  '''"name" '''and''' "email_address".'''
  
 
|-
 
|-
Line 80: Line 80:
 
|-
 
|-
 
| 01:47
 
| 01:47
| '''name '''and''' email_address '''are present in the '''Employee class.''' We can see that its also there in '''Manager class.'''
+
| '''name '''and''' email_address '''are present in the '''Employee class.''' We can see that it is also there in '''Manager class.'''
  
 
|-
 
|-
Line 92: Line 92:
 
|-
 
|-
 
| 02:08
 
| 02:08
| After '''public class Manager, '''type '''extends Employee'''
+
| After '''public class Manager, '''type: '''extends Employee'''.
  
 
|-
 
|-
 
| 02:14
 
| 02:14
| We use the '''extends keyword '''to create a '''subclass '''from an existing '''class.'''
+
| We use the '''extends''' keyword to create a '''subclass '''from an existing '''class.'''
  
 
|-
 
|-
 
| 02:21
 
| 02:21
| Remove the duplicate '''variables '''common to both the '''classes.'''
+
| Remove the duplicate '''variables '''common to both the '''class'''es.
  
 
|-
 
|-
Line 108: Line 108:
 
|-
 
|-
 
| 02:32
 
| 02:32
| Also remove '''setter '''and '''getter methods '''of the same.
+
| Also remove '''setter '''and '''getter''' '''method'''s of the same.
  
 
|-
 
|-
 
| 02:37
 
| 02:37
| In the '''class Manager, '''we will have only one '''variable department.'''
+
| In the '''class 'Manager', '''we will have only one variable '''department.'''
  
 
|-
 
|-
Line 120: Line 120:
 
|-
 
|-
 
| 02:59
 
| 02:59
| In this way, the '''Manager class '''inherits the members of '''Employee class.'''
+
| In this way, the ''''Manager' class '''inherits the members of ''''Employee' class.'''
  
 
|-
 
|-
Line 128: Line 128:
 
|-
 
|-
 
| 03:02
 
| 03:02
| I have also created another '''class '''named '''TestEmployee.'''
+
| I have also created another '''class '''named ''''TestEmployee'.'''
  
 
|-
 
|-
 
| 03:08
 
| 03:08
| Inside the '''main method, '''we will create the '''object '''of  '''Manager class.'''
+
| Inside the '''main''' method, we will create the '''object '''of  '''Manager''' class.
  
 
|-
 
|-
 
| 03:14
 
| 03:14
| So inside the '''main method''', type '''Manager manager equal to new Manager parentheses  
+
| So, inside the '''main method''', type: '''Manager manager''' equal to '''new Manager''' parentheses.
  
 
|-
 
|-
 
| 03:23
 
| 03:23
| Next, we will call the '''setter methods '''of the '''Manager class.'''
+
| Next, we will call the '''setter''' methods of the '''Manager''' class.
  
 
|-
 
|-
 
| 03:28
 
| 03:28
| So type, '''manager dot setName''' within brackets and double quotes '''Nikkita Dinesh '''
+
| So, type, '''manager dot setName''' within brackets and double quotes '''Nikkita Dinesh '''
  
 
|-
 
|-
 
| 03:38
 
| 03:38
| Then type, '''manager dot setEmail '''within brackets and double quotes '''abc at gmail dot com'''  
+
| Then type: '''manager''' dot '''setEmail''' within brackets and double quotes '''abc at gmail dot com'''.
  
 
|-
 
|-
 
| 03:49
 
| 03:49
| Then type, '''manager dot setDepartment''' within brackets and double quotes '''Accounts'''
+
| Then type, '''manager dot setDepartment''' within brackets and double quotes '''Accounts'''.
  
 
|-
 
|-
Line 160: Line 160:
 
|-
 
|-
 
| 04:02
 
| 04:02
| Now, let us call the '''getDetails() method '''using the '''Manager object.'''
+
| Now, let us call the '''getDetails()''' method using the '''Manager''' object.
  
 
|-
 
|-
 
| 04:08
 
| 04:08
| So, type '''System.out.println '''within brackets '''manager dot getDetails '''
+
| So, type: '''System.out.println '''within brackets '''manager dot getDetails'''.
  
 
|-
 
|-
 
| 04:17
 
| 04:17
| Now, let us save and run the program.
+
| Now, let us '''save''' and '''run''' the program.
  
 
|-
 
|-
 
| 04:21
 
| 04:21
| We can see that we get the '''output '''as ''':'''
+
| We can see that we get the output as ''':'''
 
'''Name: Nikkita Dinesh'''
 
'''Name: Nikkita Dinesh'''
 
'''Email: abc@gmail.com'''
 
'''Email: abc@gmail.com'''
Line 178: Line 178:
 
|-
 
|-
 
| 04:30
 
| 04:30
| Here, the '''object '''of the '''Manager class '''calls the '''getDetails() method.'''
+
| Here, the object of the ''''Manager' class '''calls the '''getDetails()''' method.
  
 
|-
 
|-
 
| 04:36
 
| 04:36
| Now, come to the '''Manager class.'''
+
| Now, come to the '''Manager''' class.
  
 
|-
 
|-
 
| 04:39
 
| 04:39
| We can see that there is no '''getDetails() method '''here.
+
| We can see that there is no '''getDetails()''' method here.
  
 
|-
 
|-
Line 194: Line 194:
 
|-
 
|-
 
| 04:52
 
| 04:52
| The '''Manager class '''automatically inherits the '''variables '''and '''methods '''of  '''Employee class.'''
+
| The '''Manager''' class automatically inherits the variables and '''method'''s of  '''Employee''' class.
  
 
|-
 
|-
 
| 04:59
 
| 04:59
| So it checks in the '''parent class '''which is '''Employee.'''
+
| So, it checks in the '''parent class '''which is '''Employee.'''
  
 
|-
 
|-
 
| 05:04
 
| 05:04
| Let us come to the '''Employee class.''' It finds the '''getDetails() method '''here.
+
| Let us come to the '''Employee''' class. It finds the '''getDetails()''' method here.
  
 
|-
 
|-
Line 210: Line 210:
 
|-
 
|-
 
| 05:20
 
| 05:20
| Now, let us change the '''getDetails method ''' to '''private.''' Save the file.
+
| Now, let us change the '''getDetails''' method  to '''private.''' '''Save''' the file.
  
 
|-
 
|-
Line 218: Line 218:
 
|-
 
|-
 
| 05:34
 
| 05:34
| It says '''The method getDetails() from the type Employee is not visible.'''
+
| It says "The method getDetails() from the type Employee is not visible".
  
 
|-
 
|-
 
| 05:40
 
| 05:40
| This means that '''getDetails() method '''cannot be accessed.
+
| This means that '''getDetails()''' method cannot be accessed.
  
 
|-
 
|-
 
| 05:45
 
| 05:45
| This is because we have declared '''getDetails() method '''as '''private.'''
+
| This is because we have declared '''getDetails()''' method as 'private'.
  
 
|-
 
|-
Line 238: Line 238:
 
|-
 
|-
 
| 06:04
 
| 06:04
| The superclass can have '''public''' or '''protected methods'''.
+
| The superclass can have '''public''' or '''protected''' methods.
  
 
|-
 
|-
 
| 06:09
 
| 06:09
|  These '''methods''' can access its '''private''' fields.
+
|  These '''method'''s can access their '''private''' '''field'''s.
  
 
|-
 
|-
 
| 06:13
 
| 06:13
|  The '''subclass''' can also access the '''private''' fields through these '''methods'''.
+
|  The '''subclass''' can also access the '''private''' '''field'''s through these '''method'''s.
  
 
|-
 
|-
Line 254: Line 254:
 
|-
 
|-
 
| 06:21
 
| 06:21
| Now, let us include the '''method getDetails '''in the '''Manager class.'''
+
| Now, let us include the method '''getDetails '''in the '''Manager''' class.
  
 
|-
 
|-
Line 262: Line 262:
 
|-
 
|-
 
| 06:33
 
| 06:33
| So type, '''public String getDetails''' parentheses.
+
| So, type: '''public String getDetails''' parentheses.
  
 
|-
 
|-

Revision as of 08:20, 17 May 2017

Time
Narration
00:01 Welcome to the spoken-tutorial on Subclassing and Method overriding.
00:06 In this tutorial, we will learn about:
  • subclassing
  • extends keyword and
  • method overriding.
00:15 Here we are using:
  • Ubuntu Linux version 12.04
  • JDK 1.7
  • Eclipse 4.3.1
00:25 To follow this tutorial, you must have knowledge of basics of Java and Eclipse IDE.
00:32 If not, for relevant Java tutorials, please visit our website.
00:37 First of all, we will learn what subclassing is.
00:41 It is a way to create a new class from an existing class.
00:46 The new class created is subclass or derived class or child class.
00:53 The already existing class is called superclass or base class or parent class.
01:00 Now, let me show you how to create a subclass. I have already created a project named MyProject.
01:10 I have created a class in it named Employee.
01:15 It has variables, name and email_address.
01:19 It also has the setter and getter methods for the class.
01:24 It has a method "getDetails()". This method returns "name" and "email_address".
01:31 Now, let us come to the Manager class.
01:35 It has variables, name, email_address and department.
01:40 We can see that some variables are common to both Employee and Manager class.
01:47 name and email_address are present in the Employee class. We can see that it is also there in Manager class.
01:57 Thus, Manager class can be made a subclass of Employee class.
02:03 For that, we have to make some changes in the Manager class.
02:08 After public class Manager, type: extends Employee.
02:14 We use the extends keyword to create a subclass from an existing class.
02:21 Remove the duplicate variables common to both the classes.
02:26 So, remove name and email_address from Manager class.
02:32 Also remove setter and getter methods of the same.
02:37 In the class 'Manager', we will have only one variable department.
02:43 We also have the setter and getter method for department.
02:59 In this way, the 'Manager' class inherits the members of 'Employee' class.
02:55 This way of extending one class from another is called single inheritance.
03:02 I have also created another class named 'TestEmployee'.
03:08 Inside the main method, we will create the object of Manager class.
03:14 So, inside the main method, type: Manager manager equal to new Manager parentheses.
03:23 Next, we will call the setter methods of the Manager class.
03:28 So, type, manager dot setName within brackets and double quotes Nikkita Dinesh
03:38 Then type: manager dot setEmail within brackets and double quotes abc at gmail dot com.
03:49 Then type, manager dot setDepartment within brackets and double quotes Accounts.
03:57 You can use any name, email address and department.
04:02 Now, let us call the getDetails() method using the Manager object.
04:08 So, type: System.out.println within brackets manager dot getDetails.
04:17 Now, let us save and run the program.
04:21 We can see that we get the output as :

Name: Nikkita Dinesh Email: abc@gmail.com

04:30 Here, the object of the 'Manager' class calls the getDetails() method.
04:36 Now, come to the Manager class.
04:39 We can see that there is no getDetails() method here.
04:43 But, we still got the output. This is because, the Manager class extends the Employee class.
04:52 The Manager class automatically inherits the variables and methods of Employee class.
04:59 So, it checks in the parent class which is Employee.
05:04 Let us come to the Employee class. It finds the getDetails() method here.
05:11 Note that we have not returned the department. As a result, it did not print the department in the output.
05:20 Now, let us change the getDetails method to private. Save the file.
05:27 We can see that we get a compilation error in the TestEmployee class.
05:34 It says "The method getDetails() from the type Employee is not visible".
05:40 This means that getDetails() method cannot be accessed.
05:45 This is because we have declared getDetails() method as 'private'.
05:52 A subclass does not inherit the private members of its superclass.
05:58 Subclass cannot directly access the private members of the superclass.
06:04 The superclass can have public or protected methods.
06:09 These methods can access their private fields.
06:13 The subclass can also access the private fields through these methods.
06:18 So, let us change it back to public.
06:21 Now, let us include the method getDetails in the Manager class.
06:27 This method will return name, email_address and department.
06:33 So, type: public String getDetails parentheses.
06:39 Inside the method, type return within brackets Name plus getName() plus slash n plus Email plus getEmail() plus slash n plus Manager of plus getDepartment() semicolon. Save the file.
07:07 Note that now, we have the method getDetails in both Manager and Employee class.
07:15 The name, return type and argument list of the method is same in both the classes.
07:22 A method in the subclass is said to override the method in the parent class if:

name return type argument list matches exactly.

07:33 Come back to the Manager class.
07:36 Before, the getDetails() method type @Override.
07:43 This is an override annotation. It indicates that a method is intended to override a method in superclass.
07:53 Now, let us see what annotation is.
07:57 Annotations:

start with at sign character provide data about a program have no direct effect on the operation of the code.

08:10 If a method is annotated with @Override, compiler generates error if: the method does override a method declared in a superclass.
08:23 The method signature is different in its superclass.
08:28 Now, let us come back to the IDE. Come back to the Manager class.
08:34 The at sign character indicates the compiler that what follows is an annotation.
08:42 Here, it shows that the getDetails method is overridden.
08:48 Let us come to the TestEmployee class.
08:51 Save the file and run the program.
08:55 We get the output as follows.

Name: Nikkita Dinesh Email: abc@gmail.com Manager of Accounts

09:05 Here, the object of the Manager class calls the getDetails() method.
09:11 But this time, it calls the method of Manager class itself.
09:16 In this way, we override the method of the parent class by the subclass.
09:23 Let us summarize.

In this tutorial we have learnt about Subclassing and Method Overriding

09:31 As an assignment Create a class Vehicle which has a method run that prints “The Vehicle is running.”
09:40 Also create a class Bike which has a method run that prints “The Bike is running safely.”
09:48 The output should be “The Bike is running safely.”
09:52 To know more about the Spoken Tutorial Project

Watch the video available at the following link It summarizes the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it

10:06 The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials Gives certificates for those who pass an online test For more details, please write to contact at spoken hyphen tutorial dot org

10:21 Spoken Tutorial Project is a part of the Talk to a Teacher project.

It is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at http://spoken-tutorial.org/NMEICT- Intro

10:42 This is Arya Ratish from IIT Bombay signing off.

Thanks for joining.

Contributors and Content Editors

Jyotisolanki, PoojaMoolya, Pratik kamble, Sandhya.np14