Java/C3/Subclassing-and-Method-Overriding/English-timed

From Script | Spoken-Tutorial
Revision as of 16:37, 23 November 2017 by Pratik kamble (Talk | contribs)

Jump to: navigation, search
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:49 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 ,match 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