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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " {| border=1 | <center>'''Time'''</center> | <center>'''Narration'''</center> |- | 00:01 | Welcome to the''' Spoken Tutorial''' on''' Java Interfaces.''' |- | 00:05 | In thi...")
 
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
 
{| border=1
 
{| border=1
 
| <center>'''Time'''</center>
 
| <center>'''Time'''</center>
Line 6: Line 5:
 
|-
 
|-
 
| 00:01
 
| 00:01
| Welcome to the''' Spoken Tutorial''' on''' Java Interfaces.'''
+
| Welcome to the''' Spoken Tutorial''' on''' Java Interfaces'''.
  
 
|-
 
|-
 
| 00:05
 
| 00:05
| In this tutorial we will learn about:
+
| In this tutorial, we will learn about: Creating an''' interface'''
* Creating an''' interface'''
+
 
* Creating an''' Implementation class''' and
+
|-
* Usage of''' Interfaces'''
+
|  00:10
 +
|Creating ''' Implementation classes''' and Usage of Interface'''.
  
 
|-
 
|-
 
| 00:16
 
| 00:16
| For this tutorial, I am using
+
|For this tutorial, I am using:
 +
 
 +
'''Ubuntu 12.04'''
 +
 
 +
'''JDK 1.7 '''and
  
* '''Ubuntu 12.04'''
+
'''Eclipse 4.3.1'''
* '''JDK 1.7 '''and
+
* '''Eclipse 4.3.1'''
+
  
 
|-
 
|-
 
| 00:28
 
| 00:28
| To follow this tutorial, you should have knowledge of basics of''' Java''' and''' Eclipse IDE.'''
+
| To follow this tutorial, you should have knowledge of basics of''' Java''' and''' Eclipse IDE.'''
  
 
|-
 
|-
| 00:36
+
| 00:36
| 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.'''
  
 
|-
 
|-
| 00:42
+
| 00:42
| If not, for relevant''' Java''' tutorials, please visit the link shown.
+
|If not, for relevant Java tutorials, please visit the link shown.
  
 
|-
 
|-
| 00:48
+
| 00:48
| First let's understand what is an '''interface'''.
+
| First let's understand what is an '''interface'''.
  
 
|-
 
|-
 
| 00:52
 
| 00:52
| An''' Interface''' contains a set of''' abstract methods''' and''' static data members'''.
+
| An Interface contains a set of''' abstract''' methods and''' static data members'''.
  
 
|-
 
|-
| 00:58
+
| 00:58
| It defines the signatures of a set of''' methods,''' without the body.
+
|It defines the signatures of a set of '''methods''' without the body.
  
 
|-
 
|-
| 01:04
+
| 01:04
| It is declared using the '''interface''' keyword.
+
|It is declared using the '''interface''' keyword.
  
 
|-
 
|-
|01:08
+
| 01:08
| A''' class''' can implement an''' interface''' using the''' implements''' keyword.
+
| Now we will switch to''' Eclipse''' and create a new project called''' InterfaceDemo'''.
  
 
|-
 
|-
| 01:13
+
| 01:15
| A''' class''' can implement more than one '''interface.'''
+
|Here, we will create the necessary''' classes''' and''' interface''' to demonstrate the usage of''' interfaces.'''
  
 
|-
 
|-
| 01:17
+
| 01:24
| All the''' abstract methods''' defined in the''' interface''' or''' interfaces''' must be implemented in such '''classes'''.
+
| Right-click on''' src''' folder and click''' New > Interface'''.
  
 
|-
 
|-
| 01:25
+
| 01:30
| Now let us explore the differences between an''' interface''' and an''' abstract''' class.
+
|Type the name as''' Animal''' and press '''Enter'''.
  
 
|-
 
|-
| 01:32
+
| 01:34
| All the methods in an''' interface''' should be''' abstract.'''
+
|Note that the '''“interface”''' keyword is used for defining an interface.
  
 
|-
 
|-
| 01:36
+
| 01:39
| Within''' interface''' there should not be any
+
|Now type the code as displayed on the screen.
 
+
* '''constructors'''
+
* '''concrete methods'''
+
* '''static methods '''and
+
* '''main method'''
+
  
 
|-
 
|-
| 01:46
+
| 01:43
| But an''' abstract class''' can have all these inside it.
+
| Here, the '''interface''' name is''' Animal.'''
  
 
|-
 
|-
| 01:50
+
| 01:46
| The variables in an''' interface''' should be''' static''' and''' final.'''  
+
|It contains three abstract methods''' talk(), see()''' and''' move().'''
  
 
|-
 
|-
| 01:55
+
| 01:52
| There are no such restrictions for an''' abstract class'''.
+
|All such '''methods''' in an interface are implicitly''' public''' and''' abstract'''.
  
 
|-
 
|-
| 02:01
+
| 01:59
| Next we will see an example for an '''interface'''.
+
|An''' interface''' can also contain '''constant variable''' declarations.
  
 
|-
 
|-
| 02:06
+
| 02:04
| Here the '''interface''' name is''' Animal.''' It contains three '''abstract methods talk(), see()''' and''' move().'''
+
|Here, the constant string value “Mammal” is assigned to the variable “type1”.
  
 
|-
 
|-
| 02:15
+
| 02:12
| All such '''methods''' in an '''interface''' are implicitly''' public''' and''' abstract'''.
+
|And “Reptiles” is assigned to the variable “type2”.
  
 
|-
 
|-
| 02:21
+
| 02:16
| An''' interface''' can also contain constant variable declarations.
+
|All''' constant''' values defined in an''' interface''' are implicitly''' public, static''' and''' final.'''
  
 
|-
 
|-
| 02:27
+
| 02:25
| Here, the constant string value''' “Mammal”''' is assigned to the variable''' “type1”'''.  
+
| Next we will see '''implementation class''' for an''' interface''' with an example.
  
 
|-
 
|-
| 02:34
+
| 02:32
| And''' “Reptiles”''' is assigned to the variable''' “type2”'''.
+
| Here,''' Human''' is a class which implements the''' Animal''' interface.
  
 
|-
 
|-
| 02:39
+
| 02:38
| All''' constant values''' defined in an''' interface''' are implicitly''' public, static''' and''' final.'''
+
|So, it must provide implementations for the methods''' talk(), see()''' and''' move().'''
  
 
|-
 
|-
| 02:47
+
|02:45
| Next, we will see how to create an '''implementation class''' for an''' interface''' with an example.
+
| A '''class''' can also implement multiple interfaces.
  
 
|-
 
|-
| 02:55
+
| 02:49
| Here''' Human''' is a''' class''' which implements the''' Animal interface.'''
+
|As shown in the example, the class '''Human''' implements two interfaces '''Animal''' and''' Action.'''
  
 
|-
 
|-
| 03:00
+
| 02:57
| So it must provide implementations for the '''methods talk(), see()''' and''' move().'''
+
|Note that a''' comma operator''' used in the syntax is to identify the different  interfaces.  
  
 
|-
 
|-
| 03:07
+
| 03:04
| A '''class''' can also implement''' multiple interfaces'''.
+
|Now this''' class''' should provide implementations to all the''' abstract methods''' in both''' Animal''' and''' Action''' interfaces.
  
 
|-
 
|-
| 03:12
+
| 03:13
| As shown in the example, the''' class Human '''implements two''' interfaces Animal''' and''' Action.'''
+
| The figure here represents an implement relation.
  
 
|-
 
|-
| 03:19
+
| 03:18
| Note that a''' comma operator''' used in the syntax is to identify the different''' interfaces.'''
+
|The''' Animal''' class is an''' interface.'''
  
 
|-
 
|-
| 03:27
+
| 03:22
| Now this''' class''' should provide implementations to all the''' abstract methods''' in both''' Animal''' and''' Action interfaces.'''
+
|The''' Human''' and''' Snake''' classes are the two '''implementation classes.'''
  
 
|-
 
|-
| 03:36
+
| 03:28
| The figure here represents an '''implements''' relation.  
+
|The '''Human''' class provides its own different implementations for''' talk()''',''' see()''' and''' move()''' methods.
  
 
|-
 
|-
| 03:41
+
| 03:36
| The''' Animal class''' is an''' interface.'''
+
|And, the '''Snake''' class provides its own different implementations for '''talk(), see()''' and '''move()''' methods.
  
 
|-
 
|-
| 03:44
+
| 03:45
| The''' Human '''and''' Snake classes''' are the two '''implementation classes'''.
+
| Let us understand the usage of '''interfaces '''with a sample program.
  
 
|-
 
|-
 
| 03:50
 
| 03:50
| The '''Human class''' provides its own different implementations for''' talk(), see()''' and''' move() methods.'''
+
| Right-click on the '''default package''' and create a''' class''' called''' Human.'''
  
 
|-
 
|-
| 03:59
+
| 03:56
| And the '''Snake class''' provides its own different implementations for''' talk(), see()''' and''' move() methods.'''
+
|Now, to make this an '''implementation class''' of''' Animal''', type: '''implements Animal'''.
  
 
|-
 
|-
| 04:08
+
| 04:04
| Let us understand the usage of '''interfaces '''with a sample program.
+
| Now, we can see an error comes up in the''' Eclipse IDE.'''  
  
 
|-
 
|-
| 04:13
+
| 04:09
| Now we will switch to''' Eclipse''' and create a new project called''' InterfaceDemo'''.
+
|This error indicates that we should provide an '''implementation''' to the '''Animal interface.'''
  
 
|-
 
|-
| 04:21
+
| 04:15
| Here we will create the necessary''' classes''' and''' interface''' to demonstrate the usage of''' interfaces.'''
+
| Let us see how to rectify this error.
  
 
|-
 
|-
| 04:28
+
| 04:19
| Now, right click on''' src''' folder and click''' new-> interface'''.
+
| Now let us define the methods '''talk(), see()''' and''' move().'''
  
 
|-
 
|-
| 04:35
+
| 04:23
| Type the name as''' Animal''' and press '''Enter'''.
+
|So, type:''' public void talk( )''' within curly brackets type '''System.out.println''' within quotes "I am a human and I belong to".
  
 
|-
 
|-
| 04:39
+
| 04:37
| Note that the '''“interface”''' keyword is used for defining an '''interface'''.
+
|Now we can use the value of the''' static, final variable type1''' declared in the''' Animal''' interface.
  
 
|-
 
|-
| 04:45
+
| 04:45
| Now type the code as displayed on the screen.
+
|So, type: '''+ Animal.type1+''' within quotes "family" semicolon.
  
 
|-
 
|-
| 04:48
+
| 04:54
| Next let us create the''' implementation classes.'''
+
|Let us now implement the '''see()''' method.
  
 
|-
 
|-
| 04:53
+
| 04:57
| Right click on the '''default package '''and create a''' class''' called''' Human.'''
+
|So, type:''' public void see( )''' within curly brackets type '''System.out.println''' within quotes "I can see all colors" semicolon.
  
 
|-
 
|-
| 04:59
+
| 05:11
| Now to make this an implementation class of''' Animal, '''type''' implements Animal.'''
+
|We should also define the''' move()''' method.
  
 
|-
 
|-
| 05:06
+
| 05:14
| Now we can see, an error comes up in the''' Eclipse IDE.'''  
+
|So, type:''' public void move( )''' within curly brackets type '''System.out.println''' within quotes "I move by walking" semicolon.
  
 
|-
 
|-
| 05:11
+
| 05:29
| This error indicates that, we should provide an implementations to the '''Animal interface.'''
+
|Note that the error disappears, once all the '''methods''' are implemented.
  
 
|-
 
|-
| 05:17
+
| 05:34
| Let us see how to rectify this error.
+
| Next we will see how to define the''' Snake''' class.
  
 
|-
 
|-
| 05:21
+
| 05:38
| Now let us define the''' methods talk(), see()''' and''' move().'''
+
| I have already created it in my '''project'''.
  
 
|-
 
|-
| 05:26
+
| 05:42
| So type''' public void talk( )''' Within curly brackets type '''System.out.println''' within quotes''' I am a human and I belong to'''
+
|Please create the '''snake class''' in your '''project''' and type the following code as displayed on the screen.
  
 
|-
 
|-
| 05:39
+
| 05:49
| Now we can use the value of the''' static, final variable type1''' declared in the''' Animal interface'''
+
|Now let us go through the code.
  
 
|-
 
|-
| 05:47
+
| 05:52
| So type '''+ Animal.type1+''' within quotes''' family semicolon'''.
+
| We can see that all the '''methods''' of the''' Animal interface- talk(), see()''' and''' move()''' are implemented inside this '''class'''.
  
 
|-
 
|-
| 05:56
+
| 06:01
| 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'''
+
|Here, the''' talk()''' method prints “I am a snake and I belong to”.
  
 
|-
 
|-
| 06:13
+
| 06:07
| We should also define the''' move() method'''
+
|Then the value of''' Animal.type2''' is to be printed and then “family”.
  
 
|-
 
|-
| 06:17
+
| 06:13
| So type''' public void move( )''' Within curly brackets type '''System.out.println''' within quotes''' I move by walking semicolon'''
+
|Here, the''' see()''' method prints “I can see only in black and white”.
  
 
|-
 
|-
| 06:31
+
| 06:19
| Note that the error disappears, once all the '''methods''' are implemented.
+
|The''' move()''' method prints "I move by crawling".
  
 
|-
 
|-
| 06:36
+
| 06:23
| Next we will see how to define the''' Snake class.'''
+
| Note that the''' Human''' class has its own implementations of''' talk(), see()''' and''' move()''' methods.
  
 
|-
 
|-
| 06:40
+
| 06:31
| 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.
+
|And, the''' Snake''' class has its own implementations of''' talk(), see()''' and''' move()''' methods.
  
 
|-
 
|-
| 06:51
+
| 06:39
| Now let us go through the code.
+
| Now, right-click on the default package, click''' new >  class''' and then type the name as''' Demo.'''
  
 
|-
 
|-
| 06:55
+
| 06:47
| We can see that all the '''methods''' of the''' Animal interface talk(), see()''' and''' move()''' are implemented inside this '''class'''.
+
|Inside this '''class''', we will have the''' main''' method.
  
 
|-
 
|-
| 07:04
+
| 06:51
| 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”'''
+
|So, type''' main''' and then press '''ctrl+space''' to generate the''' main''' method.
  
 
|-
 
|-
| 07:16
+
| 06:58
| Here the''' see() method''' prints “'''I can see only in black and white”'''
+
| Type the following code as displayed on the screen.
  
 
|-
 
|-
| 07:21
+
| 07:01
| The''' move() method''' prints''' "I move by crawling"'''
+
|In this line, we instantiate the ''' Human class''' using the''' Animal''' interface.
  
 
|-
 
|-
| 07:26
+
| 07:07
| Note that the''' Human class''' has its own implementations of''' talk(), see()''' and''' move() methods'''.
+
|This is represented as '''Animal h''' equals''' new Human()''';
  
 
|-
 
|-
| 07:33
+
| 07:14
| And the''' Snake class''' has its own implementations of''' talk(), see()''' and''' move() methods'''.
+
|Now we can invoke the different methods using this '''object''' as '''h.talk(); h.see();  h.move();'''
  
 
|-
 
|-
| 07:41
+
| 07:26
| Now right click on the '''default package''' click''' new-> class''' and then type name as''' Demo.'''
+
| Next, we instantiate the''' Snake''' class using the''' Animal''' interface.
  
 
|-
 
|-
| 07:49
+
| 07:31
| Inside this '''class''', we will have the''' main method''', So type''' main''' and then press ctrl+space to generate the''' main method.'''
+
|Now we can invoke the different methods using this '''object''' as shown.
  
 
|-
 
|-
| 08:00
+
| 07:38
| Type the following code as displayed on the screen. In this line, we instantiate the''' Human class''' using the''' Animal interface.'''
+
| Now, let us '''run''' this''' Demo''' program.
  
 
|-
 
|-
| 08:09
+
| 07:41
| This is represented as '''Animal h equals new Human();'''
+
| So, right-click on the class ''' Demo''' and then select''' Run as > Java Application'''.
  
 
|-
 
|-
| 08:16
+
| 07:48
| Now we can invoke the different '''methods''' using this object as
+
| We can see the output.
  
'''h.talk()'''
+
|-
 +
|  07:52
 +
|These are printed by the''' talk(), see()''' and''' move()''' methods invoked using the''' human class object h'''.
  
'''h.see();'''
+
|-
 +
|  08:00
 +
|  These are printed by the''' talk(), see()''' and''' move()''' methods invoked using  the''' Snake class object s'''.
  
'''h.move();'''
+
|-
 +
| 08:08
 +
| Now, let us explore the differences between an''' interface''' and an''' abstract class'''.
  
 
|-
 
|-
| 08:28
+
|08:14
| Next, we instantiate the''' Snake class''' using the''' Animal interface.'''
+
| All the methods in an interface should be''' abstract.'''
  
 
|-
 
|-
| 08:33
+
| 08:18
| This is represented as '''Animal s equals new Snake()'''
+
|Within interface, there should not be any  '''constructors''',
  
 
|-
 
|-
| 08:39
+
| 08:23
| Now we can invoke the different '''methods''' using this object as
+
| '''concrete methods''',  '''static methods '''and '''main method'''.
  
'''s.talk();'''
+
|-
 +
|  08:28
 +
|But an''' abstract class''' can have all these inside it.
  
'''s.see();'''
+
|-
 
+
|  08:32
'''s.move();'''
+
|The variables in an interface should be''' static''' and''' final.'''  
  
 
|-
 
|-
| 08:50
+
| 08:38
| Now let us run this''' Demo''' program. So right click on the'' '''''class Demo''' and then select''' Run as -> Java Application.'''
+
|There are no such restrictions for an '''abstract class'''.
  
 
|-
 
|-
| 09:01
+
| 08:43
| We can see the output as '''I am a human and I belong to Mammal family'''
+
| This brings us to the end of this tutorial. Let us summarize.
  
 
|-
 
|-
| 09:07
+
| 08:48
| '''I can see all colors''' and '''I move by walking'''
+
| In this tutorial, we have learnt about: * Creating an''' Interface'''
  
 
|-
 
|-
| 09:11
+
| 08:53
| These are printed by the''' talk(), see()''' and''' move() methods''' invoked using the''' Human class object h.'''
+
| * Creating an''' implementation class''' and
  
 
|-
 
|-
| 09:19
+
| 08:56
| We can also see in the output '''I am a snake and I belong to Reptiles family'''. '''I can see only in black and white''' and '''I move by crawling'''
+
|* Usage of''' interfaces'''.
  
 
|-
 
|-
| 09:30
+
| 08:59
| These are printed by the''' talk(), see()''' and''' move() methods''' invoked using the''' Snake class object s.'''
+
| As an assignment, create an interface '''Vehicle''' which contains the methods '''brake()''' and''' run()'''.
  
 
|-
 
|-
| 09:39
+
| 09:07
| This brings us to the end of this tutorial. Let us summarize.
+
| Create another interface '''Fuel''' which contains the following methods.   '''fill(String type,int quantity)''', '''pay(int quantity,int price)'''.
  
 
|-
 
|-
| 09:44
+
| 09:19
| In this tutorial we have learnt about
+
| Create a subclass''' Car''' which implements both interfaces''' Vehicle''' and''' Fuel.'''
 
+
* Creating an''' Interface'''
+
* Creating an''' implementation class '''and
+
* Usage of''' interfaces'''
+
  
 
|-
 
|-
| 09:54
+
| 09:26
| As an''' assignment''' Create an '''interface Vehicle''' which contains the methods''' brake()''' and''' run().'''
+
|Here,''' brake''' method should print "Car Applies Power brake".  
  
 
|-
 
|-
| 10:02
+
| 09:30
| Create another '''interface Fuel''' which contains the following methods.
+
|And the '''run''' method must print "Car is running on 4 wheels".
 
+
'''fill(String type, int quantity)'''
+
 
+
'''pay(int quantity, int price)'''
+
  
 
|-
 
|-
| 10:14
+
| 09:35
| Create a subclass''' Car''' which implements both the '''interfaces Vehicle''' and''' Fuel.'''
+
| Similarly''' fill()''' method can print the type and quantity of the fuel filled.
  
 
|-
 
|-
| 10:21
+
| 09:41
| Here''' brake method''' should print''' ”Car, Applies Power brake”''' and the '''run method''' should print''' ”Car is running on 4 wheels”.'''
+
|For example: 10 Litres of petrol.
  
 
|-
 
|-
| 10:30
+
| 09:44
| Similarly''' fill() method''' can print the type and quantity of the fuel filled.
+
| '''pay()''' method can be used to print price to be paid. For example: Pay Rs. 640
  
 
|-
 
|-
| 10:36
+
| 09:53
| For example: 10 Litres of Petrol. '''pay() method''' can be used to print the price to be paid.
+
|Create another subclass''' Bike''' which again implements both the interfaces '''Vehicle''' and''' Fuel'''.
For example: Pay Rs.640
+
  
 
|-
 
|-
| 10:48
+
| 10:00
| Create another subclass''' Bike''' which again implements both the '''interfaces'''- '''Vehicle''' and''' Fuel.'''
+
|Here, '''brake''' method can print "Bike Applies hand brake".  
  
 
|-
 
|-
| 10:56
+
| 10:05
| Here '''brake method''' can print ”'''Bike, Applies hand brake”''' and the '''run method''' can print “'''Bike is running on 2 wheels”'''.
+
|And the '''run''' method can print “Bike is running on 2 wheels”.
  
 
|-
 
|-
| 11:05
+
| 10:10
| Next, implement the '''fill()''' and '''pay() methods''' as explained earlier.
+
|Next, implement the '''fill()''' and '''pay()''' methods as explained earlier.
  
 
|-
 
|-
| 11:11
+
| 10:15
| Finally, create a '''Demo class''' containing the '''main method''' to verify the results.
+
|Finally create a '''Demo class''' containing the '''main''' method to verify the results.
  
 
|-
 
|-
| 11:16
+
| 10:21
| The video at the following link summarizes the''' '''Spoken Tutorial Project. Please download and watch it.
+
| This video at the following link summarizes the Spoken Tutorial Project. Please download and watch it.
  
 
|-
 
|-
| 11:25
+
| 10:29
| The Spoken Tutorial Project Team
+
| The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials and
  
*Conducts workshops using spoken tutorials and
+
Gives certificates on passing the online tests.
*Gives certificates on passing the online tests
+
  
 
|-
 
|-
| 11:34
+
| 10:38
 
| For more details, please write to us.
 
| For more details, please write to us.
  
 
|-
 
|-
| 11:37
+
| 10:41
| Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
+
| '''Spoken Tutorial Project''' is funded by NMEICT, MHRD, Government of India.
  
More information on this Mission is available at the link shown.
+
|-
 +
|  10:48
 +
|More information on this mission is available at the link shown.
  
 
|-
 
|-
| 11:48
+
| 10:52
| This script has been contributed by:
+
|This script has been contributed by: Department of Information Technology, Amal Jyothi College of Engineering.
  
Department of Information Technology, Amal Jyothi College of Engineering.
+
|-
 
+
|  11:01
This is Priya from IIT Bombay signing off. Thank you for joining.
+
|This is Priya from '''IIT Bombay''', signing off. Thanks for joining.
  
 
|}
 
|}

Latest revision as of 18:09, 3 May 2019

Time
Narration
00:01 Welcome to the Spoken Tutorial on Java Interfaces.
00:05 In this tutorial, we will learn about: Creating an interface
00:10 Creating Implementation classes and Usage of Interface.
00:16 For this tutorial, I am using:

Ubuntu 12.04

JDK 1.7 and

Eclipse 4.3.1

00:28 To follow this tutorial, you should have knowledge of basics of Java and Eclipse IDE.
00:36 You should also have the knowledge of subclassing and Abstract classes in Java.
00:42 If not, for relevant Java tutorials, please visit the link shown.
00:48 First let's understand what is an interface.
00:52 An Interface contains a set of abstract methods and static data members.
00:58 It defines the signatures of a set of methods without the body.
01:04 It is declared using the interface keyword.
01:08 Now we will switch to Eclipse and create a new project called InterfaceDemo.
01:15 Here, we will create the necessary classes and interface to demonstrate the usage of interfaces.
01:24 Right-click on src folder and click New > Interface.
01:30 Type the name as Animal and press Enter.
01:34 Note that the “interface” keyword is used for defining an interface.
01:39 Now type the code as displayed on the screen.
01:43 Here, the interface name is Animal.
01:46 It contains three abstract methods talk(), see() and move().
01:52 All such methods in an interface are implicitly public and abstract.
01:59 An interface can also contain constant variable declarations.
02:04 Here, the constant string value “Mammal” is assigned to the variable “type1”.
02:12 And “Reptiles” is assigned to the variable “type2”.
02:16 All constant values defined in an interface are implicitly public, static and final.
02:25 Next we will see implementation class for an interface with an example.
02:32 Here, Human is a class which implements the Animal interface.
02:38 So, it must provide implementations for the methods talk(), see() and move().
02:45 A class can also implement multiple interfaces.
02:49 As shown in the example, the class Human implements two interfaces Animal and Action.
02:57 Note that a comma operator used in the syntax is to identify the different interfaces.
03:04 Now this class should provide implementations to all the abstract methods in both Animal and Action interfaces.
03:13 The figure here represents an implement relation.
03:18 The Animal class is an interface.
03:22 The Human and Snake classes are the two implementation classes.
03:28 The Human class provides its own different implementations for talk(), see() and move() methods.
03:36 And, the Snake class provides its own different implementations for talk(), see() and move() methods.
03:45 Let us understand the usage of interfaces with a sample program.
03:50 Right-click on the default package and create a class called Human.
03:56 Now, to make this an implementation class of Animal, type: implements Animal.
04:04 Now, we can see an error comes up in the Eclipse IDE.
04:09 This error indicates that we should provide an implementation to the Animal interface.
04:15 Let us see how to rectify this error.
04:19 Now let us define the methods talk(), see() and move().
04:23 So, type: public void talk( ) within curly brackets type System.out.println within quotes "I am a human and I belong to".
04:37 Now we can use the value of the static, final variable type1 declared in the Animal interface.
04:45 So, type: + Animal.type1+ within quotes "family" semicolon.
04:54 Let us now implement the see() method.
04:57 So, type: public void see( ) within curly brackets type System.out.println within quotes "I can see all colors" semicolon.
05:11 We should also define the move() method.
05:14 So, type: public void move( ) within curly brackets type System.out.println within quotes "I move by walking" semicolon.
05:29 Note that the error disappears, once all the methods are implemented.
05:34 Next we will see how to define the Snake class.
05:38 I have already created it in my project.
05:42 Please create the snake class in your project and type the following code as displayed on the screen.
05:49 Now let us go through the code.
05:52 We can see that all the methods of the Animal interface- talk(), see() and move() are implemented inside this class.
06:01 Here, the talk() method prints “I am a snake and I belong to”.
06:07 Then the value of Animal.type2 is to be printed and then “family”.
06:13 Here, the see() method prints “I can see only in black and white”.
06:19 The move() method prints "I move by crawling".
06:23 Note that the Human class has its own implementations of talk(), see() and move() methods.
06:31 And, the Snake class has its own implementations of talk(), see() and move() methods.
06:39 Now, right-click on the default package, click new > class and then type the name as Demo.
06:47 Inside this class, we will have the main method.
06:51 So, type main and then press ctrl+space to generate the main method.
06:58 Type the following code as displayed on the screen.
07:01 In this line, we instantiate the Human class using the Animal interface.
07:07 This is represented as Animal h equals new Human();
07:14 Now we can invoke the different methods using this object as h.talk(); h.see(); h.move();
07:26 Next, we instantiate the Snake class using the Animal interface.
07:31 Now we can invoke the different methods using this object as shown.
07:38 Now, let us run this Demo program.
07:41 So, right-click on the class Demo and then select Run as > Java Application.
07:48 We can see the output.
07:52 These are printed by the talk(), see() and move() methods invoked using the human class object h.
08:00 These are printed by the talk(), see() and move() methods invoked using the Snake class object s.
08:08 Now, let us explore the differences between an interface and an abstract class.
08:14 All the methods in an interface should be abstract.
08:18 Within interface, there should not be any constructors,
08:23 concrete methods, static methods and main method.
08:28 But an abstract class can have all these inside it.
08:32 The variables in an interface should be static and final.
08:38 There are no such restrictions for an abstract class.
08:43 This brings us to the end of this tutorial. Let us summarize.
08:48 In this tutorial, we have learnt about: * Creating an Interface
08:53 * Creating an implementation class and
08:56 * Usage of interfaces.
08:59 As an assignment, create an interface Vehicle which contains the methods brake() and run().
09:07 Create another interface Fuel which contains the following methods. fill(String type,int quantity), pay(int quantity,int price).
09:19 Create a subclass Car which implements both interfaces Vehicle and Fuel.
09:26 Here, brake method should print "Car Applies Power brake".
09:30 And the run method must print "Car is running on 4 wheels".
09:35 Similarly fill() method can print the type and quantity of the fuel filled.
09:41 For example: 10 Litres of petrol.
09:44 pay() method can be used to print price to be paid. For example: Pay Rs. 640
09:53 Create another subclass Bike which again implements both the interfaces Vehicle and Fuel.
10:00 Here, brake method can print "Bike Applies hand brake".
10:05 And the run method can print “Bike is running on 2 wheels”.
10:10 Next, implement the fill() and pay() methods as explained earlier.
10:15 Finally create a Demo class containing the main method to verify the results.
10:21 This video at the following link summarizes the Spoken Tutorial Project. Please download and watch it.
10:29 The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials and

Gives certificates on passing the online tests.

10:38 For more details, please write to us.
10:41 Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
10:48 More information on this mission is available at the link shown.
10:52 This script has been contributed by: Department of Information Technology, Amal Jyothi College of Engineering.
11:01 This is Priya from IIT Bombay, signing off. Thanks for joining.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Sandhya.np14