Difference between revisions of "Java/C3/Java-Interfaces/English"
(Created page with "'''Title of script:''' Java Interfaces '''Author:''' Joms Antony '''Keywords:''' Java Interfaces, Implementation Classes, implements keyword, abstract method, video tutorial...") |
Nancyvarkey (Talk | contribs) |
||
Line 22: | Line 22: | ||
| 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 #000000;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 class''' and | ||
* Usage of''' Interfaces''' | * Usage of''' Interfaces''' | ||
− | |||
− | |||
|- | |- | ||
Line 41: | Line 38: | ||
* '''JDK 1.7 '''and | * '''JDK 1.7 '''and | ||
* '''Eclipse 4.3.1''' | * '''Eclipse 4.3.1''' | ||
− | |||
− | |||
|- | |- | ||
Line 65: | Line 60: | ||
'''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''' | + | | 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'''. |
− | |||
+ | 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. | + | |
|- | |- | ||
Line 86: | Line 75: | ||
− | | 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''' | + | | 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. |
− | + | ||
− | using the''' implements''' keyword. | + | |
− | + | ||
− | |||
− | '''interface.''' | + | A''' class''' can implement more than one '''interface.''' |
Line 106: | Line 91: | ||
'''Interface Vs Abstract Classes''' | '''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 | + | | 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.''' |
− | + | ||
− | be''' abstract.''' | + | |
Line 118: | Line 101: | ||
* '''main method''' | * '''main method''' | ||
− | But an''' abstract''' | + | But an''' abstract class''' can have all these inside it. |
− | + | ||
− | |||
− | be''' static''' and''' final.''' | + | The variables in an''' interface''' should be''' static''' and''' final.''' |
− | There are no such restrictions for an''' abstract''' | + | There are no such restrictions for an''' abstract class'''. |
|- | |- | ||
Line 144: | Line 125: | ||
− | | 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.''' | + | | 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 | + | It contains three '''abstract methods talk(), see()''' and''' move().''' |
Line 166: | Line 147: | ||
|- | |- | ||
| style="border:1pt solid #000000;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;"| | ||
− | | 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: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. |
|- | |- | ||
Line 200: | Line 181: | ||
'''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:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The figure here represents an '''implements''' relation. |
The''' Animal class''' is an''' interface.''' | The''' Animal class''' is an''' interface.''' | ||
Line 206: | Line 187: | ||
The''' Human '''and''' Snake classes''' are the two '''implementation classes'''. | The''' Human '''and''' Snake classes''' are the two '''implementation classes'''. | ||
− | The '''Human class''' provides its own different implementations for''' talk() | + | 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() | + | And the '''Snake class''' provides its own different implementations for''' talk(), see()''' and''' move() methods.''' |
|- | |- | ||
Line 228: | Line 209: | ||
Code is copied | Code is copied | ||
− | | 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- | + | | 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'''. |
Line 234: | Line 215: | ||
− | Note that | + | Note that the '''“interface”''' keyword is used for defining an '''interface'''. |
Line 255: | Line 236: | ||
|- | |- | ||
| 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 #000000;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 #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.''' |
Line 288: | Line 269: | ||
So type''' public void talk( )''' | So type''' public void talk( )''' | ||
− | |||
− | '''System. | + | Within curly brackets type |
+ | |||
+ | '''System.out.println''' within quotes''' I am a human and I belong to''' | ||
Line 296: | Line 278: | ||
− | So type | + | So type '''+ Animal.type1+''' within quotes''' family semicolon'''. |
− | |||
− | + | Let us now implement the '''see() method''' | |
− | Let us now implement the '''see()''' | + | |
So type''' public void see( )''' | So type''' public void see( )''' | ||
− | |||
− | '''System. | + | Within curly brackets type |
+ | |||
+ | '''System.out.println''' within quotes''' I can see all colors semicolon''' | ||
− | We should also define the''' move()''' | + | We should also define the''' move() method''' |
So type''' public void move( )''' | 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. | + | |
+ | Note that the error disappears, once all the '''methods''' are implemented. | ||
|- | |- | ||
Line 332: | Line 314: | ||
− | Please create the '''snake''' | + | Please create the '''snake class''' in your project and type the following code as displayed on the screen. |
Line 359: | Line 341: | ||
} | } | ||
− | | 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() | + | | 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'''. |
+ | |||
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 367: | Line 351: | ||
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"''' | ||
Line 374: | Line 359: | ||
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''' | + | | 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'''. |
− | And the''' Snake''' | + | 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 #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 #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Now right click on the '''default package''' click''' new | + | | 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.''' |
− | Inside this class we will have the''' main method''', | + | Inside this '''class''', we will have the''' main method''', |
Line 409: | Line 394: | ||
− | In this line, we | + | In this line, we instantiate the''' 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 | |
− | + | ||
− | + | ||
− | + | ||
− | Now we can invoke the different methods using this object as | + | |
Line 443: | Line 423: | ||
− | This is represented as | + | This is represented as '''Animal s equals new Snake()''' |
− | + | ||
− | '''Animal s | + | |
− | Now we can invoke the different methods using this object as | + | Now we can invoke the different '''methods''' using this object as |
Line 463: | Line 441: | ||
|- | |- | ||
| 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 #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 #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 | + | | 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.''' |
|- | |- | ||
Line 490: | Line 468: | ||
'''I can see only in black and white''' | '''I can see only in black and white''' | ||
+ | |||
and '''I move by crawling''' | and '''I move by crawling''' | ||
Line 509: | Line 488: | ||
* Creating an''' implementation class '''and | * Creating an''' implementation class '''and | ||
* Usage of''' interfaces''' | * Usage of''' interfaces''' | ||
− | |||
− | |||
|- | |- | ||
Line 519: | Line 496: | ||
− | Create an '''interface Vehicle''' which | + | Create an '''interface Vehicle''' which contains the methods''' brake()''' and''' run().''' |
− | + | ||
− | contains the methods''' brake()''' and''' run().''' | + | |
− | + | ||
− | |||
− | contains the following methods. | + | Create another '''interface Fuel''' which contains the following methods. |
Line 538: | Line 511: | ||
− | Here''' brake method''' should print''' ”Car | + | 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 #000000;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 | + | | 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. |
− | + | ||
− | the type and quantity of the fuel | + | |
− | + | ||
− | filled. | + | |
Line 558: | Line 521: | ||
− | '''pay() method''' can be used to print | + | '''pay() method''' can be used to print the price to be paid. |
− | + | ||
− | the price to be paid. | + | |
− | + | ||
For example: Pay Rs.640 | For example: Pay Rs.640 | ||
Line 567: | Line 527: | ||
|- | |- | ||
| style="border:1pt solid #000000;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;"| '''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 | + | | 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'''- |
− | + | ||
− | again implements both the '''interfaces''' | + | |
'''Vehicle''' and''' Fuel.''' | '''Vehicle''' and''' Fuel.''' | ||
− | Here '''brake method''' can print ”'''Bike | + | Here '''brake method''' can print ”'''Bike, Applies hand brake”''' and the '''run method''' can print “'''Bike is running on 2 wheels”'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 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;"| | ||
− | | 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()''' | + | | 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. |
− | 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. |
|- | |- | ||
Line 606: | Line 558: | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| The Spoken Tutorial Project Team | | style="border:1pt solid #000000;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 | |
− | + | *Gives certificates on passing the online tests | |
− | + | ||
Line 630: | Line 581: | ||
| 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: | | 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: | ||
− | Department of Information | + | Department of Information Technology, Amal Jyothi College of Engineering. |
− | + | ||
− | Technology, Amal Jyothi College of Engineering | + | |
Revision as of 16:12, 29 December 2015
Title of script: Java Interfaces
Author: Joms Antony
Keywords: Java Interfaces, Implementation Classes, implements keyword, abstract method, video tutorial, Java 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.
|
Slide 6:
Implementing the interface
|
A class can implement an interface using the implements keyword.
|
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.
|
Next we will see an example for an interface. | |
Slide 8:
Interface: Example:
|
Here the interface name is Animal.
|
Next, we will see how to create an implementation class for an interface with 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 implements relation.
The Animal class is an interface. The Human and Snake classes are the two implementation classes. 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. |
Let us understand the usage of interfaces with a sample program. | |
In Ecilpse 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.
|
Next let us create the implementation classes. | |
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( )
System.out.println within quotes I am a human and I belong to
System.out.println within quotes I can see all colors semicolon
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.
|
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.
s.see(); s.move(); |
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 | We can also see in the output
|
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
pay(int quantity, int price) |
Assignment | Create a subclass Car which implements both the interfaces Vehicle and Fuel.
|
Assignment | Similarly fill() method can print the type and quantity of the fuel filled.
For example: Pay Rs.640 |
Assignment | Create another subclass Bike which again implements both the interfaces-
Vehicle and Fuel.
|
Next, implement the fill() and pay() methods as explained earlier.
| |
About Project
|
The video at the following link summarizes the Spoken Tutorial Project.
|
About Workshops
|
The Spoken Tutorial Project Team
|
About NMEICT
|
Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
|
Contributor slide
|
This script has been contributed by:
Department of Information Technology, Amal Jyothi College of Engineering.
|