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

From Script | Spoken-Tutorial
Revision as of 13:03, 12 March 2014 by Arya Ratish (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Subclassing and Method overriding

Author: arya

Keywords: subclassing, extends keyword, overriding, method overriding, Java, Eclipse IDE, video tutorial


Visual Cue
Narration
Slide 1 Welcome to the spoken-tutorial on Subclassing and Method overriding.
Slide 2 In this tutorial we will learn about :
  • subclassing
  • extends keyword and
  • method overriding


Slide 3

Software Requirements

Here we are using
  • Ubuntu Linux version 12.04
  • JDK 1.7
  • Eclipse 4.3.1


Slide 4

Prerequisites

To follow this tutorial, you must have knowledge of

basics of Java and Eclipse IDE.


If not, for relevant Java tutorials, please visit our website.

Slide 5

Subclassing

First of all, we will learn what subclassing is.


  • It is a way to create a new class from an existing class.
  • The new class created is called subclass or derived class or child class.
  • The already existing class is called superclass or base class or parent class.


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.


public class Employee {

I have already created a class called Employee.
Highlight the variables.


public String name="";

public String email_address;

It has variables name and email_address.
Highlight the setter and getter methods.


public void setName(String newName) {

name=newName;

}


public String getName() {

return name;

}


public void setEmail(String newEmail) {

email_address=newEmail;

}


public String getEmail() {

return email_address;

}

It also has the setter and getter methods for the class.
Highlight the method getDetails().


public String getDetails()

{

return("Name: " + getName() + "\n" + "Email: " + getEmail());

}

}

It has a method getDetails().


This method returns the name and email_address.

Highlight the Manager class.


public class Manager {

Now, let us come to the class called Manager.
Highlight the variables.


public String name="";

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 name="";

public String email_address;

We can see that some variables are common to both Employee and Manager class.


name and email_address are there in Employee class.


We can see that its also there in 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.


public void setDepartment(String newDepartment) {

department=newDepartment;

}


public String getDepartment() {

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.


public class TestEmployee {

I have also created another class named TestEmployee.



Highlight object of the Manager class.


public static void main(String[] args) {


Manager manager = new Manager();

Inside the main method, we will create the object of the Manager class.


So inside the main method, type

Manager manager equal to new Manager parentheses.

Highlight


manager.setName("Nikkita Dinesh");

manager.setEmail("abc@gmail.com");


manager.setDepartment(“Accounts”);

Next, we will call the setter methods of the Manager class.


So type,

manager dot setName within brackets and double quotes Nikkita Dinesh semicolon.


Then type,

manager dot setEmail within brackets and double quotes abc@gmail.com semicolon.


Then type,

manager dot setDepartment within brackets and double quotes Accounts semicolon.


You can use any name, emailaddress and department.

Type


System.out.println(manager.getDetails());

Now, let us call the getDetails() method using the Manager object.


So, type

System.out.println within brackets manager dot getDetails parentheses

Run the program. Now, let us run the program.
Highlight the output


Name: Nikkita Dinesh

Email: abc@gmail.com

We can see that we get the output as :


Name: Nikkita Dinesh

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.


This is because, the Manager class extends the Employee class.


The Manager class automatically inherits the variables and methods of the Employee class.



Highlight the getDetails() method in the Employee class.


Highlight the return statement.


So it checks in the parent class which is Employee.

Let us come to the Employee class.

It finds the getDetails() method here.


Note that we have not returned the department.

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.
  • Subclass cannot directly access the private members of the superclass.
  • However, the superclass can have public or protected methods.
  • These methods can access its private fields.
  • Then the subclass can also access the private fields through these methods.


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.


So type,

public String getDetails parentheses.


Inside the method we will return name, email and department.


So type

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.


public String getDetails()

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:


  • name
  • return type
  • argument list

matches exactly.

Come back to the IDE. Now, let us come back to the IDE.


Come back to the Manager class.

Type @Override. Before, the getDetails() method type @Override.


This is an override annotation.


It indicates that a method is intended to override a method in superclass.


Now, let us see what an annotation is.

Slide 8

Annotation

Annotations:
  • start with at sign character(@)
  • provide data about a program
  • have no direct effect on the operation of the code.


Slide 9

@Override Annotation


If a method is annotated with @Override compiler generates error message if:


  • the method does override a method declared in a superclass.
  • the method signature is different in its superclass.


Come back to the IDE.


Highlight the annotation.

Now, let us come back to the IDE.


Come back to the Manager class.


The at sign character indicates the compiler that what follows is an annotation.


Here, it shows that the getDetails method is overridden.

Come to the TestEmployee class. Let us come to the TestEmployee class.
Right click on TestEmployee Now, let us run the program.


For that, right click on TestEmployee.

Click on Run As. Then click on Run As.
Click on Java Application. Click on Java Application.
Highlight the output.


Name: Nikkita Dinesh

Email:abc@gmail.com

Manager of:Accounts

We get the output as follows.


Name: Nikkita Dinesh

Email:abc@gmail.com

Manager of:Accounts

Here, the object of the Manager class calls the getDetails() method.


But this time, it calls the method of Manager class itself.


In this way, we override the method of the parent class by the subclass.

Slide 10

Summary

Let us summarize.

In this tutorial we have learnt about

  • Subclassing
  • extends keyword
  • Overriding


Slide 11


Assignment

As an assignment


Create a class Vehicle which has a method run that prints “The Vehicle is running.”


Also create a class Bike which has a method run that prints “The Bike is running safely.”


The output should be “The Bike is running safely.”

Slide 12


About Spoken Tutorial Project

is this fine check?


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 itModifiedok do it in previous tutorial as wellTo 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


Slide 13

Spoken Tutorial Workshop

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


Slide 14

Acknowledgement

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

This is Arya Ratish from IIT Bombay signing off.

Thanks for joining.

Contributors and Content Editors

Arya Ratish, Nancyvarkey