Java/C3/Java-Interfaces/English-timed

From Script | Spoken-Tutorial
Revision as of 14:13, 12 August 2016 by PoojaMoolya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time
Narration
00:01 Welcome to the Spoken Tutorial on Java Interfaces.
00:05 In this tutorial we will learn about:
  • Creating an interface
  • Creating an Implementation class and
  • Usage of Interfaces
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 A class can implement an interface using the implements keyword.
01:13 A class can implement more than one interface.
01:17 All the abstract methods defined in the interface or interfaces must be implemented in such classes.
01:25 Now let us explore the differences between an interface and an abstract class.
01:32 All the methods in an interface should be abstract.
01:36 Within interface there should not be any
  • constructors
  • concrete methods
  • static methods and
  • main method
01:46 But an abstract class can have all these inside it.
01:50 The variables in an interface should be static and final.
01:55 There are no such restrictions for an abstract class.
02:01 Next we will see an example for an interface.
02:06 Here the interface name is Animal. It contains three abstract methods talk(), see() and move().
02:15 All such methods in an interface are implicitly public and abstract.
02:21 An interface can also contain constant variable declarations.
02:27 Here, the constant string value “Mammal” is assigned to the variable “type1”.
02:34 And “Reptiles” is assigned to the variable “type2”.
02:39 All constant values defined in an interface are implicitly public, static and final.
02:47 Next, we will see how to create an implementation class for an interface with an example.
02:55 Here Human is a class which implements the Animal interface.
03:00 So it must provide implementations for the methods talk(), see() and move().
03:07 A class can also implement multiple interfaces.
03:12 As shown in the example, the class Human implements two interfaces Animal and Action.
03:19 Note that a comma operator used in the syntax is to identify the different interfaces.
03:27 Now this class should provide implementations to all the abstract methods in both Animal and Action interfaces.
03:36 The figure here represents an implements relation.
03:41 The Animal class is an interface.
03:44 The Human and Snake classes are the two implementation classes.
03:50 The Human class provides its own different implementations for talk(), see() and move() methods.
03:59 And the Snake class provides its own different implementations for talk(), see() and move() methods.
04:08 Let us understand the usage of interfaces with a sample program.
04:13 Now we will switch to Eclipse and create a new project called InterfaceDemo.
04:21 Here we will create the necessary classes and interface to demonstrate the usage of interfaces.
04:28 Now, right click on src folder and click new-> interface.
04:35 Type the name as Animal and press Enter.
04:39 Note that the “interface” keyword is used for defining an interface.
04:45 Now type the code as displayed on the screen.
04:48 Next let us create the implementation classes.
04:53 Right click on the default package and create a class called Human.
04:59 Now to make this an implementation class of Animal, type implements Animal.
05:06 Now we can see, an error comes up in the Eclipse IDE.
05:11 This error indicates that, we should provide an implementations to the Animal interface.
05:17 Let us see how to rectify this error.
05:21 Now let us define the methods talk(), see() and move().
05:26 So type public void talk( ) Within curly brackets type System.out.println within quotes I am a human and I belong to
05:39 Now we can use the value of the static, final variable type1 declared in the Animal interface
05:47 So type + Animal.type1+ within quotes family semicolon.
05:56 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
06:13 We should also define the move() method
06:17 So type public void move( ) Within curly brackets type System.out.println within quotes I move by walking semicolon
06:31 Note that the error disappears, once all the methods are implemented.
06:36 Next we will see how to define the Snake class.
06:40 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.
06:51 Now let us go through the code.
06:55 We can see that all the methods of the Animal interface talk(), see() and move() are implemented inside this class.
07:04 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”
07:16 Here the see() method prints “I can see only in black and white”
07:21 The move() method prints "I move by crawling"
07:26 Note that the Human class has its own implementations of talk(), see() and move() methods.
07:33 And the Snake class has its own implementations of talk(), see() and move() methods.
07:41 Now right click on the default package click new-> class and then type name as Demo.
07:49 Inside this class, we will have the main method, So type main and then press ctrl+space to generate the main method.
08:00 Type the following code as displayed on the screen. In this line, we instantiate the Human class using the Animal interface.
08:09 This is represented as Animal h equals new Human();
08:16 Now we can invoke the different methods using this object as

h.talk()

h.see();

h.move();

08:28 Next, we instantiate the Snake class using the Animal interface.
08:33 This is represented as Animal s equals new Snake()
08:39 Now we can invoke the different methods using this object as

s.talk();

s.see();

s.move();

08:50 Now let us run this Demo program. So right click on the class Demo and then select Run as -> Java Application.
09:01 We can see the output as I am a human and I belong to Mammal family
09:07 I can see all colors and I move by walking
09:11 These are printed by the talk(), see() and move() methods invoked using the Human class object h.
09:19 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
09:30 These are printed by the talk(), see() and move() methods invoked using the Snake class object s.
09:39 This brings us to the end of this tutorial. Let us summarize.
09:44 In this tutorial we have learnt about
  • Creating an Interface
  • Creating an implementation class and
  • Usage of interfaces
09:54 As an assignment Create an interface Vehicle which contains the methods brake() and run().
10:02 Create another interface Fuel which contains the following methods.

fill(String type, int quantity)

pay(int quantity, int price)

10:14 Create a subclass Car which implements both the interfaces Vehicle and Fuel.
10:21 Here brake method should print ”Car, Applies Power brake” and the run method should print ”Car is running on 4 wheels”.
10:30 Similarly fill() method can print the type and quantity of the fuel filled.
10:36 For example: 10 Litres of Petrol. pay() method can be used to print the price to be paid.

For example: Pay Rs.640

10:48 Create another subclass Bike which again implements both the interfaces- Vehicle and Fuel.
10:56 Here brake method can print ”Bike, Applies hand brake” and the run method can print “Bike is running on 2 wheels”.
11:05 Next, implement the fill() and pay() methods as explained earlier.
11:11 Finally, create a Demo class containing the main method to verify the results.
11:16 The video at the following link summarizes the Spoken Tutorial Project. Please download and watch it.
11:25 The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials and
  • Gives certificates on passing the online tests
11:34 For more details, please write to us.
11:37 Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.

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

11:48 This script has been contributed by:

Department of Information Technology, Amal Jyothi College of Engineering.

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

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Sandhya.np14