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. | Now, let me show you how to create a sub-class.
So, go to the Eclipse IDE. |
Highlight the Employee class.
|
I have already created a class called 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 class called Manager. |
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.
So, remove name and email_address from Manager class. Also remove setter and getter methods of the same. |
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
manager.setEmail("abc@gmail.com");
|
Next, we will call the setter methods of the Manager class.
manager dot setName within brackets and double quotes Nikkita Dinesh semicolon.
manager dot setEmail within brackets and double quotes abc@gmail.com semicolon.
manager dot setDepartment within brackets and double quotes Accounts semicolon.
|
Type
|
Now, let us call the getDetails() method using the Manager object.
System.out.println within brackets manager dot getDetails parentheses |
Run the program. | Now, let us 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 here to private. |
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 |
* A subclass does not inherit the private members of its 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 the name, email_address and department.
public String getDetails parentheses.
return “Name:” plus getName() plus “Email:” plus getEmail() plus “Manager of:” plus getDepartment() |
Highlight the method getDetails in both Employee and Manager class. | Note that now, we have the method getDetails in both Employee and Manager 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 IDE. | Now, let us come back to the IDE.
|
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 | Now, let us run the program.
|
Click on Run As. | Then click on Run As. |
Click on Java Application. | Click on Java Application. |
Highlight the output.
Manager of:Accounts |
We get the output as follows.
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
|
Slide 12
|
is this fine check?
- Watch the video available at the following link
|
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. |