Java/C3/Calling-methods-of-the-superclass/English

From Script | Spoken-Tutorial
Revision as of 12:53, 3 May 2014 by Trupti (Talk | contribs)

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

Title of script: Calling methods of the super class

Author: Arya Ratish

Keywords: super keyword, video tutorial, Java, Eclipse IDE


Visual Cue
Narration
Slide 1 Welcome to the spoken-tutorial on Calling methods of the super class.
Slide 2 In this tutorial we will learn  :
  • when to use the super keyword
  • how to call methods of the super class
  • how to invoke the constructor of the super class


Slide 3

Software Requirements

Here we are using
  • Ubuntu Version 12.04
  • JDK 1.7
  • Eclipse 4.3.1


Slide 4

Prerequisites

To follow this tutorial, you must have basic knowledge of Java and Eclipse IDE.


You must also have knowledge of subclassing and method overriding.


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

Slide 5

Using super keyword


A subclass can use a superclass data or method using the super keyword.
Slide 6

Using super keyword

The super keyword :
  • refers to the instance variable of the parent class.
  • is used to invoke parent class constructor.
  • is used to invoke parent class method.


Go to the IDE. Now, let us go to the IDE and to the project we created earlier.
Go to the Manager class. Let us go to the Manager class.
Come to the getDetails() method.

Remove Name and Email.

Now, come to the getDetails() method.


In the return statement, let us remove Name and Email.

Go to the Employee class.


Highlight the getDetails() method.

Let us come to the Employee class.


This is the parent class or super class.


We already have a getDetails() method here.

Highlight the return statement. This method returns the name and email.
So, we can use the getDetails() method in the Manager class.
Go to the Manager class. We will come to the Manager class.
We will call  getDetails() method from the Employee class, inside the Manager class.
Come to the getDetails() method. So, come to the getDetails() method.
Inside the return statement type super dot getDetails(). Inside the return statement type

super dot getDetails()  plus within double quotes Manager of plus department.

Run the program. Now, let me run the program again.
Highlight the output. We can see that we get the Manager details.
Come back to the Employee class. Come back to the Employee class.
Let us include a constructor here.
Type

public Employee(String name, String email_address)  {

this.name=name;

this.email_address=email_address;

}

So inside the Employee class, type


public space Employee within brackets String name, String email_address


Within curly brackets, type


this dot name is equal to name semicolon

this dot email_address is equal to email_address semicolon



Also comment the setter and getter methods.
Type


public String getDetails()

{


return("Name: " + name + "\n" + "Email: " + email_address);

}

Inside the getDetails() method,  
  • instead of getName type name,
  • instead of getEmail type email_address.


A subclass inherits all of the methods and variables from a parent class.


Note that it does not inherit constructors.

But, constructors can call the non-private constructors of its superclass.


We do this using the keyword super from the child class constructor.

Come to the Manager class. We will see that now.


For that, come to the Manager class.


Now, we will include a constructor here.

Type


public Manager(String name, String email_address, String dept) {

super(name,email_address);

department=dept;

}

Type


public space Manager within brackets String space name, String space email  address, String space dept


Then within curly brackets, type


super within brackets name, email address semicolon


Then type,


department is equal to dept semicolon

Here we will comment the setter and getter methods.
Type

public String getDetails() {

return ( super.getDetails()+"\n"

+"Manager of: " + department);

}

Then, in the getDetails() method type department instead of getDepartment.
Now, come to the TestEmployee class.
Comment manager.setName("Nikkita Dinesh");

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

manager.setDepartment("Accounts");

Comment the call to the setter methods.
Type

Manager manager= new Manager("Nikkita Dinesh", "abc@gmail.com", "Accounts");

Now, inside the call to the Manager constructor type within quotes Nikkita Dinesh, abc@gmail.com, Accounts
Now, run the program.
Highlight


Name: Nikkita Dinesh

Email: abc@gmail.com

Manager of: Accounts


We get the output as shown.

We get the Manager details.

In this way, we can call the constructor of the super class.
Slide 7

Summary

In this tutorial we have learnt:
  • the super keyword
  • calling the method of the super class and
  • invoking constructor of super class


Slide 8


Assignment

Assignment
  • Open the previous assignment.
  • Call the Vehicle class run method in the Bike class.
  • The output should be:
    • The Vehicle is running.
    • The Bike is running safely.


Slide 9


About 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 10

About Spoken Tutorial workshops

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 11

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

Trupti