Difference between revisions of "Java/C3/Subclassing-and-Method-Overriding/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': Subclassing and Method overriding '''Author: arya ''' '''Keywords: '''subclassing, extends keyword, overriding, method overriding, Java, Eclipse IDE, vid…')
 
 
Line 13: Line 13:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''Slide 1'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''Slide 1'''
| style="border:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken-tutorial on''' Subclassing and Method overriding.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken-tutorial on''' Subclassing''' and '''Method overriding.'''
  
 
|-
 
|-
Line 22: Line 22:
 
* '''extends '''keyword and
 
* '''extends '''keyword and
 
* '''method overriding'''
 
* '''method overriding'''
 
 
  
 
|-
 
|-
Line 34: Line 32:
 
* '''JDK '''1.7
 
* '''JDK '''1.7
 
* '''Eclipse '''4.3.1
 
* '''Eclipse '''4.3.1
 
 
  
 
|-
 
|-
Line 41: Line 37:
  
 
'''Prerequisites'''
 
'''Prerequisites'''
| style="border:1pt solid #000000;padding:0.106cm;"| To follow this tutorial, you must have knowledge of
+
| style="border:1pt solid #000000;padding:0.106cm;"| To follow this tutorial, you must have knowledge of basics of '''Java''' and '''Eclipse IDE.'''
 
+
'''basics of Java and Eclipse IDE.'''
+
  
  
Line 53: Line 47:
 
'''Subclassing'''
 
'''Subclassing'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| First of all, we will learn what '''subclassing '''is.
 
| style="border:1pt solid #000000;padding:0.097cm;"| First of all, we will learn what '''subclassing '''is.
 
  
 
* It is a way to create a new '''class''' from an existing '''class.'''
 
* 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 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.'''
+
 
+
  
 +
* The already existing '''class''' is called '''superclass '''or''' base class '''or''' parent class.'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the '''Eclipse''' '''IDE.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the '''Eclipse''' '''IDE''' >> Click on '''MyProject'''
| style="border:1pt solid #000000;padding:0.097cm;"| Now, let me show you how to create a '''sub-class. '''
+
| style="border:1pt solid #000000;padding:0.097cm;"| I have already created a project named '''MyProject'''.
 
+
So, go to the '''Eclipse''' '''IDE.'''
+
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the '''Employee class.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the '''Employee class.'''
 
  
 
'''public class Employee {'''
 
'''public class Employee {'''
| style="border:1pt solid #000000;padding:0.097cm;"| I have already created a '''class '''called '''Employee.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| I have created a '''class ''' in it named '''Employee.'''
  
 
|-
 
|-
Line 83: Line 71:
  
 
'''public String email_address<nowiki>;</nowiki>'''
 
'''public String email_address<nowiki>;</nowiki>'''
| style="border:1pt solid #000000;padding:0.097cm;"| It has '''variables name '''and '''email_address.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| It has '''variables, name '''and '''email_address.'''
  
 
|-
 
|-
Line 144: Line 132:
  
 
'''public class Manager {'''
 
'''public class Manager {'''
| style="border:1pt solid #000000;padding:0.097cm;"| Now, let us come to the '''class '''called '''Manager.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now, let us come to the '''Manager class'''.
  
 
|-
 
|-
Line 155: Line 143:
  
 
'''public String department<nowiki>="";</nowiki>'''
 
'''public String department<nowiki>="";</nowiki>'''
| style="border:1pt solid #000000;padding:0.097cm;"| It has '''variables name, email_address '''and '''department.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| It has '''variables, name, email_address '''and '''department.'''
  
 
|-
 
|-
Line 182: Line 170:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''public class Manager extends Employee{'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''public class Manager extends Employee{'''
| style="border:1pt solid #000000;padding:0.097cm;"| After '''public class Manager, '''type '''extends Employee.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| After '''public class Manager, '''type  
 +
 
 +
'''extends Employee'''
  
 
|-
 
|-
Line 191: Line 181:
 
| style="border:1pt solid #000000;padding:0.097cm;"| Remove '''name, email_address '''from '''Manager class.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Remove '''name, email_address '''from '''Manager class.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Remove the duplicate '''variables '''common to both the '''classes.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Remove the duplicate '''variables '''common to both the '''classes.'''
 +
  
 
So, remove '''name '''and''' email_address '''from '''Manager class.'''
 
So, remove '''name '''and''' email_address '''from '''Manager class.'''
 +
  
 
Also remove '''setter '''and '''getter methods '''of the same.
 
Also remove '''setter '''and '''getter methods '''of the same.
Line 232: Line 224:
 
'''public class TestEmployee {'''
 
'''public class TestEmployee {'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| I have also created another '''class '''named '''TestEmployee.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| I have also created another '''class '''named '''TestEmployee.'''
 
 
  
  
Line 249: Line 239:
 
So inside the '''main method''', type  
 
So inside the '''main method''', type  
  
'''Manager manager ''equal to''''' '''new Manager parentheses'''.''' '''
+
'''Manager manager equal to new Manager parentheses  
  
 
|-
 
|-
Line 256: Line 246:
  
 
'''manager.setName("Nikkita Dinesh");'''
 
'''manager.setName("Nikkita Dinesh");'''
 
'''manager.setEmail("[mailto:abc@gmail.com abc][mailto:abc@gmail.com @][mailto:abc@gmail.com gmail][mailto:abc@gmail.com .][mailto:abc@gmail.com com]");'''
 
 
 
'''manager.setDepartment(“Accounts”);'''
 
 
| style="border:1pt solid #000000;padding:0.097cm;"| Next, we will call the '''setter methods '''of the '''Manager class.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Next, we will call the '''setter methods '''of the '''Manager class.'''
  
Line 266: Line 251:
 
So type,  
 
So type,  
  
'''manager ''dot ''setName ''within brackets and double quotes ''Nikkita Dinesh ''semicolon.'''''
+
'''manager dot setName''' within brackets and double quotes '''Nikkita Dinesh '''
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight
 +
'''manager.setEmail("abc@gmail.com");'''
  
Then type,  
+
| style="border:1pt solid #000000;padding:0.097cm;"|Then type,  
  
'''manager ''dot ''setEmail ''within brackets and double quotes ''[mailto:abc@gmail.com abc][mailto:abc@gmail.com @][mailto:abc@gmail.com gmail][mailto:abc@gmail.com .][mailto:abc@gmail.com com] ''semicolon.'''''
+
'''manager dot setEmail '''within brackets and double quotes '''abc at gmail dot com'''  
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight
 +
 +
'''manager.setDepartment(“Accounts”);'''
  
Then type,  
+
| style="border:1pt solid #000000;padding:0.097cm;"|Then type,  
  
'''manager ''dot ''setDepartment ''within brackets and double quotes ''Accounts ''semicolon.'''''
+
'''manager dot setDepartment''' within brackets and double quotes '''Accounts'''
  
  
You can use any '''name, emailaddress''' and '''department'''.
+
You can use any '''name, email address''' and '''department'''.
  
 
|-
 
|-
Line 291: Line 283:
 
So, type  
 
So, type  
  
'''System.out.println ''within brackets ''manager ''dot ''getDetails ''parentheses'''''
+
'''System.out.println '''within brackets '''manager dot getDetails '''
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Run the program.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Run the program.
| style="border:1pt solid #000000;padding:0.097cm;"| Now, let us run the program.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now, let us save and run the program.
  
 
|-
 
|-
Line 314: Line 306:
 
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the '''println statement '''in '''TestEmployee class.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the '''println statement '''in '''TestEmployee class.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Here, the '''object '''of '''Manager class '''calls the '''getDetails() method.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Here, the '''object '''of '''Manager class '''calls the '''getDetails() method.'''
 
 
 
  
 
|-
 
|-
Line 334: Line 323:
  
  
The '''Manager class '''automatically '''inherits '''the '''variables '''and '''methods '''of the '''Employee class.'''
+
The '''Manager class '''automatically inherits the '''variables '''and '''methods '''of the '''Employee class.'''
 
+
 
+
 
+
  
 
|-
 
|-
Line 356: Line 342:
 
Note that we have not returned the '''department.'''
 
Note that we have not returned the '''department.'''
  
As a result, it did not print the '''department '''in the '''output.'''
+
As a result, it did not print the '''department '''in the output.  
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Change '''getDetails method '''to '''private.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Change '''getDetails method '''to '''private.'''
| style="border:1pt solid #000000;padding:0.097cm;"| Now, let us change the '''getDetails method '''here''' '''to '''private.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now, let us change the '''getDetails method ''' to '''private.'''
 +
 
 +
Save the file.
  
 
|-
 
|-
Line 382: Line 370:
  
 
'''Private members in a superclass'''
 
'''Private members in a superclass'''
| style="border:1pt solid #000000;padding:0.097cm;"| * A '''subclass''' does not inherit the private members of its '''superclass'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"|  
* '''Subclass '''cannot directly access the private members of the '''superclass.'''
+
* A '''subclass''' does not inherit the '''private''' members of its '''superclass'''.
* However, the superclass can have public or protected methods.
+
* '''Subclass '''cannot directly access the '''private''' members of the '''superclass.'''
* These methods can access its private fields.
+
* However, the superclass can have '''public''' or '''protected methods'''.
* Then the subclass can also access the private fields through these methods.
+
* These '''methods''' can access its '''private''' fields.
 
+
* Then the '''subclass''' can also access the '''private''' fields through these '''methods'''.
 
+
  
 
|-
 
|-
Line 410: Line 397:
  
 
'''}'''
 
'''}'''
| style="border:1pt solid #000000;padding:0.097cm;"| This '''method '''will return the '''name, email_address '''and '''department.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| This '''method '''will return '''name, email_address '''and '''department.'''
  
  
 
So type,  
 
So type,  
  
'''public String getDetails ''parentheses.'''''
+
'''public String getDetails parentheses.'''
  
  
Inside the method we will return '''name, email''' and '''department'''.
+
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'''
  
So type'' ''
+
Save the file.
 
+
'''return “Name:” ''plus ''getName() ''plus ''“Email:” ''plus ''getEmail() ''plus ''“Manager of:” ''plus ''getDepartment() '''
+
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the '''method getDetails '''in both '''Employee '''and '''Manager class.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the '''method getDetails '''in both '''Employee '''and '''Manager class.'''
| style="border:1pt solid #000000;padding:0.097cm;"| Note that now, we have the '''method getDetails '''in both '''Employee '''and '''Manager class.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Note that now, we have the '''method getDetails '''in both '''Manager''' and '''Employee  class.'''
  
 
|-
 
|-
Line 435: Line 421:
 
'''public String getDetails() '''
 
'''public String getDetails() '''
 
| style="border:1pt solid #000000;padding:0.097cm;"| The '''name, return type '''and '''argument list '''of the''' method '''is same in both the '''classes.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| The '''name, return type '''and '''argument list '''of the''' method '''is same in both the '''classes.'''
 +
 +
  
 
|-
 
|-
Line 441: Line 429:
 
'''Method overriding'''
 
'''Method overriding'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| A''' method '''in the '''subclass '''is said to '''override '''the '''method '''in the '''parent class '''if:
 
| style="border:1pt solid #000000;padding:0.097cm;"| A''' method '''in the '''subclass '''is said to '''override '''the '''method '''in the '''parent class '''if:
 
  
 
* '''name'''
 
* '''name'''
Line 450: Line 437:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Come back to the '''IDE.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Come back to the '''Manager class.'''
| style="border:1pt solid #000000;padding:0.097cm;"| Now, let us come back to the '''IDE.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Come back to the '''Manager class.'''
 
+
 
+
Come back to the '''Manager class.'''
+
  
 
|-
 
|-
Line 461: Line 445:
  
  
This is an '''override''' '''annotation.'''
+
This is an '''override annotation.'''
  
  
It''' '''indicates that a method is intended to '''override''' a '''method''' in '''superclass'''.
+
It indicates that a '''method''' is intended to '''override''' a '''method''' in '''superclass'''.
  
  
Now, let us see what an '''annotation '''is.
+
Now, let us see what '''annotation '''is.
  
 
|-
 
|-
Line 475: Line 459:
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''Annotations:'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''Annotations:'''
  
* start with at sign character(@)
+
* start with '''at sign''' character'''(@)'''
 
* provide data about a program  
 
* provide data about a program  
 
* have no direct effect on the operation of the code.
 
* have no direct effect on the operation of the code.
 
 
  
 
|-
 
|-
Line 488: Line 470:
  
  
| style="border:1pt solid #000000;padding:0.097cm;"| If a '''method '''is '''annotated '''with '''@Override compiler '''generates '''error message '''if:
+
| style="border:1pt solid #000000;padding:0.097cm;"| If a '''method '''is '''annotated '''with '''@Override, compiler '''generates '''error message '''if:
 
+
  
 
* the''' method''' does '''override''' a '''method''' declared in a '''superclass'''.  
 
* the''' method''' does '''override''' a '''method''' declared in a '''superclass'''.  
 
* the '''method signature '''is different in its '''superclass.'''
 
* the '''method signature '''is different in its '''superclass.'''
 
 
  
 
|-
 
|-
Line 507: Line 486:
  
  
The '''at sign character '''indicates the compiler that what follows is an '''annotation.'''
+
The '''at sign''' character indicates the compiler that what follows is an '''annotation.'''
  
  
Line 518: Line 497:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| Right click on '''TestEmployee'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Right click on '''TestEmployee'''
| style="border:1pt solid #000000;padding:0.097cm;"| Now, let us '''run '''the '''program.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Save the file and run the program.
 
+
 
+
For that, right click on '''TestEmployee.'''
+
 
+
|-
+
| style="border:1pt solid #000000;padding:0.097cm;"| Click on '''Run As.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Then click on '''Run As.'''
+
 
+
|-
+
| style="border:1pt solid #000000;padding:0.097cm;"| Click on '''Java Application.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Click on '''Java Application.'''
+
  
 
|-
 
|-
Line 545: Line 513:
 
'''Name: Nikkita Dinesh'''
 
'''Name: Nikkita Dinesh'''
  
'''Email:[mailto:abc@gmail.com abc][mailto:abc@gmail.com @][mailto:abc@gmail.com gmail][mailto:abc@gmail.com .][mailto:abc@gmail.com com]
+
'''Email: abc@gmail.com'''
  
'''Manager of:Accounts'''
+
'''Manager of Accounts'''
  
 
|-
 
|-
Line 554: Line 522:
  
  
But this time, it calls the '''method of Manager class itself.'''
+
But this time, it calls the '''method''' of '''Manager class''' itself.
  
  
Line 567: Line 535:
 
In this tutorial we have learnt about
 
In this tutorial we have learnt about
  
* '''Subclassing'''
+
* '''Subclassing''' and
* '''extends '''keyword
+
 
* '''Overriding'''
 
* '''Overriding'''
 
 
  
 
|-
 
|-
Line 584: Line 549:
  
  
Also create a class '''Bike '''which has a method '''run '''that prints “'''The Bike is running safely.”'''
+
Also create a class '''Bike '''which has a method '''run '''that prints  
 +
 
 +
“'''The Bike is running safely.”'''
  
  
Line 594: Line 561:
  
 
'''About Spoken Tutorial Project'''
 
'''About Spoken Tutorial Project'''
| style="border:1pt solid #000000;padding:0.106cm;"| is this fine check?
+
| style="border:1pt solid #000000;padding:0.106cm;"|To know more about the Spoken Tutorial Project
 
+
 
+
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
 
* Watch the video available at the following link
 
 
* It summarizes the Spoken Tutorial project
 
* It summarizes the Spoken Tutorial project
 
* If you do not have good bandwidth, you can download and watch it
 
* If you do not have good bandwidth, you can download and watch it
 
 
  
 
|-
 
|-
Line 622: Line 575:
 
* Conducts workshops using spoken tutorials
 
* Conducts workshops using spoken tutorials
 
* Gives certificates for those who pass an online test
 
* Gives certificates for those who pass an online test
* For more details, please write to contact at spoken hyphen tutorial dot org
+
* For more details, please write to '''contact at spoken hyphen tutorial dot org'''
 
+
 
+
 
+
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''Slide 14'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| '''Slide 14'''
  
 
'''Acknowledgement'''
 
'''Acknowledgement'''
| style="border:1pt solid #000000;padding:0.106cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher project
+
| style="border:1pt solid #000000;padding:0.106cm;"| 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
+
* It is supported by the National Mission on Education through ICT, MHRD, Government of India.
  
 
* More information on this Mission is available at
 
* More information on this Mission is available at
  
 
* http://spoken-tutorial.org/NMEICT- Intro
 
* http://spoken-tutorial.org/NMEICT- Intro
 +
 +
  
 
This is '''Arya Ratish '''from '''IIT Bombay '''signing off.
 
This is '''Arya Ratish '''from '''IIT Bombay '''signing off.

Latest revision as of 15:36, 12 March 2014

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 >> 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 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 Manager class.
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");

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


So type,

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


You can use any name, email address 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

Run the program. Now, let us save and 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 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

  • 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 name, email_address and department.


So type,

public String getDetails parentheses.


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.

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.


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 Manager class. 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 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 Save the file and run the program.
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 and
  • 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

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