Like classes, interfaces can also be extended. That is, an interface can be sub-interfaced from other interface. This is achieved using the keyword extends as shown below:
syntax:
Interfaces used as super classes whose properties inherited by classes. It is therefore necessary to create a class that inherits the given interface. This is done as follows
syntax:
interface i2 extends i1
{
body of interface2
}
Example program
interface i1
{
void display();
}
interface i2 extends
{
void show();
}
class MyClass implements i2
{
public void display()
{
System.out.println(“ interface one method”);
}
public void show()
{
System.out.println(“ interface two method”);
}
}
class ExtendDemo
{
public static void main(String args[])
{
MyClass m=new MyClass();
m.display();
m.show();
}
}
Implementing interfacesInterfaces used as super classes whose properties inherited by classes. It is therefore necessary to create a class that inherits the given interface. This is done as follows
Syntax:
class classname implements interfaceName
{
body of the class
}
Example:
interface i1
{
void display();
}
interface i2 extends
{
void show();
}
class MyClass implements i2
{
public void display()
{
System.out.println(“ interface one method”);
}
public void show()
{
System.out.println(“ interface two method”);
}
}
class ImplementDemo
{
public static void main(String args[])
{
MyClass m=new MyClass();
m.display();
m.show();
}
}
0 comments :
Post a Comment
Note: only a member of this blog may post a comment.