Difference between revisions of "Java/C3/Abstract-Classes/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(One intermediate revision by one other user not shown)
Line 22: Line 22:
  
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial we will learn about:
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial we will learn about:
 +
 
* '''Abstract Methods '''and''' Concrete Methods'''
 
* '''Abstract Methods '''and''' Concrete Methods'''
 
* '''Abstract Classes '''and''' Concrete Classes '''and
 
* '''Abstract Classes '''and''' Concrete Classes '''and
Line 34: Line 35:
  
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| For this tutorial, I am using
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| For this tutorial, I am using
 +
 
* '''Ubuntu 12.04'''
 
* '''Ubuntu 12.04'''
 
* '''JDK 1.7 '''and
 
* '''JDK 1.7 '''and
Line 59: Line 61:
  
 
'''What is an Abstract Method?'''
 
'''What is an Abstract Method?'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| An '''Abstract method''' is a''' method''' that is declared without implementation.
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| An '''Abstract method''' is a''' method''' that is declared without '''implementation'''.
  
  
It is declared using''' abstract '''keyword.
+
It is declared using''' abstract keyword'''.
  
  
 
There should not be opening and closing parenthesis for this '''method'''.
 
There should not be opening and closing parenthesis for this '''method'''.
 
|-
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 6:'''
 
 
'''Abstract Method'''
 
 
'''Example:'''
 
 
Highlight the method and '''abstract''' keyword
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The''' method showDetails()''' illustrated here, is an example of''' abstract method'''.
 
 
 
Note that this '''method''' is declared using the''' abstract '''keyword, with no current implementation.
 
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Next we will see about '''concrete method'''.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us understand the usage of '''Abstract class '''with a sample program.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 7:'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In Ecilpse IDE a project called '''AbstractDemo''' is created,
  
'''Concrete Method:'''
 
  
'''Example:'''
 
  
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we will switch to''' Eclipse''' and create a new '''project''' called '''AbstractDemo'''.
  
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| A '''Concrete method''' is completely implemented within the curly brackets.
+
Inside this '''project''' we will create the necessary '''classes''' to demonstrate the usage of''' Abstract class.'''
 
+
 
+
This method''' showBasicDetails()''' illustrated here is an example of '''concrete method'''.
+
 
+
 
+
Observe that this '''method''' is implemented completely.
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Create the''' class Person''' in the '''src''' folder.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we will learn about '''abstract class''' with an example.
+
  
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 8:'''
 
  
'''Abstract Class:'''
+
Type
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| An''' abstract class''' usually contains at least one''' abstract method'''.
+
  
 +
'''String name;'''
  
'''Abstract methods '''can be added only to '''abstract classes.'''
+
'''int age;'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now, right click on '''src''' folder and click '''New-'''> '''Class'''.
  
|-
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 9:'''
 
  
'''Abstract Class - Example'''
+
Type the name of the '''class''' as '''Person''' and press '''Enter'''.
  
  
 +
Now we will add the fields to represent the name and age of the''' Person'''.
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The '''class Person''' illustrated here is an''' abstract class.'''
 
  
 +
Type''' String name semicolon.'''
  
It contains an''' abstract method''' called''' showDetails()'''.
 
  
 
+
Also type''' int age semicolon.'''
It can also contain '''variables''' and other '''concrete methods.'''
+
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 10:'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Click on''' source ->''' and select''' generate constructor using fields'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now click on''' Source ->''' and select''' Generate constructor using fields. '''
  
'''Abstract Class - Example'''
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The figure here represents an '''inheritance relation'''.
 
  
Here, the''' Person class''' is an''' abstract class.'''
+
Delete the '''super keyword''' from the generated code.
 
+
The''' Employee class''' and the '''Student class''' are '''subclasses''' of the''' Person class'''.
+
 
+
These '''subclasses''' can provide their own different implementations.
+
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Go back to '''Slide 9 '''and point the '''showDetails() '''method
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Constructor''' code
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This '''constructor''' can initialise the values of '''name''' and '''age '''fields.
 
+
Switch to '''Slide 10'''
+
 
+
 
+
 
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| These are done by''' showDetails( ) method''' present in the''' Person class.'''
+
 
+
For example:
+
 
+
'''ShowDetails() Method''' in the''' Employee class''' prints the '''Employee ID''' and the''' Salary''', where as
+
 
+
'''ShowDetails() Method''' in the''' Student class''' prints the '''Student Reg No''' and the '''Grade.'''
+
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 11:'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type
  
'''Concrete Class:'''
+
'''public void showBasicDetails( )'''
  
'''ExampleSlide 10'''
+
{
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| A '''class''' is said to be a''' concrete class,''' if all the '''methods''' in that '''class''' are''' concrete methods'''.
+
  
 +
System.''out''.println("Name:"+name);
  
The '''class Employee''' illustrated here, is a''' concrete class'''.  
+
System.''out''.println("Age:"+age);
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Next we will see about '''concrete method'''.
  
  
All the '''methods''' inside this '''class''' including''' showDetails()''' are '''concrete'''.
+
A '''Concrete method''' is completely implemented within the curly brackets.
  
|-
+
We will add a''' concrete method''' to this '''class''' to print the name and age.  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us understand the usage of '''Abstract class '''with a sample program.
+
  
|-
+
Type the following code as displayed on the screen.
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In '''Ecilpse IDE''' a project called '''AbstractDemo''' is created,
+
  
  
 +
This '''method showBasicDetails()''' illustrated here, is an example of '''concrete method'''.
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we will switch to''' Eclipse''' and create a new '''project''' called '''AbstractDemo'''.
 
  
 
+
Observe that this '''method''' is implemented completely.
Inside this '''project''', we will create the necessary '''classes''' to demonstrate the usage of''' Abstract class.'''
+
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Create the''' class Person''' in the '''src''' folder.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type
  
 +
'''public void showDetails( )'''<nowiki>;</nowiki>
  
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now, right click on '''src''' folder and click '''new-'''> '''class'''.
+
Point to the error
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we will add an '''abstract method''' to this '''class'''.
  
  
Type the name of the '''class''' as '''Person''' and press '''Enter'''.
+
So type '''public void showDetails( )''' semicolon.
  
|-
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
 
Type
 
  
'''String name;'''
+
An error comes up, since we have not yet added the''' abstract  keyword. '''
 
+
'''int age;'''
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|Now we will add the fields to represent the name and age of the''' Person'''.
+
 
+
 
+
Type''' String name semicolon.'''
+
 
+
 
+
Also type''' int age semicolon.'''
+
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| click on''' source ->''' and select''' generate constructor using fields'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type the '''keyword abstract'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now click on''' source ->''' and select''' generate constructor using fields. '''
+
 
+
  
Delete the '''super '''keyword from the generated code.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| So now add the '''keyword abstract.'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Constructor''' code
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Point to the error.  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This '''constructor''' can initialise the values of '''name''' and '''age '''fields.
+
  
|-
+
Type the '''keyword abstract'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type
+
  
'''public void showBasicDetails( )'''
 
  
{
+
'''Highlight in the video'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we can see another error comes up.
  
System.''out''.println("Name:"+name);
 
  
System.''out''.println("Age:"+age);
+
This is because, '''abstract methods''' can be added only to '''abstract classes'''.
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We will add a''' concrete method''' to this '''class''' to print the name and age.  
+
  
So type '''public void showBasicDetails( ).'''
 
  
Within '''brackets''' type,
+
So now add the''' keyword abstract''' to the''' Person class '''to make it an''' abstract class.'''
  
'''System.out.println''' within round brackets and within quotes type''' Name colon''' plus''' name semicolon'''
 
  
 +
The '''class Person''' illustrated here is an''' abstract class.'''
  
Also type
 
  
'''System.out.println''' within round brackets and within quotes''' Age colon''' plus''' age semicolon'''
+
It contains an''' abstract method''' called''' showDetails()'''.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 6:'''
  
'''public void showDetails( );'''
+
'''Abstract Class - Example'''
 +
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The figure here represents an '''inheritance relation'''.
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we will add an '''abstract method''' to this '''class'''.
+
Here, the''' Person class''' is an''' abstract class.'''
  
 +
The''' Employee class''' and the '''Student class''' are '''subclasses''' of the''' Person class'''.
  
So type '''public void showDetails( ) semicolon'''
+
These '''subclasses''' can provide their own different implementations.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Go back to''' eclipse '''and point the '''showDetails() '''method
Point to the error
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| An error comes up, since we have not yet added the''' abstract '''keyword.
+
  
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type the keyword''' abstract'''
 
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| So now, add the keyword '''abstract.'''
 
  
|-
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| These are done by''' showDetails( ) method''' present in the''' Person class.'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Point to the error
+
  
 +
For example:
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we can see, another error comes up.
+
'''ShowDetails() Method''' in the''' Employee class''' prints the '''Employee ID''' and the''' Salary''', where as
  
 
+
'''ShowDetails() Method''' in the''' Student class''' prints the '''Student Reg No''' and '''Grade.'''
This is because, '''abstract methods''' can be added only to '''abstract classes'''.
+
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|Type the keyword''' abstract'''
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Right-click on the '''default package''' and create another''' class''' called''' Employee.'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|So now, add the keyword''' abstract''' to the''' Person class '''to make it an''' abstract class.'''
+
  
|-
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Right click on the '''default package''' and create another''' class''' called''' Employee.'''
 
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Then right click on the '''default package''' and create another''' class''' called''' Employee.'''
+
Type''' extends Person'''
 +
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Then right-click on the '''default package''' and create another''' class''' called''' Employee.'''
  
|-
+
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type''' extends Person'''
+
Now to make this a '''subclass''' of '''Person class, '''type''' extends Person'''.
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now to make this a '''subclass''' of '''Person class, '''type''' extends Person'''.
+
  
 
|-
 
|-
Line 291: Line 223:
  
  
It indicates that, we should provide an implementation to the '''abstract method showDetails( )'''.
+
It indicates that, we should provide an '''implementation''' to the '''abstract method showDetails( )'''.
  
  
Line 308: Line 240:
  
  
So type
+
So type''' String empid semicolon'''
*''' String empid semicolon'''
+
 
*and''' int salary semicolon'''
+
and''' int salary semicolon'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| click on '''source'''-> and select''' generate constructor using fields'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| click on '''source'''-> and select''' generate constructor using fields'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now click on '''source'''-> and then select''' generate constructor using fields.'''  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now click on '''Source'''-> and then select''' Generate constructor using fields.'''  
  
  
Line 321: Line 253:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type
 
  
 
'''public void''' showDetails()
 
'''public void''' showDetails()
Line 337: Line 268:
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us now define the''' showDetails method'''.
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us now define the''' showDetails method'''.
  
So type''' public void showDetails( )'''
+
So type''' public void showDetails( ).'''
 
+
  
 
Inside this '''method''', we need to print the employee details.
 
Inside this '''method''', we need to print the employee details.
  
So type
+
Type the following code as displayed on the screen.  
 
+
'''System.out.println''' within quotes''' Emp id colon''' plus''' empid semicolon'''
+
 
+
 
+
'''System.out.println''' within quotes''' Salary colon''' plus''' salary semicolon'''
+
  
  
Line 362: Line 287:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight the''' regno''' and''' grade''' fields
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight the''' regno''' and''' grade''' fields
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| There are two fields in the '''Student class, regno''' and''' grade''' which represent '''student reg. no''' and '''grade'''.
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| There are two fields in the '''Student class - regno''' and''' grade''' which represent student reg. no and grade.
  
 
|-
 
|-
Line 379: Line 304:
 
{
 
{
  
System.out.println("Student regno:"+regno);
 
  
System.out.println("Student grade:"+grade);
+
System.''out''.println("Student regno:"+regno);
 +
 
 +
System.''out''.println("Student grade:"+grade);
  
 
}
 
}
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The''' showDetails method''' is also implemented in this '''class'''.  
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The''' showDetails method''' is also implemented in this '''class'''.  
  
 +
It prints the values of''' Student Reg. Number''' and''' grade'''
  
  
  
It prints the values of''' Student Reg. Number''' and''' grade'''
 
  
 
|-
 
|-
Line 402: Line 328:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Right click on the '''default package, '''click '''new'''-> '''class''' and then type name as''' Demo'''.
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Right click on the '''default package, '''click '''new'''-> '''class''' and then type name as''' Demo'''.
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now right click on the '''default package.'''  
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now right-click on the '''default package.'''  
  
Click on '''new'''-> '''class''' and then type name as''' Demo.'''
+
Click on '''New'''-> '''Class''' and then type name as''' Demo. '''
  
  
Inside this '''class''', we will have the '''main method''',
+
Inside this '''class''' we will have the '''main method''',
  
  
So type '''main''' and then press '''Ctrl space''' to generate the''' main method.'''
+
So type '''main''' and then press ctrl+space to generate the''' main method.'''
  
 
|-
 
|-
Line 417: Line 343:
 
Person p =new Person(“John”);
 
Person p =new Person(“John”);
  
 +
 +
Point to the error
 +
 +
 +
Remove the line
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us try to instantiate the''' Person class''' by typing
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us try to instantiate the''' Person class''' by typing
  
Line 422: Line 353:
  
  
Within brackets and double quotes, type '''John''' and put a '''semicolon'''.
+
Within brackets and double quotes type '''John''' and put a semicolon.
  
|-
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Point to the error
 
  
 
+
Now we can see an error.
Remove the line
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we can see an error.
+
  
  
Line 466: Line 393:
  
  
In the first line, we are passing the values of different '''arguments'''.
+
In the first line, we are passing the values of different arguments.
  
*'''John''' is passed as '''Employee name''',
 
  
*'''40''' as the value of '''ageE267''' as the value of '''Employee ID''' and
+
'''John''' is passed as '''Employee name'''.
  
*'''10000''' as the value of '''Employee salary.'''
+
'''40''' as the value of '''age.'''
 +
 
 +
'''E267''' as the value of '''Employee ID''' and
 +
 
 +
'''10000''' as the value of '''Employee salary.'''
  
  
Line 481: Line 411:
  
  
Similarly, instantiate the''' Person class''' using the''' Student class'''.
+
Similarly instantiate the''' Person class''' using the''' Student class'''.
 +
 
  
 
This is represented as '''Person p2 equals new Student.'''
 
This is represented as '''Person p2 equals new Student.'''
Line 488: Line 419:
 
Now we are passing the values of different '''arguments'''.
 
Now we are passing the values of different '''arguments'''.
  
*'''Hari''' is passed as the value of''' student name''',
 
  
*'''20''' as the value of '''age'''.
+
We can invoke the''' showBasicDetails() method '''and''' showDetails()''' '''method '''using the '''object''' as shown.
  
*'''12005''' as the value of''' student Reg No.''' and
 
 
*'''A''' as the value of '''grade'''.
 
 
 
Now we can call the''' showBasicDetails() method '''using the '''object p2''' as '''p2.showBasicDetails()'''
 
 
 
We can also invoke the''' showDetails() method''' using the '''object p2''' as '''p2.showDetails()'''
 
  
 
|-
 
|-
Line 517: Line 438:
  
  
Other Employee details like''' employee ID '''and '''salary '''are printed by the''' showDetails() method'''.
+
Other employee details like''' employee ID '''and '''salary '''are printed by the''' showDetails() method'''.
  
  
Similarly, the basic student details like '''name''' and '''age''' are printed by''' showBasicDetails() method.'''
+
Similarly the basic student details like '''name''' and '''age''' are printed by''' showBasicDetails() method.'''
  
  
Other details of the student like
+
Other details of the student like '''Student reg no '''and '''grade''' are printed by the''' showDetails() method'''.
 
+
'''Student reg no '''and '''grade''' are printed by the''' showDetails() method'''.
+
  
 
|-
 
|-
Line 532: Line 451:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 12:'''
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 7:'''
  
 
'''Summary'''
 
'''Summary'''
Line 542: Line 461:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 13:'''
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 8:'''
  
 
'''Assignment'''
 
'''Assignment'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As an assignment-
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As an''' '''assignment
 +
 
 +
 
 +
# Create an '''abstract class Vehicle''' which contains an '''abstract method''' '''run()'''
 +
# Create a'''subclass Car''' which extends the '''Vehicle class''' and implements the '''run method''' that prints
 +
 
 +
'''“Car is running on 4 wheels”'''
 +
 
 +
# Also create a'''subclass Bike''' which again extends the '''Vehicle class''' and implements the '''run method''' that prints
 +
 
 +
'''“Bike is running on 2 wheels”'''
  
# Create an '''abstract class Vehicle''' which contains an '''abstract method run()'''
 
# Create a '''subclass Car''' which extends the '''Vehicle class''' and implements the '''run method''' that prints '''“Car is running on 4 wheels”'''
 
# Also create a'''subclass Bike''' which again extends the '''Vehicle class''' and implements the '''run method''' that prints '''“Bike is running on 2 wheels”'''
 
 
# Also create a '''Demo class '''containing the''' main method''' to verify the results.
 
# Also create a '''Demo class '''containing the''' main method''' to verify the results.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''About Project'''
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''About Project'''
 +
 +
  
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The video at the following link summarizes the''' '''Spoken Tutorial Project.
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The video at the following link summarizes the''' '''Spoken Tutorial Project.
 +
  
 
Please download and watch it.
 
Please download and watch it.
Line 568: Line 497:
 
* Conducts workshops using spoken tutorials
 
* Conducts workshops using spoken tutorials
 
* Gives certificates on passing the online tests
 
* Gives certificates on passing the online tests
 +
  
 
For more details, please write to us.
 
For more details, please write to us.
Line 573: Line 503:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''About NMEICT'''
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''About NMEICT'''
 +
  
  
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
 +
  
 
More information on this Mission is available at the link shown.
 
More information on this Mission is available at the link shown.
Line 585: Line 517:
  
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This script has been contributed by:
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This script has been contributed by:
Dept. of Information Technology, Amal Jyothi College of Engineering.
+
 
 +
Dept. of Information
 +
 
 +
Technology, Amal Jyothi College of Engineering.
  
  

Latest revision as of 14:41, 12 January 2017

Title of script: Abstract Classes

Author: Joms Antony

Keywords: Abstract Methods, Concrete Methods, Abstract Classes, Concrete Classes, Inheritance, Method Implementation, Video tutorial, java tutorial


Visual Cue
Narration
Slide 1: Welcome to the Spoken Tutorial on Abstract Classes.
Slide 2:

Learning Objectives


In this tutorial we will learn about:
  • Abstract Methods and Concrete Methods
  • Abstract Classes and Concrete Classes and
  • How to use Abstract Classes
Slide 3:

Software Requirements


For this tutorial, I am using
  • Ubuntu 12.04
  • JDK 1.7 and
  • Eclipse 4.3.1
Slide 4:

Prerequisites


To follow this tutorial, you should have knowledge of basics of Java and Eclipse IDE.

You should also have the knowledge of subclassing in Java.

If not, for relevant Java tutorials, please visit the link shown.

First we will see about Abstract Method.
Slide 5:

What is an Abstract Method?

An Abstract method is a method that is declared without implementation.


It is declared using abstract keyword.


There should not be opening and closing parenthesis for this method.

Let us understand the usage of Abstract class with a sample program.
In Ecilpse IDE a project called AbstractDemo is created,


Now we will switch to Eclipse and create a new project called AbstractDemo.


Inside this project we will create the necessary classes to demonstrate the usage of Abstract class.

Create the class Person in the src folder.


Type

String name;

int age;

Now, right click on src folder and click New-> Class.


Type the name of the class as Person and press Enter.


Now we will add the fields to represent the name and age of the Person.


Type String name semicolon.


Also type int age semicolon.

Click on source -> and select generate constructor using fields Now click on Source -> and select Generate constructor using fields.


Delete the super keyword from the generated code.

Constructor code This constructor can initialise the values of name and age fields.
Type

public void showBasicDetails( )

{

System.out.println("Name:"+name);

System.out.println("Age:"+age);

Next we will see about concrete method.


A Concrete method is completely implemented within the curly brackets.

We will add a concrete method to this class to print the name and age.

Type the following code as displayed on the screen.


This method showBasicDetails() illustrated here, is an example of concrete method.


Observe that this method is implemented completely.

Type

public void showDetails( );


Point to the error

Now we will add an abstract method to this class.


So type public void showDetails( ) semicolon.


An error comes up, since we have not yet added the abstract keyword.

Type the keyword abstract So now add the keyword abstract.
Point to the error.

Type the keyword abstract


Highlight in the video

Now we can see another error comes up.


This is because, abstract methods can be added only to abstract classes.


So now add the keyword abstract to the Person class to make it an abstract class.


The class Person illustrated here is an abstract class.


It contains an abstract method called showDetails().

Slide 6:

Abstract Class - Example

The figure here represents an inheritance relation.

Here, the Person class is an abstract class.

The Employee class and the Student class are subclasses of the Person class.

These subclasses can provide their own different implementations.

Go back to eclipse and point the showDetails() method


These are done by showDetails( ) method present in the Person class.

For example:

ShowDetails() Method in the Employee class prints the Employee ID and the Salary, where as

ShowDetails() Method in the Student class prints the Student Reg No and Grade.

Right-click on the default package and create another class called Employee.


Type extends Person

Then right-click on the default package and create another class called Employee.


Now to make this a subclass of Person class, type extends Person.

Point to the error message Now we can see an error comes up in the Eclipse IDE.


It indicates that, we should provide an implementation to the abstract method showDetails( ).


We will do it a little later.

Type

String empid semicolon;

int salary;


Now create two fields to represent the employee id and employee salary.


So type String empid semicolon

and int salary semicolon

click on source-> and select generate constructor using fields Now click on Source-> and then select Generate constructor using fields.


This constructor can initialise the values of name, age, empid and salary.

Type

public void showDetails()

{

System.out.println("Emp id"+empid);

System.out.println("Salary:"+salary);

}


Let us now define the showDetails method.

So type public void showDetails( ).

Inside this method, we need to print the employee details.

Type the following code as displayed on the screen.


Note that the error disappears, once the showDetails() method is implemented.

Next we will see a Student class of the project.
Show and highlight the code for Student Class. I have already created a subclass called Student.
Highlight the regno and grade fields There are two fields in the Student class - regno and grade which represent student reg. no and grade.
Highlight constructor A constructor is also created inside this class.


This constructor can be used to initialize the values for name, age, regno and grade.

Highlight the code


public void showDetails()

{


System.out.println("Student regno:"+regno);

System.out.println("Student grade:"+grade);

}

The showDetails method is also implemented in this class.

It prints the values of Student Reg. Number and grade



Highlight showDetails() method of Employee class

Highlight showDetails() method of student class

Now note that the Employee class has its own implementation of showDetails().


And the Student class has its own implementation of showDetails().

Right click on the default package, click new-> class and then type name as Demo. Now right-click on the default package.

Click on New-> Class and then type name as Demo.


Inside this class we will have the main method,


So type main and then press ctrl+space to generate the main method.

Type

Person p =new Person(“John”);


Point to the error


Remove the line

Now let us try to instantiate the Person class by typing

Person p equals new Person.


Within brackets and double quotes type John and put a semicolon.


Now we can see an error.


This is because the Person class is abstract and it cannot be instantiated.


Let's remove this line.

Highlight the code line by line


Person p1=

new Employee("John",40,"E267",10000);

p1.showBasicDetails();

p1.showDetails();


Highlight the code line by line


Person p2=

new Student("Hari",20,"12005",'A');

p2.showBasicDetails();

p2.showDetails();

Type the following code as displayed on the screen.


Now let us instantiate the Person class using the Employee class as Person p1 equals new Employee.


In the first line, we are passing the values of different arguments.


John is passed as Employee name.

40 as the value of age.

E267 as the value of Employee ID and

10000 as the value of Employee salary.


Now we can invoke the concrete method in the Person class as p1.showBasicDetails()


We can also call the showDetails() method using the object p1 as p1.showDetails()


Similarly instantiate the Person class using the Student class.


This is represented as Person p2 equals new Student.


Now we are passing the values of different arguments.


We can invoke the showBasicDetails() method and showDetails() method using the object as shown.


Right click on the class Demo and then select Run as -> Java Application Now let us run this Demo program.


So right click on the class Demo and then select Run as -> Java Application

Highlight code and the output We can see the output with the basic employee details like name and age.


These are printed by showBasicDetails() method.


Other employee details like employee ID and salary are printed by the showDetails() method.


Similarly the basic student details like name and age are printed by showBasicDetails() method.


Other details of the student like Student reg no and grade are printed by the showDetails() method.

This brings us to the end of this tutorial. Let us summarize.
Slide 7:

Summary

In this tutorial we have learnt about
  • Abstract Methods and Concrete Methods
  • Abstract Classes and Concrete Classes and
  • How to create and use Abstract Classes
Slide 8:

Assignment

As an assignment


  1. Create an abstract class Vehicle which contains an abstract method run()
  2. Create asubclass Car which extends the Vehicle class and implements the run method that prints

“Car is running on 4 wheels”

  1. Also create asubclass Bike which again extends the Vehicle class and implements the run method that prints

“Bike is running on 2 wheels”

  1. Also create a Demo class containing the main method to verify the results.
About Project


The video at the following link summarizes the Spoken Tutorial Project.


Please download and watch it.

About Workshops


The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates on passing the online tests


For more details, please write to us.

About NMEICT


Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.


More information on this Mission is available at the link shown.

Contributor slide


This script has been contributed by:

Dept. of Information

Technology, Amal Jyothi College of Engineering.


This is Priya from IIT Bombay signing off. Thank you for joining.

Contributors and Content Editors

Nancyvarkey, Priyacst