Total members 11895 |It is currently Tue Dec 03, 2024 5:44 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka



Go to page 1, 2  Next


Java interface example :
Interfaces are just a definition of behavior. Java Interface is not a class but it is defined in away similar to the class definition. An Interface describes the functions and behaviors of a specific object type but doesn't implement them. When a class implements an interface it has to implement all methods declared in that interface.

Why to use interfaces?


We can answer this by example. Suppose you are designing a library system which has library manager class and you have a research thesis class which contains a group of functions and variables that describe its characteristics. The research thesis class will have a relationship to the manager class. The manager class will need determined set of information from each library item and doesn’t care about its class. Note that any library could have many items such as research papers, books, magazines and CDs. In hope to do that the manager class will force all the library items to implements a specific interface which includes all the needed variables and functions prototypes. By this way, the interface will works as communication protocol or a set of rules between the library manager class and the library items classes. The manager class design will be built with the usage of interface properties whatever the item class type. You even can finish the implementation of the library manager program without the need to wait for finishing items classes’ implementation. You can also add any new item class with no needs for manger class changes. In the other side all the items will implements the interface and each item can has its unique implementation. Usage of interface in this example avoids the non needed classes’ relationships by finding the similarities between unrelated classes. Interfaces also save the time of rewriting the functions prototypes creating reusable declarations.
Some hints about Java Interfaces
  • Interface functions should be public and abstract.
  • Interface fields should be public and final.
  • Use the Keyword interface to define an interface.
  • If you define a public interface with name myInterface the java file should be named as myInterface.java (Similar to public class definition rules).
  • A class implementing an interface should use the keyword implements.
  • No objects can be created from an interface.
  • Interfaces don't have constructors as they can't be initiated
  • An Interface can extends one or more interfaces.
  • You can define a reference of type interface but you should assign to it an object instance of class type which implements that interface.

One Interface - One Class


In this example I defined a java shape interface and implemented it using the circle class . Both shape interface and circle class are defined within a Main.java file. As you may notice Main class is the only public class in this file.
java code
public class Main {
 
     
            
    public static void main(String[] args) {
        
          shape circleshape=new circle();
          
             circleshape.Draw();
    }
}

interface shape
 {
     public   String baseclass="shape";
     
     public void Draw();     
     
 }
 class circle implements shape
 {

    public void Draw() {
        System.out.println("Drawing Circle here");
    }
     
     
 }
 

As you see in the example above, shape interface has only one constant and one abstract method with no body. I have defined a reference of type shape with name "circleshape" and i assigned to it a circle object. This type of casting is called upcasting. Then I called the overridden function Draw(), in such case JVM will call the function of the object instance type not the one belongs to variable reference type. The output of running this snippet is :
Code:
Drawing Circle here


Interface Inherits An Interface


An interface can inherit one or more interfaces using extends keyword , but no interface can implements any other interfaces.
java code
public class Main {



public static void main(String[] args) {

shapeA circleshape=new circle();

circleshape.Draw();
circleshape.Draw();
}
}

interface shapeA
{
public String baseclass="shape";
public void Draw();
}
interface shapeB extends shapeA
{
public String baseclass="shape2";
public void Draw2();
}
class circle implements shapeB
{
public String baseclass="shape3";
public void Draw() {
System.out.println("Drawing Circle here:"+baseclass);
}
@Override
public void Draw2() {
System.out.println("Drawing Circle here:"+baseclass);
}
}

Please note multi- declaration of member variable baseclass, even that all interface fields are finals you can redefine your owns field with same in name while you extending or/ implementing an interface.
The output is :
java code
Drawing Circle here:shape3
Drawing Circle here:shape3


Many interfaces - One class


As you see in the previous examples , classe can implement an interface, interface can extends other interfaces, but in example below one class is implementing many interface.(Note: one class can extends only one class).
java code
public class Main {

public static void main(String[] args) {

shapeA circleshape = new circle();

circleshape.Draw();
// Note we can't call function Draw2 because the reference of type shapeA
//circleshape.Draw2();
}
}

interface shapeA {

public String baseclass = "shape";

public void Draw();
}

interface shapeB extends shapeA {

public String baseclass = "shape2";

public void Draw2();
}

interface shapeC {

public String baseclass = "shape3";

public void Draw3();
}

class circle implements shapeA, shapeB, shapeC {

public void Draw() {
// Try to change the value you will get error because baseclass is final
//baseclass = "Trying-To-Change Vlaue"
System.out.println("Drawing Circle here:" + shapeA.baseclass);
}

@Override
public void Draw2() {
System.out.println("Drawing Circle here:" + shapeB.baseclass);
}

@Override
public void Draw3() {
System.out.println("Drawing Circle here:" + shapeC.baseclass);
}
}

The output of this snippet is :
Code:
Drawing Circle here:shape


Note that am calling the Draw Function not Draw2 or Draw3. You notice that in accessing the baseclass string in the Draw function implmentation I used the super class name, for instance here:
java code
public void Draw() {
// Try to change the value you will get error because baseclass is final
//baseclass = "Trying-To-Change Vlaue"
System.out.println("Drawing Circle here:" + shapeA.baseclass);
}

@Override
public void Draw2() {
System.out.println("Drawing Circle here:" + shapeB.baseclass);
}

@Override
public void Draw3() {
System.out.println("Drawing Circle here:" + shapeC.baseclass);
}

If you removed the base(super) class name and called the field directly like this:
java code
System.out.println("Drawing Circle here:" + shapeC.baseclass);

you will get an error because of calling baseclass string in ambiguous way.


Interface and Abstract Class


Same rules between the normal classes and interfaces are applied to the abstract classes. The only differences are in using abstract class that you can create you own new abstract methods and you can't create an objects of abstract class itself. See snippet below.
java code
public class Main {

public static void main(String[] args) {

shape solidCircle = new SolidCircle();

solidCircle.Draw();
// Note we can't call function Draw2 because the reference of type shapeA
//circleshape.Draw2();
}
}

interface shape {

public String myname = "shape";

public void Draw();
}

abstract class circle implements shape {

public void Draw() {
// Try to change the value you will get error because baseclass is final
//baseclass = "Trying-To-Change Vlaue"
System.out.println("Drawing Circle here:" + myname);
}

abstract public void fillingAlgorithm();
}


class SolidCircle extends circle {

@Override
public void fillingAlgorithm() {
// Write fillign algorithm here.
}

public void Draw() {
// Try to change the value you will get error because baseclass is final
//baseclass = "Trying-To-Change Vlaue"
System.out.println("Overriden Solid Circle here:" + myname);
}
}

In this example I have added a new normal class name SolidCircle to extends the abstract class circle, but notice that in this way am not forced to override the Draw function in SolidCircle class but am forced to implement the abstract class method fillingAlgorithm(). The output of this snippet is :
Code:
Overriden Solid Circle here:shape


And always remember that java interfaces can't be instantiated, for example
java code
// This code gives you error
shape circleshape = new shape();




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

Use Full thanks



Author:

Hmmmmmmmmmmmmmmm
Thanks a lot!!!!!!!!!!!!!!!!!!!



Author:

thanx...it is very useful to me...... :clapping:



Author:

very useful
thanks :gOOd:



Author:

Nice



Author:

Java Interface Examples Updated.


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

thanks a lot this is so helpfull atleast now i understand interfaces. kee up the good work..



Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

Hmm... Nice tutorial bro... helps a lot.. easy to understand... :wave:



Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

It is benificial for everyone, so thanks.



Author:
Newbie
User avatar Posts: 6
Have thanks: 0 time
Post new topic Reply to topic  [ 12 posts ]  Go to page 1, 2  Next

  Related Posts  to : java interface example
 about Java IO-interface     -  
 Network Interface Adapter Info using Java APIs.     -  
 java abstract class,concrete class and interface     -  
 What is an Interface? !!!!     -  
 Iterator interface     -  
 Implement an interface in php     -  
 List interface     -  
 how to implement an interface     -  
 how to write interface program     -  
 Is garbageCollector a Class or interface?     -  



Topic Tags

Java OOP






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com