Java/C3/Java-Interfaces/English
Title of script: Interfaces
Author: Joms Antony
Keywords: Interface, Implementation Classes,implements keyword, video tutorial
|
|
Slide 1:
|
Welcome to the Spoken Tutorial on Java Interfaces |
Slide 2:
Learning Objectives
|
In this tutorial we will learn about:
|
Slide 3:
Software Requirements
|
For this tutorial, I am using
|
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.
|
In Eclipse IDE a project called InterfaceDemo is created,
|
Now we will switch to Eclipse and create a new project called InterfaceDemo.
|
The interface Animal is created in the src folder.
Code is copied
|
Now, right click on src folder and click new-> interface.
|
Highlight the code | Here the name of the interface is Animal.
|
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.
|
Slide 10:
Implementing multiple interfaces Example:
|
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.
But an abstract class can have all these inside it.
be static and final.
|
Let us understand the usage of interfaces with a sample program. | |
Right click on the default package and create a class called Human.
|
Right click on the default package and create a class called Human.
|
Point to the error message | Now we can see an error comes up in the Eclipse IDE.
|
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"); }
System.out.println("I can see all colors "); }
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
+ Animal.type1+ within quotes family semicolon.
within curly brackets type System.out.println within quotes I can see all colors semicolon
within curly brackets type System.out.println within quotes I move by walking semicolon
|
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.
|
Highlight the code
System.out.println("I am a Snake, I belong to the "+Animal.type2+" Family"); }
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”
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.
|
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.
|
Highlight the code line by line
h.talk() h.see(); h.move();
|
Type the following code as displayed on the screen.
h.see(); h.move();
|
Highlight the code
s.talk(); s.see(); s.move(); |
Next, we instantiate the Snake class using the Animal interface
|
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
|
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.
But an abstract class can have all these inside it.
|
This brings us to the end of this tutorial. Let us summarize. | |
Slide 10:
Summary |
In this tutorial we have learnt about
|
Slide 11:
Assignment |
As an assignment
contains the methods brake() and run()
contains the following methods .
pay(int quantity,int price) |
Assignment | Create a subclass Car which implements both interfaces Vehicle and Fuel.
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.
price to be paid.
|
Assignment | Create another subclass Bike which
again implements both the interfaces Vehicle and Fuel
Applies hand brake” and the run method can print “Bike is running on 2 wheels”
|
About Project
(retain the slide as in TEX file) |
This video at the following link summarizes the Spoken Tutorial Project.
|
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.
|
About NMEICT
(retain the slide as in TEX file) |
Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
|
Contributor slide
(retain the slide as in TEX file) |
This script has been contributed by:
Deptartment of Information Technology, Amal Jyothi College of Engineering
|