Java/C3/Subclassing-and-Method-Overriding/English
Title of script: Subclassing and Method overriding
Author: arya
Keywords: subclassing, extends keyword, overriding, method overriding, Java, Eclipse IDE, video tutorial
|
|
Slide 1 | Welcome to the spoken-tutorial on Subclassing and Method overriding. |
Slide 2 | In this tutorial we will learn about :
|
Slide 3
Software Requirements |
Here we are using
|
Slide 4
Prerequisites |
To follow this tutorial, you must have knowledge of basics of Java and Eclipse IDE.
|
Slide 5
Subclassing |
First of all, we will learn what subclassing is.
|
Go to the Eclipse IDE >> Click on MyProject | I have already created a project named MyProject. |
Highlight the Employee class.
public class Employee { |
I have created a class in it named Employee. |
Highlight the variables.
public String email_address; |
It has variables, name and email_address. |
Highlight the setter and getter methods.
name=newName; }
return name; }
email_address=newEmail; }
return email_address; } |
It also has the setter and getter methods for the class. |
Highlight the method getDetails().
{
return("Name: " + getName() + "\n" + "Email: " + getEmail()); }
} |
It has a method getDetails().
|
Highlight the Manager class.
|
Now, let us come to the Manager class. |
Highlight the variables.
public String email_address; public String department=""; |
It has variables, name, email_address and department. |
Highlight name and email_address in both the Employee and Manager class.
public String email_address; |
We can see that some variables are common to both Employee and Manager class.
|
Thus, Manager class can be made a subclass of Employee class. | |
For that, we have to make some changes in the Manager class. | |
Type public class Manager extends Employee{ | After public class Manager, type
extends Employee |
Highlight the extends keyword. | We use the extends keyword to create a subclass from an existing class. |
Remove name, email_address from Manager class. | Remove the duplicate variables common to both the classes.
|
Highlight variable department. | In the class Manager, we will have only one variable department. |
Highlight the setter and getter method.
department=newDepartment; }
return department; } |
We also have the setter and getter method for department. |
In this way, the Manager class inherits the members of Employee class. | |
This way of extending one class from another is called single inheritance. | |
Open the class TestEmployee.
|
I have also created another class named TestEmployee.
|
Highlight object of the Manager class.
|
Inside the main method, we will create the object of the Manager class.
Manager manager equal to new Manager parentheses |
Highlight
|
Next, we will call the setter methods of the Manager class.
manager dot setName within brackets and double quotes Nikkita Dinesh |
Highlight
manager.setEmail("abc@gmail.com"); |
Then type,
manager dot setEmail within brackets and double quotes abc at gmail dot com |
Highlight
manager.setDepartment(“Accounts”); |
Then type,
manager dot setDepartment within brackets and double quotes Accounts
|
Type
|
Now, let us call the getDetails() method using the Manager object.
System.out.println within brackets manager dot getDetails |
Run the program. | Now, let us save and run the program. |
Highlight the output
Email: abc@gmail.com |
We can see that we get the output as :
Email: abc@gmail.com |
Highlight the println statement in TestEmployee class. | Here, the object of Manager class calls the getDetails() method. |
Come to the Manager class. | Now, come to the Manager class. |
Point that there is no getDetails() method. | We can see that there is no getDetails() method here. |
Highlight the Manager class. | But, we still got the output.
|
Highlight the getDetails() method in the Employee class.
|
So it checks in the parent class which is Employee.
Let us come to the Employee class. It finds the getDetails() method here.
As a result, it did not print the department in the output. |
Change getDetails method to private. | Now, let us change the getDetails method to private.
Save the file. |
Point to the error. | We can see that we get a compilation error in the TestEmployee class. |
It says The method getDetails() from the type Employee is not visible. | |
This means that getDetails() method cannot be accessed. | |
Come to the Employee class. Highlight getDetails() method. | This is because we have declared getDetails() method as private. |
Slide 6
Private members in a superclass |
|
Change the getDetails() method from private to public. | So, let us change it back to public. |
Now, let us include method getDetails in the Manager class. | |
Type
public String getDetails() { return ( "Name: " + getName() + "\n" + "Email: " +getEmail()+"\n" +"Manager of: " + getDepartment()); } |
This method will return name, email_address and department.
public String getDetails parentheses.
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. |
Highlight the method getDetails in both Employee and Manager class. | Note that now, we have the method getDetails in both Manager and Employee class. |
Highlight the method declaration in both the classes.
|
The name, return type and argument list of the method is same in both the classes.
|
Slide 7
Method overriding |
A method in the subclass is said to override the method in the parent class if:
matches exactly. |
Come back to the Manager class. | Come back to the Manager class. |
Type @Override. | Before, the getDetails() method type @Override.
|
Slide 8
Annotation |
Annotations:
|
Slide 9
@Override Annotation
|
If a method is annotated with @Override, compiler generates error message if:
|
Come back to the IDE.
|
Now, let us come back to the IDE.
|
Come to the TestEmployee class. | Let us come to the TestEmployee class. |
Right click on TestEmployee | Save the file and run the program. |
Highlight the output.
Manager of:Accounts |
We get the output as follows.
Email: abc@gmail.com Manager of Accounts |
Here, the object of the Manager class calls the getDetails() method.
| |
Slide 10
Summary |
Let us summarize.
In this tutorial we have learnt about
|
Slide 11
|
As an assignment
“The Bike is running safely.”
|
Slide 12
|
To know more about the Spoken Tutorial Project
|
Slide 13
Spoken Tutorial Workshop |
The Spoken Tutorial Project Team
|
Slide 14
Acknowledgement |
Spoken Tutorial Project is a part of the Talk to a Teacher project.
This is Arya Ratish from IIT Bombay signing off. Thanks for joining. |