Java2all.com  

Extending interfaces





Share This Topic -

Extending interfaces :

One interface can inherit another by use of 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 implementation of all methods defined within the interface inheritance.

Note : Any class that implements an interface must implement all methods defined by that interface, including any that inherited from other interfaces.

EX :

 interface if1
{
	void dispi1();
}
interface if2 extends if1
{
	void dispi2();
}
class cls1 implements if2
{
	public void dispi1()
	{
		System.out.println("This is display of i1");
	}
	public void dispi2()
	{
		System.out.println("This is display of i2");
	}
}
public class Ext_iface 
{
	public static void main(String args[])
	{
		cls1 c1obj = new cls1();
			
		c1obj.dispi1();
		c1obj.dispi2();
	}

}

 
Output :
 
This is display of i1
This is display of i2

Note : We have to define disp1() and disp2() in cls1.

Multiple inheritance using interface..

 class stu
{
	int rollno;
	String name = new String();
	int marks;
	stu(int r, String n, int m)
	{
		rollno = r;
		name = n;
		marks = m;
	}
}

interface i
{
	void display();
}

class studerived extends stu implements i
{
	studerived(int r, String n, int m)
	{
		super(r,n,m);
	}
	public void display()
	{
		System.out.println("Displaying student details .. ");
		System.out.println("Rollno = " + rollno);
		System.out.println("Name = " + name);
		System.out.println("Marks = " + marks);
	}
}

public class Multi_inhe_demo 
{
	public static void main(String args[])
	{
		studerived obj = new studerived(1912, "Ram", 75);
		obj.display();
	}

}

 
Output :
 
Displaying student details .. 
Rollno = 1912
Name = Ram
Marks = 75

We can make various forms of interface implementation as below

Java2All.Com