Java/C3/Java-Interfaces/English-timed
From Script | Spoken-Tutorial
|
|
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. |