Interface
Java
doesn’t support multiple inheritance (means you can’t extends more than one
class to a single class e.g. class
derived extends base1,base2) .
But there
is often requirement of program to inherit a class from more than one class.
Then Interface
comes into picture. And as we “extends” the class similarly we “implements”
interface.
Interface:
--- it is from of collection of abstract method (methods without definition). Keyword
interface is used to declare an interface.
NOTE :--
whenever you implements a interface in any class the function which is declared
in interface must be overrided in
derived class.
NOTE :-- if
you are implementing interface and extending any class simultaneously make sure
extends is before implements.
Example:--
interface base1
{
public
void show();
}
interface base2
{
public
void disp();
}
class normal
{
void
display()
{
System.out.println("this
is normal class");
}
}
class derived extends normal implements
base1,base2
{
public
void show() //overriding show()
{
System.out.println("this
is base1 interface method");
}
public
void disp() //overriding disp()
{
System.out.println("this
is base2 interface method");
}
}
public class abc
{
public
static void main(String as[])
{
derived
d=new derived();
d.show();
d.disp();
d.display();
}
}
No comments:
Post a Comment