Write about Interfaces in Java

Interfaces: Java doesn’t support multiple inheritance . classes in java can’t have more than one superclass. Java provi...

Interfaces:

Java doesn’t support multiple inheritance . classes in java can’t have more than one superclass. Java provides an alternate approach known as interfaces, to support the concept of multiple inheritance. Although a java class cannot be a subclass of more than 1 superclass but it can implement more than 1 interface.

Defining interfaces:

An interface is basically a kind of class like classes, interfaces contain methods and variables but with a major difference. The difference is that interfaces define only abstract methods and final fields. This means that interfaces do not specify any code to implement. These methods and data fields contain only contents. Therefore it is the responsibility of the class that implements in interface to define the code for implementation of these methods.

The syntax for defining an interface is very simple to that for defining a class. The general form of an interface definitions.

interface interfaceName
{
Variables declaration;
Method declaration;
}

Here, interface is the keyword and interface name is any valid java variable (just like class names). Variables are declared as follows.


interface

Example:

interface Item
{
static final int code=1001;
static final string name=”fan”;
void display( );
}

Note that the code for the method is not included in the interface and the method declaration simple ends with a semicolon.

The class that implements the interface must define the code for that method.

Extending interfaces:

Like classes, interfaces can also be extended that is, an interface can be subinterfaced from other interfaces . the new sub interface will inherit all the members of the super interface in the manner similar to subclasses. This is achieved using the keyword extends as shown below.

Interface name 2 extends name 1
{
Body of name 2
}

For example, we can put all the constants is one interface and the methods in the other. This will enable to use the constants in classes where the methods are not receipted.
Interface item extends
{
Int code=1001;
String name=”fan”;
}
Interface item extends item Constants
{
Void display( );
}
The interface item would inherit both the constants code and name into it. Note that the variables name and code are declared like simple variables. It is allowed because all the variables in an interface are created as contents although the keywords final and static are not present.
While interfaces are allowed to extend to other interfaces sub interfaces cannot define the methods declared in the sub interfaces. After all, sub interfaces are still interfaces, not classes.
Note that when an interface extends two or more interfaces, they are separated by commas.
 Implementary interfaces:
Interfaces are used as “superclass” whose properties are inherited by classes. It is therefore necessary to create a class that inherits the given interfaces. This is done as follows.
Class classname implements interfacename
{
Body of className
}
Here the class classname “implements” the interface interfacename./.a more general form of implementation may look like this.
Class classname extends superclass
Implements interface1, interface 2….
{
Body of className
}
This shows that a class can extends another class while implementing interfaces.
Note that if a class that implements an interface does not implement all the methods of the interface, then the class becomes an abstract class and cannot be instantiated.
Accessing interface variables:
Interfaces can be used to declare a set of constants that can be used in different classes. The constant values will be available to any class that implements the interface. The values can be used in any method as part of any variable declaration or anywhere we can use a final value.
Example:
Interface Area
{
Final static float pi=3.14 F;
Float compute (float x, float y);
}
Class rectangle implements Area
{
Public float compute (float x, float y)
{
Return(x*y);
}
}
Class circle implements Area
{
Public float compute (float x, float y)
{
Return(pi*x*x);
}
}
Class interfaceTest
{
Public static void main(string args[ ] )
{
Rectangle rect=new rectangle( );
Circle cir=new circle( );
Area area;
Area=rect;
System.out.println(“area of rectangle=”+area.compute(10, 20));
Area=cir;
System.out.println(“area of circle=”+area.compute(10, 20));
}
}

Various forms of implementing interfaces:


Various forms of  interfaces3


      

Various forms of  interfaces2

   

Various forms of  interfaces1

   

Various forms of  interfaces

   
Example for implementing multiple inheritance:
Class student
{
int rollNumber;
void getnumber(int n)
{
rollnumber=n;
}
Void putNumber( )
{
System.out.println(“roll no is”+roll number);
}
}
Class test extends student
{
Float part1, part2;
Void getmarks(flaot m1, float m2)
{
Part1=m1;
Part2=m2;
}
Void putmarks( )
{
System.out.println(“marks obtained”);
System.out,println(“part1=”+part1);
System.out.println(“part2=”+part2);
}
}
Interface sports
{
Flaot sportwt=6.0F;
Void putwt( );
}
Class results extends test implements sports
{
Float total;
Public void putwt( )
{
System.out,.println(“sports wt=”+sportwt);
}
Void display( )
{
total=part1+part2+sports;
putNumber( );
putMarks( );
putwt ( );
System.out.println(“total score=”+total);
}
}

class Hybrid
{
public static void main(String args[ ] )
{
Results student1=new Results( );
student1. getNumber(1234);
student1.getMarks(27.5F, 33.0 F)
student1.display( );
}
}

Post a Comment

emo-but-icon

item