Difference between revisions of "Java/C3/Java-Interfaces/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 1: Line 1:
'''Title of script:''' Java Interfaces
+
'''Title of script:''' Interfaces
  
 
'''Author:''' Joms Antony
 
'''Author:''' Joms Antony
  
'''Keywords:''' Java Interfaces, Implementation Classes, implements keyword, abstract method, video tutorial, Java tutorial
+
'''Keywords:''' Interface, Implementation Classes,implements keyword, video tutorial
 +
 
  
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
| style="background-color:#ffffff;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <center>'''Visual Cue'''</center>
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <center>'''Visual Cue'''</center>
| style="background-color:#ffffff;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <center>'''Narration'''</center>
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| <center>'''Narration'''</center>
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 1:'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 1:'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Welcome to the''' Spoken Tutorial''' on''' Java Interfaces.'''
+
 
 +
 
 +
 
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Welcome to the''' Spoken Tutorial''' on''' Java Interfaces'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 2:'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 2:'''
  
 
'''Learning Objectives'''
 
'''Learning Objectives'''
Line 21: Line 25:
  
  
| 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 #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial we will learn about:
 +
 
 
* Creating an''' interface'''
 
* Creating an''' interface'''
* Creating an''' Implementation class''' and
+
* Creating an''' Implementation classes''' and
* Usage of''' Interfaces'''
+
* How to use''' Interface'''
 +
 
 +
 
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 3:'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 3:'''
  
 
'''Software Requirements'''
 
'''Software Requirements'''
Line 33: Line 40:
  
  
| 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 #000001;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
 
* '''Eclipse 4.3.1'''
 
* '''Eclipse 4.3.1'''
 +
 +
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 4:'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 4:'''
  
 
'''Prerequisites'''
 
'''Prerequisites'''
Line 46: Line 55:
  
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To follow this tutorial, you should have knowledge of basics of''' Java''' and''' Eclipse IDE.'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To follow this tutorial, you should have knowledge of basics of''' Java''' and''' Eclipse IDE.'''
  
 
You should also have the knowledge of''' subclassing''' and''' Abstract classes '''in''' Java.'''
 
You should also have the knowledge of''' subclassing''' and''' Abstract classes '''in''' Java.'''
  
If not, for relevant''' Java''' tutorials, please visit the link shown.
+
If not, for relevant''' Java''' tutorials please visit the link shown.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
+
| style="border:1pt solid #000001;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;"| First let's understand what is an '''interface'''.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| First let's understand what is an '''interface'''.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 5:'''
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 5:'''
  
 
'''What is an interface?'''
 
'''What is an interface?'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| An''' Interface''' contains a set of''' abstract methods''' and''' static data members'''.
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| An''' Interface''' contains a set of''' abstract'''
  
 +
'''methods''' and''' static data members'''.
  
It defines the signatures of a set of''' methods,''' without the body.
+
 
 +
It defines the signatures of a set of '''methods,''' without the body.
  
  
 
It is declared using the '''interface''' keyword.
 
It is declared using the '''interface''' keyword.
  
|-
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 6:'''
 
  
'''Implementing the interface'''
 
  
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In Eclipse IDE a project called Interface'''Demo''' is created,
  
| style="background-color:#ffffff;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| A''' class''' can implement an''' interface''' using the''' implements''' keyword.
 
  
  
A''' class''' can implement more than one '''interface.'''
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;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''' InterfaceDemo'''.
  
  
All the''' abstract methods''' defined in the''' interface''' or''' interfaces''' must be implemented in such '''classes'''.
+
Here we will create the necessary''' classes''' and''' interface''' to demonstrate the usage of''' interfaces.'''
  
 
|-
 
|-
| style="border: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 #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The''' interface Animal''' is created in the''' src''' folder.
| style="background-color:#ffffff;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us explore the differences between an''' interface''' and an''' abstract''' class.
+
  
|-
+
Code is copied
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 7:'''
+
  
'''Interface Vs Abstract Classes'''
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| All the methods in an''' interface''' should be''' abstract.'''
 
  
  
Within''' interface''' there should not be any
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now, right click on''' src''' folder and click''' new-'''>''' interface'''.
  
* '''constructors'''
 
* '''concrete methods'''
 
* '''static methods '''and
 
* '''main method'''
 
  
But an''' abstract class''' can have all these inside it.
+
Type the name as''' Animal''' and press '''enter'''.
  
  
The variables in an''' interface''' should be''' static''' and''' final.'''
+
Note that''' “interface”''' keyword is used for defining an interface.
  
  
There are no such restrictions for an''' abstract class'''.
+
Now type the 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;"|
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight the code
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Next we will see an example for an '''interface'''.
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here the name of the interface is''' Animal.'''
 
+
|-
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 8:'''
+
 
+
'''Interface:'''
+
 
+
'''Example:'''
+
 
+
 
+
Highlight the''' methods'''
+
 
+
 
+
 
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here the '''interface''' name is''' Animal.'''
+
  
  
It contains three '''abstract methods talk(), see()''' and''' move().'''
+
It contains three abstract methods''' talk(), see()''' and''' move().'''
  
  
All such '''methods''' in an '''interface''' are implicitly''' public''' and''' abstract'''.
+
All such methods in an interface are implicitly''' public''' and''' abstract'''.
  
  
Line 146: Line 133:
  
 
|-
 
|-
| style="border: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 #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;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;"| Next, we will see how to create an '''implementation class''' for an''' interface''' with an example.
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Next we will see '''implementation class''' for an''' interface''' through an example.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 9:'''
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 9:'''
  
 
'''Implementation class:'''
 
'''Implementation class:'''
  
 
'''Example:'''
 
'''Example:'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here''' Human''' is a''' class''' which implements the''' Animal interface.'''
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here''' Human''' is a''' class''' which implements the''' Animal interface.'''
 +
 
 +
 
 +
So it must provide implementations for the methods''' talk(), see()''' and''' move().'''
 +
 
  
  
So it must provide implementations for the '''methods talk(), see()''' and''' move().'''
 
  
 
|-
 
|-
| 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 #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 10:'''
  
 
'''Implementing multiple interfaces'''
 
'''Implementing multiple interfaces'''
Line 169: Line 159:
  
 
Highlight the''' syntax''' and the usage of''' comma operator'''
 
Highlight the''' syntax''' and the usage of''' comma operator'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| A '''class''' can also implement''' multiple interfaces'''.
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| A class can also implement''' multiple interfaces'''.
  
As shown in the example, the''' class Human '''implements two''' interfaces Animal''' and''' Action.'''
+
As shown in the example, the''' class Human implements''' two''' interfaces Animal''' and''' Action.'''
  
 
Note that a''' comma operator''' used in the syntax is to identify the different''' interfaces.'''
 
Note that a''' comma operator''' used in the syntax is to identify the different''' interfaces.'''
Line 178: Line 168:
  
 
|-
 
|-
| 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 #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 11:'''
  
 
'''Interface Usage - Example'''
 
'''Interface Usage - 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 '''implements''' relation.
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The figure here represents an implement relation.
  
 
The''' Animal class''' is an''' interface.'''
 
The''' Animal class''' is an''' interface.'''
  
The''' Human '''and''' Snake classes''' are the two '''implementation classes'''.
+
The''' Human class''' and''' Snake class''' are the two implementation classes.
  
The '''Human class''' provides its own different implementations for''' talk(), see()''' and''' move() methods.'''
+
The '''Human class''' provides its own different implementations for''' talk()''',''' see()''' and''' move() methods.'''
  
And the '''Snake class''' provides its own different implementations for''' talk(), see()''' and''' move() methods.'''
+
The '''Snake class''' provides its own different implementations for '''talk(), see()''' and '''move() methods'''.
  
 
|-
 
|-
| style="border: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 #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;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 '''interfaces '''with a sample program.
+
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us understand the usage of interfaces''' '''with a sample program.
  
 
|-
 
|-
| 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 Interface'''Demo''' is created,
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us explore the differences between an''' interface''' and an''' abstract''' class.
  
 +
|-
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 7:'''
  
 +
'''Interface Vs Abstract Classes'''
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| All the methods in an''' interface''' should
  
| 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''' InterfaceDemo'''.
+
be''' abstract.'''
  
  
Here we will create the necessary''' classes''' and''' interface''' to demonstrate the usage of''' interfaces.'''
+
Within''' interface''' there should not be any
  
|-
+
* '''constructors'''
| style="background-color:#ffffff;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The''' interface Animal''' is created in the''' src''' folder.
+
* '''concrete methods'''
 +
* '''static methods and'''
 +
* '''main method'''
  
Code is copied
+
But an''' abstract''' class can have all these inside it.
| style="background-color:#ffffff;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-> interface'''.
+
  
  
Type the name as''' Animal''' and press '''Enter'''.
+
The variables in an''' interface''' should
  
 +
be''' static''' and''' final.'''
  
Note that the '''“interface”''' keyword is used for defining an '''interface'''.
 
  
 
+
There are no such restrictions for an''' abstract''' class.
Now type the 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;"|  
+
| style="border:1pt solid #000001;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;"| Next let us create the''' implementation classes.'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us understand the usage of '''interfaces''' with a sample program.
  
 
|-
 
|-
| 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 a''' class''' called''' Human.'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Right click on the default package and create a''' class''' called''' Human.'''
  
  
Type''' implements Animal'''
+
Type''' implements Living Thing'''
| 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 a''' class''' called''' Human.'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Right click on the '''default package''' and create a''' class''' called''' Human.'''
 +
 
 +
 
 +
Now, to make this an implementation class of''' Animal, '''type''' implements Animal'''
 +
 
  
  
Now to make this an implementation class of''' Animal, '''type''' implements Animal.'''
 
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Point to the error message
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Point to the error message
| 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 comes up in the''' Eclipse IDE.'''  
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now we can see an error comes up in the''' Eclipse IDE.'''  
  
  
 
This error indicates that, we should provide an implementations to the '''Animal interface.'''
 
This error indicates that, we should provide an implementations to the '''Animal interface.'''
 +
 +
 +
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
+
| style="border:1pt solid #000001;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 see how to rectify this error.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Let us see how to rectify this error.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| public void talk(){
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| public void talk(){
  
System.out.println("I am a Human, I belong to the "+Animal.type1+" Family");
+
System.out.println("I am a Human, I belong to the "+Animal.type1+" family");
  
 
}
 
}
Line 255: Line 256:
 
public void see(){
 
public void see(){
  
System.out.println("I CAN SEE ALL COLORS ");
+
System.out.println("I can see all colors ");
  
 
}
 
}
Line 262: Line 263:
 
public void move(){
 
public void move(){
  
System.out.println("I MOVE BY WALKING");
+
System.out.println("I move by walking");
  
 
}
 
}
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us define the''' methods talk(), see()''' and''' move().'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us define the''' methods talk(), see()''' and''' move().'''
 
+
So type''' public void talk( )'''
+
  
 +
So type''' public void talk( ).'''
  
Within curly brackets type
+
within curly brackets type
  
'''System.out.println''' within quotes''' I am a human and I belong to'''
+
'''System.''out''.println''' within quotes''' I am a human I belong to'''
  
  
Line 278: Line 278:
  
  
So type '''+ Animal.type1+''' within quotes''' family semicolon'''.
+
So type
  
 +
'''+ Animal.type1+''' within quotes''' family '''semicolon'''.'''
  
Let us now implement the '''see() method'''
 
  
 +
Let us now implement the see() method
  
So type''' public void see( )'''
 
  
 +
So type''' public void see( ).'''
  
Within curly brackets type
+
within curly brackets type
  
'''System.out.println''' within quotes''' I can see all colors semicolon'''
+
'''System.''out''.println''' within quotes''' I can see all colors''' semicolon
  
  
We should also define the''' move() method'''
+
We should also define the''' move()''' method
  
  
So type''' public void move( )'''
+
So type''' public void move( ).'''
  
 +
within curly brackets type
  
Within curly brackets type
+
'''System.''out''.println''' within quotes''' I move by walking''' semicolon
 +
 
 +
 
 +
Note that the error disappears, once all the methods are implemented.
  
'''System.out.println''' within quotes''' I move by walking semicolon'''
 
  
  
Note that the error disappears, once all the '''methods''' are implemented.
 
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
+
| style="border:1pt solid #000001;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;"| Next we will see how to define the''' Snake class.'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Next we will see how to define the''' Snake class.'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Show and highlight the code for Snake Class.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Show and highlight the code for Snake Class.
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| I have already created it in my project.  
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| I have already created it in my project.
  
  
Line 317: Line 320:
  
  
Now let us go through the code.
+
Now let us go through the code.  
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Highlight the code'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Highlight the code'''
  
  
Line 332: Line 335:
 
public void see(){
 
public void see(){
  
System.out.println("I CAN SEE ONLY IN BLACK AND WHITE");
+
System.out.println("I can see only in black and white");
  
 
}
 
}
Line 338: Line 341:
 
public void move(){
 
public void move(){
  
System.out.println("I MOVE BY CRAWLING");
+
System.out.println("I move by crawling");
  
 
}
 
}
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We can see that all the '''methods''' of the''' Animal interface talk(), see()''' and''' move()''' are implemented inside this '''class'''.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We can see that all the methods of the''' Animal interface talk()''' ,''' see()''' and''' move()''' are implemented inside this class.
 
+
 
+
Here the''' talk() method''' prints “'''I am a snake and I belong to'''
+
  
 +
Here the''' talk() method''' prints “'''I am a snake and I belong to”'''
  
 
Then the value of''' Animal.type2''' is to be printed and then''' “family”'''
 
Then the value of''' Animal.type2''' is to be printed and then''' “family”'''
Line 351: Line 352:
  
 
Here the''' see() method''' prints “'''I can see only in black and white”'''
 
Here the''' see() method''' prints “'''I can see only in black and white”'''
 
  
 
The''' move() method''' prints''' "I move by crawling"'''
 
The''' move() method''' prints''' "I move by crawling"'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight the''' talk(), see()''' and''' move()''' methods of''' Human class'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight the''' talk(), see()''' and''' move()''' methods of''' Human class'''
  
 
Highlight the''' talk(), see()''' and''' move()''' methods of''' Snake class'''
 
Highlight the''' talk(), see()''' and''' move()''' methods of''' Snake class'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Note that the''' Human class''' has its own implementations of''' talk(), see()''' and''' move() methods'''.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Note that the''' Human''' class has its own implementations of''' talk(), see()''' and''' move()''' methods.
  
  
And the''' Snake class''' has its own implementations of''' talk(), see()''' and''' move() methods'''.
+
And the''' Snake''' class has its own implementations of''' talk(), see()''' and''' move()''' methods
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| click new-> class and then type name as''' Demo'''.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| 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''' click''' new-> class''' and then type name as''' Demo.'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now right click on the default package click''' 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''',
  
  
Line 375: Line 375:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight the code line by line
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight the code line by line
 
+
 
+
Highlight the second line
+
  
  
Line 391: Line 388:
  
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type the following code as displayed on the screen.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type the following code as displayed on the screen.
  
  
In this line, we instantiate the''' Human class''' using the''' Animal interface.'''
+
In this line we instantiate''' Human class''' using the''' Animal interface.'''
  
  
This is represented as '''Animal h equals new Human();'''
+
This is represented as
  
  
Now we can invoke the different '''methods''' using this object as
+
'''Animal h''' equals''' new Human()'''<nowiki>;</nowiki>
 +
 
 +
 
 +
Now we can invoke the different methods using this object as
  
  
Line 408: Line 408:
  
 
'''h.move();'''
 
'''h.move();'''
 +
 +
 +
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight the code
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight the code
  
  
Line 420: Line 423:
  
 
s.move();
 
s.move();
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Next, we instantiate the''' Snake class''' using the''' Animal interface.'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Next, we instantiate the''' Snake class''' using the''' Animal interface'''
  
  
This is represented as '''Animal s equals new Snake()'''
+
Now we can invoke the different methods using this object as shown
  
  
Now we can invoke the different '''methods''' using this object as
 
  
 
'''s.talk();'''
 
 
'''s.see();'''
 
 
'''s.move();'''
 
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
+
| style="border:1pt solid #000001;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;"| Now let us run this''' Demo''' program.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now let us run this''' Demo''' program.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Right click on the'' class'' Demo and then select''' Run as''' ->''' Java Application'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Right click on the'' class'' Demo and then select''' Run as''' ->''' Java Application'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| So right click on the'' '''''class Demo''' and then select''' Run as -> Java Application.'''
+
  
|-
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight code and the output
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We can see the output as
 
  
  
'''I am a human and I belong to Mammal family'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| So right click on the'' class''''' Demo''' and then select''' Run as''' ->''' Java Application'''
  
  
'''I can see all colors''' and
 
  
  
'''I move by walking'''
+
|-
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight code and the output
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We can see the output as
  
  
These are printed by the''' talk(), see()''' and''' move() methods''' invoked using the''' Human class object h.'''
+
These are printed by the''' talk(), see()''' and''' move() methods''' invoked by the''' human class object s'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight code and the output
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Highlight code and the output
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We can also see in the output
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| These are printed by the''' talk(), see()''' and''' move() methods''' invoked by the''' Snake class object s'''
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now lett us explore the differences between an''' interface''' and an''' abstract''' class.
  
'''I am a snake and I belong to Reptiles family'''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 7:'''
  
 +
'''Interface Vs Abstract Classes'''
 +
| style="border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| All the methods in an''' interface''' should
  
'''I can see only in black and white'''
+
be''' abstract.'''
  
  
and '''I move by crawling'''
+
Within''' interface''' there should not be any
  
 +
* '''constructors'''
 +
* '''concrete methods'''
 +
* '''static methods '''and
 +
* '''main method'''
  
These are printed by the''' talk(), see()''' and''' move() methods''' invoked using the''' Snake class object s.'''
+
But an''' abstract''' class can have all these inside it.
 +
 
 +
 
 +
The variables in an''' interface''' should be''' static''' and''' final.'''  
 +
 
 +
 
 +
There are no such restrictions for an '''abstract''' class.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|  
+
| style="border:1pt solid #000001;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;"| This brings us to the end of this tutorial. Let us summarize.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This brings us to the end of this tutorial. Let us summarize.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 10:'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 10:'''
  
 
'''Summary'''
 
'''Summary'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial we have learnt about
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| In this tutorial we have learnt about
  
 
* Creating an''' Interface'''
 
* Creating an''' Interface'''
* Creating an''' implementation class '''and
+
* Creating an''' implementation class'''
 
* Usage of''' interfaces'''
 
* Usage of''' interfaces'''
 +
 +
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 11:'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Slide 11:'''
  
 
'''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 #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As an''' assignment'''
  
  
Create an '''interface Vehicle''' which contains the methods''' brake()''' and''' run().'''
+
Create an interface''' Vehicle''' which
  
 +
contains the methods''' brake()''' and''' run()'''
  
Create another '''interface Fuel''' which contains the following methods.
 
  
 +
Create another interface''' Fuel''' which
  
'''fill(String type, int quantity)'''
+
contains the following methods .
  
'''pay(int quantity, int price)'''
+
 
 +
'''fill(String type,int quantity)'''
 +
 
 +
'''pay(int quantity,int price)'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Assignment'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Assignment'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Create a subclass''' Car''' which implements both the '''interfaces Vehicle''' and''' Fuel.'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Create a subclass''' Car''' which implements both interfaces''' Vehicle''' and''' Fuel.'''
 +
 
 +
 
 +
Here''' brake method''' must print''' ”Car'''
 +
 
 +
'''Applies Power brake”''' and the run
 +
 
 +
method must print''' ”Car is running on 4'''
 +
 
 +
'''wheels”'''
 +
 
  
  
Here''' brake method''' should print''' ”Car, Applies Power brake”''' and the '''run method''' should print''' ”Car is running on 4 wheels”.'''
 
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Assignment'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Assignment'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Similarly''' fill() method''' can print the type and quantity of the fuel filled.
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Similarly''' fill() method''' can print the
 +
 
 +
details of type and quantity of fuel
 +
 
 +
filled.
  
  
Line 521: Line 549:
  
  
'''pay() method''' can be used to print the price to be paid.
+
'''pay() method''' can be used to print
  
For example: Pay Rs.640
+
price to be paid.
 +
 
 +
 
 +
For example: Pay Rs. 640
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Assignment'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Assignment'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Create another subclass''' Bike''' which again implements both the '''interfaces'''-
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Create another subclass''' Bike''' which
  
'''Vehicle''' and''' Fuel.'''
+
again implements both the interfaces
  
 +
'''Vehicle''' and''' Fuel'''
  
Here '''brake method''' can print ”'''Bike, Applies hand brake”''' and the '''run method''' can print “'''Bike is running on 2 wheels”'''.
 
  
|-
+
Here '''brake method''' can print ”'''Bike'''
| 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, implement the '''fill()''' and '''pay() methods''' as explained earlier.
+
  
 +
'''Applies hand brake”''' and the run
  
Finally, create a '''Demo class''' containing the '''main method''' to verify the results.
+
method can print “'''Bike is running on'''
  
|-
+
'''2 wheels”'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''About Project'''
+
  
  
 +
Next implement the '''fill()''' and '''pay()''' methods as explained earlier.
  
| 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.
+
 
 +
Finally create a '''Demo class''' containing the '''main method''' to verify the results.
 +
 
 +
|-
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''About Project'''
 +
 
 +
(retain the slide as in TEX file)
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| This video at the following link summarizes the''' Spoken Tutorial Project.'''
  
  
Line 552: Line 589:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''About Workshops'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''About Workshops'''
  
 +
(retain the slide as in TEX file)
 +
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The''' Spoken Tutorial Project Team'''
  
 +
• Conducts workshops using spoken tutorials and
  
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The Spoken Tutorial Project Team
+
Gives certificates on passing the online tests.
 
+
*Conducts workshops using spoken tutorials and
+
*Gives certificates on passing the online tests
+
  
  
Line 565: Line 602:
  
 
|-
 
|-
| 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 #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''About NMEICT'''
  
 
+
(retain the slide as in TEX file)
 
+
| style="border:1pt solid #000001;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.
+
  
  
Line 575: Line 611:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Contributor slide'''
+
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Contributor slide'''
 
+
  
 +
(retain the slide as in TEX file)
 +
| style="border:1pt solid #000001;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:
+
'''Deptartment of Information'''
  
Department of Information Technology, Amal Jyothi College of Engineering.
+
'''Technology, Amal Jyothi College of Engineering'''
  
  
This is Priya from IIT Bombay signing off. Thank you for joining.
+
This is Priya from IIT Bombay signing off. Thanks for joining.
  
 
|}
 
|}

Revision as of 12:07, 12 January 2017

Title of script: Interfaces

Author: Joms Antony

Keywords: Interface, Implementation Classes,implements keyword, video tutorial


Visual Cue
Narration
Slide 1:


Welcome to the Spoken Tutorial on Java Interfaces
Slide 2:

Learning Objectives


In this tutorial we will learn about:
  • Creating an interface
  • Creating an Implementation classes and
  • How to use Interface


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 and Abstract classes in Java.

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

First let's understand what is an interface.
Slide 5:

What is an interface?

An Interface contains a set of abstract

methods and static data members.


It defines the signatures of a set of methods, without the body.


It is declared using the interface keyword.



In Eclipse IDE a project called InterfaceDemo is created,


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


Here we will create the necessary classes and interface to demonstrate the usage of interfaces.

The interface Animal is created in the src folder.

Code is copied


Now, right click on src folder and click new-> interface.


Type the name as Animal and press enter.


Note that “interface” keyword is used for defining an interface.


Now type the code as displayed on the screen

Highlight the code Here the name of the interface is Animal.


It contains three abstract methods talk(), see() and move().


All such methods in an interface are implicitly public and abstract.


An interface can also contain constant variable declarations.


Here, the constant string value “Mammal” is assigned to the variable “type1”.


And “Reptiles” is assigned to the variable “type2”.


All constant values defined in an interface are implicitly public, static and final.

Next we will see implementation class for an interface through an example.
Slide 9:

Implementation class:

Example:

Here Human is a class which implements the Animal interface.


So it must provide implementations for the methods talk(), see() and move().



Slide 10:

Implementing multiple interfaces

Example:


Highlight the syntax and the usage of comma operator

A class can also implement multiple interfaces.

As shown in the example, the class Human implements two interfaces Animal and Action.

Note that a comma operator used in the syntax is to identify the different interfaces.

Now this class should provide implementations to all the abstract methods in both Animal and Action interfaces.

Slide 11:

Interface Usage - Example

The figure here represents an implement relation.

The Animal class is an interface.

The Human class and Snake class are the two implementation classes.

The Human class provides its own different implementations for talk(), see() and move() methods.

The Snake class provides its own different implementations for talk(), see() and move() methods.

Let us understand the usage of interfaces with a sample program.
Now let us explore the differences between an interface and an abstract class.
Slide 7:

Interface Vs Abstract Classes

All the methods in an interface should

be abstract.


Within interface there should not be any

  • constructors
  • concrete methods
  • static methods and
  • main method

But an abstract class can have all these inside it.


The variables in an interface should

be static and final.


There are no such restrictions for an abstract class.

Let us understand the usage of interfaces with a sample program.
Right click on the default package and create a class called Human.


Type implements Living Thing

Right click on the default package and create a class called Human.


Now, to make this an implementation class of Animal, type implements Animal



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


This error indicates that, we should provide an implementations to the Animal interface.



Let us see how to rectify this error.
public void talk(){

System.out.println("I am a Human, I belong to the "+Animal.type1+" family");

}


public void see(){

System.out.println("I can see all colors ");

}


public void move(){

System.out.println("I move by walking");

}

Now let us define the methods talk(), see() and move().

So type public void talk( ).

within curly brackets type

System.out.println within quotes I am a human I belong to


Now we can use the value of the static, final variable type1 declared in the Animal interface


So type

+ Animal.type1+ within quotes family semicolon.


Let us now implement the see() method


So type public void see( ).

within curly brackets type

System.out.println within quotes I can see all colors semicolon


We should also define the move() method


So type public void move( ).

within curly brackets type

System.out.println within quotes I move by walking semicolon


Note that the error disappears, once all the methods are implemented.



Next we will see how to define the Snake class.
Show and highlight the code for Snake Class. I have already created it in my project.


Please create the snake class in your project and type the following code as displayed on the screen.


Now let us go through the code.

Highlight the code


public void talk(){

System.out.println("I am a Snake, I belong to the "+Animal.type2+" Family");

}


public void see(){

System.out.println("I can see only in black and white");

}

public void move(){

System.out.println("I move by crawling");

}

We can see that all the methods of the Animal interface talk() , see() and move() are implemented inside this class.

Here the talk() method prints “I am a snake and I belong to”

Then the value of Animal.type2 is to be printed and then “family”


Here the see() method prints “I can see only in black and white”

The move() method prints "I move by crawling"

Highlight the talk(), see() and move() methods of Human class

Highlight the talk(), see() and move() methods of Snake class

Note that the Human class has its own implementations of talk(), see() and move() methods.


And the Snake class has its own implementations of talk(), see() and move() methods

click new-> class and then type name as Demo. Now right click on the default package click 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.

Highlight the code line by line


Animal h=new Human();

h.talk()

h.see();

h.move();


Type the following code as displayed on the screen.


In this line we instantiate Human class using the Animal interface.


This is represented as


Animal h equals new Human();


Now we can invoke the different methods using this object as


h.talk()

h.see();

h.move();



Highlight the code


Animal s =new Snake();

s.talk();

s.see();

s.move();

Next, we instantiate the Snake class using the Animal interface


Now we can invoke the different methods using this object as shown



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


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



Highlight code and the output We can see the output as


These are printed by the talk(), see() and move() methods invoked by the human class object s

Highlight code and the output These are printed by the talk(), see() and move() methods invoked by the Snake class object s
Now lett us explore the differences between an interface and an abstract class.
Slide 7:

Interface Vs Abstract Classes

All the methods in an interface should

be abstract.


Within interface there should not be any

  • constructors
  • concrete methods
  • static methods and
  • main method

But an abstract class can have all these inside it.


The variables in an interface should be static and final.


There are no such restrictions for an abstract class.

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

Summary

In this tutorial we have learnt about
  • Creating an Interface
  • Creating an implementation class
  • Usage of interfaces


Slide 11:

Assignment

As an assignment


Create an interface Vehicle which

contains the methods brake() and run()


Create another interface Fuel which

contains the following methods .


fill(String type,int quantity)

pay(int quantity,int price)

Assignment Create a subclass Car which implements both interfaces Vehicle and Fuel.


Here brake method must print ”Car

Applies Power brake” and the run

method must print ”Car is running on 4

wheels”



Assignment Similarly fill() method can print the

details of type and quantity of fuel

filled.


For example: 10 Litres of Petrol


pay() method can be used to print

price to be paid.


For example: Pay Rs. 640

Assignment Create another subclass Bike which

again implements both the interfaces

Vehicle and Fuel


Here brake method can print ”Bike

Applies hand brake” and the run

method can print “Bike is running on

2 wheels”


Next implement the fill() and pay() methods as explained earlier.


Finally create a Demo class containing the main method to verify the results.

About Project

(retain the slide as in TEX file)

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


Please download and watch it.

About Workshops

(retain the slide as in TEX file)

The Spoken Tutorial Project Team

• Conducts workshops using spoken tutorials and

• Gives certificates on passing the online tests.


For more details, please write to us.

About NMEICT

(retain the slide as in TEX file)

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


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

Contributor slide

(retain the slide as in TEX file)

This script has been contributed by:

Deptartment of Information

Technology, Amal Jyothi College of Engineering


This is Priya from IIT Bombay signing off. Thanks for joining.

Contributors and Content Editors

Nancyvarkey, Priyacst