Tuesday 9 July 2019

interface in java - online help

An interface in Java:

An interface is a group of constants and methods declaration that defines the form of class. An interface is a collection methods declaration which can be implemented by the classes.

 An interface is similar to class.Java uses interfaces to multiple inheritance.java does not support multiple inheritances, but interface serves to implement multiple inheritances.

 All the methods of an interface are automatically public. It is not necessary to supply the keyword "public" when declaring a method in an interface. Once it's defined, any number of classes can implement any number of the interface.

Rules for naming the interface:

-Methods inside the interface must not be static, final, native.

-All variable declared inside the interface is implicitly public static final variables(constants).

-All methods declared inside the Java interface are implicitly public and abstract, even if you don't use public and abstract keyword.

-The interface can extend one or more other interfaces.

-The interface cannot implement a class.

-The interface can be nested inside another interface.

Defining an interface:

Defining an interface is similar to the class. An interface is created in the same way as a class. To create an interface, we use a keyword interface. An interface can extend any number of interfaces. The general form to create interface is,

interface name
{
datatype varname1=value1;
datatype varname2=value2;
datatype varname3=value3;
-------------------
-------------------
-------------------
datatype varnameN=valueN;
return-type methodname1(parameter-list);
return-type methodname2(parameter-list);
return-type methodname3(parameter-list);
--------------------
--------------------
--------------------
return-type methodnameN(parameter list);
}

the interface is the java keyword, the name is the name of the interface and can be any valid identifier. Notice that the method which has nobody is ended with the semicolon after the parameter list. Methods without parameters are an abstract method, there is no default implementation of methods specified within an interface. Each class that includes an interface must implement all of the methods.


The variable can be declared inside of interface declaration. They are implicitly final and static, they cannot be changed by the implementing class. They must also be initialized with a constant value. All methods and variables are implicitly public if the interface, itself, is declared as public.

We can combine the declaration of several variables onto one line as follow:
datatype varname1=value1...............varnameN=valueN;

Here, is an example of an interface definition. It declares a simple interface which contains one method called callback() that takes an integer parameter.
interface callback
{
void callback(int p);
}

Extending Interface:

An interface can be interfaced from another interface, using the keyword extends. The general format of extending interface as follows:
interface subclass extends baseclass
{
//variable declaration;
//method declaration;
}

Here interface and extends are java keyword.subclass is a new class and baseclass is an existing class.

Implementing interface:

Once an interface has been defined, one or more classes can implement that interface. To implement an interface, include the implement keyword in the class definition, and then create the method defined by the interface. The general form of a class is

class classname extends supperclass implements interface,[interface...........]]
{
//class body
}

classname is the name of the class. If the class implements more than one interface, the interface is separated with a comma. If a class implements two interfaces that declare the same method, then the same method will be used by both of the interfaces. The methods that implement an interface must be declared public. Also, the data type of the implementing method must match exactly the data type specified in the interface definition.

class salary
{
void show_salary()
{
System.out.println("salary:100000");
}
}
interface commission
{
void show_comm();
}
class income extends salary implements commission
{
public void show_comm()
{
System.out.println("commission:1000");
}
void show_income()
{
System.out.println("Total income includes");
show_salary();
show_comm();
}
public static void main(String args[])
{
income in=new income();
in.show_income();
}
}

So, the output is
salary:100000
commission:1000

Accessing implementation through interface reference:

The interface can be accessed by a variable as an object reference. Any instance of any class that implements the interface can be stored in this variable. This is the key feature of the interface. The method to be executed at the run time, allowing classes to be created later than the code which calls a method on them. The calling code can dispatch through an interface without having to know anything about the callee. The following example calls the show_income() method via an interface variable:
class totalsalary{
public static void main(String args[])
{
commission c=new income();
c.show_income();
}



Partial implementation of the interface:

If a class includes an interface but does not fully implement the method defined by that interface, then that class must be declared as abstract. For example,
abstract class incomplete implements callback
{
int a,b;
void show()
{
System.out.println(a+""+b)
}
//...............................
}
here, the class incomplete does not implement the methods callback() because it is declared as abstract.

Variables in the interface:

you can use interface to import the shared constants into multiple classes by simply declaring an interface that contains variable which is initialized to the desired value. when you include that interface in a class that is when you "implement" the interface, all of those variable names will be in scoop as constants. If an interface contains no method, The any class that includes such an interface does not actually implement anything. It is as if that class was importing the constant variable into the class namespace as the final variable.

import java.util.*;
interface sharedconstants
{
int no=0;
int yes=1;
int maybe=2;
int later=3;
int soon=4;
int never=5;
}
class question implements sharedconstant{
Random r=new Random();
int ask()
{
int p=int(100*r.nextDouble());
if(p<30)
return no;
else if(p<60)
return yes;
else if(p<75)
return later;
else if(p<98)
return soon;
else
return never;
}
}
class askme implements sharedconstant
{
static void answer(int result)
{
switch(result)
{
case no:
System.out.println("no");
break;
case yes:
System.out.println("yes");
break;
case maybe:
System.out.println("maybe");
break;
case later:
System.out.println("later");
break;
case soon:
System.out.println("soon");
break;
case never:
System.out.println("never");
break;
}
}
public static void main(String args[])
{
question q=new question();
answer(q.ask());
answer(q.ask());
answer(q.ask());
answer(q.ask());
}
}

Notice that this program makes use of one of java standard classes: Random. This class provides psuedorandom numbers. It contains several methods which allow you to obtain random numbers in the form required by your program.In this example ,the method nextDouble()is used.It returns random numbers in the range of 0.0 to 1.0.

In this sample program, the two classes, question, and askme, both implement the shared constants interface where no, yes, maybe, soon, later and neverare defined and inherited from them directly. Here is the sample output when you run this program note that the result is different each time it is run.

later

soon

no

yes



Interface inheritance:

one interface can inherit another by using the keyword extends. The syntax is the same as for inheriting classes. when a class implements an interface that inherits another interface, it must provide an implementation for all methods defined within the interface inheritance chain. The following is an example,

interface A
{
void math1();
void math2();
}

interface B extends A
{
void math3();
}
class myclass implements B
{
public void math1()
{
Syustem.out.println("implements math1()");
}
public void math2()
{
Syustem.out.println("implements math2()");
}
public void math3()
{
Syustem.out.println("implements math3()");
}
}
class iterfaceinheritance
{
public static void main(Strin args[])
{
myclass m=new myclass();
m.math1();
m.math2();
m.math3();
}
}

Difference between Classes and interface:

Classes

Interfaces

Classes have an instance as variables and methods with body.

Interfaces have instances as abstract methods and final constants variable.
Inheritance goes with extends keyword.

Inheritance goes with implements keywords.
The variable can have any access specifier.

The variable should be public, static, final.
Multiple inheritances are not possible.

It is possible
Classes are keyword by putting the keyword class prior to the classname.

The interface is created by putting the keyword interface prior to the interface-name
Classes contain any type of methods. Classes may or may not provide the abstraction.
The interface contains mostly abstract methods. Interfaces are exhibited fully abstraction.














Difference between  Abstract class and interface:

Abstract class

Interface

An abstract class is a class which contains one or more abstract methods, which has to be implemented by its subclasses.
The interface is the java object containing method declaration but no implementation. The classes which implement the interfaces must provide the method definition for all the methods.

An abstract class is a class prefix with an abstract keyword followed by class definition.
The interface is a pure abstract class which starts with the interface keyword.

An abstract class can also contain concrete methods.
Whereas, the interface contains all the abstract methods and final variable declaration

Abstract classes are useful in a situation that some general methods should be implemented and specialization behavior should be implemented by the child class.

Interfaces are useful in a situation that all properties should be implemented.

No comments:

Post a Comment