Interfaces are pure abstract classes i.e, all the methods in an interface are abstract methods only.
Hence interface is also known as "Skeleton-class ".
Interface is a keyword used to declare it.
Ex
interface A
{
void m1();
void m2();
}
Here keyword abstract is optional while declaring abstract methods of an interface.
inheritance can contain data members.By default, these data members are public static final.
Ex
interface A
{
public static final int a=10;
public static final in b=20;
int c=30;
void m1();
void m2();
}
Such data members can be access directly with interface name.
Interface object can't be created because it is purely incomplete class.
A obj=new A(); //Error..
How to use interface
Since interface object can't be created,we have to inherit the interface.
Implement is a keyword using which we inherit an interface.
The class which inherits interface should be redefined all the abstract methods or interface.
The class which inherits interface should redefine all the abstract methods or interface otherwise such sub-class also becomes an incomplete class or abstract class and this class can't be instantiated.
Ex
class B implements A
{
int x,y;
void m1()
{
System.out.println("m1() Method");
}
void m2()
{
System.out.printnln("m2() Method");
}
void m3()
{
System.out.println("m3() method");
}
}
class InterfaceExample
{
public static void main(String args[])
{
//A obj=new A(); //Error..
A a;
System.out.printnln("A value="+A.a);
System.out.printnln("B value="+A.b);
System.out.printnln("C value="+A.c);
a=new B();
a.m1();
a.m2();
a.m3();
B b=new B()
b.x=11;
b.y=22;
System.out.printnln("X value="+b.x);
System.out.printnln("Y value="+b.y);
b.m1();
b.m2();
b.m3();
}
}
Output
Ex
//program related to interfaces
Interface interface1
{
/*final*/ int x=10;
Static int x=20; // by default its final,public.
Void method1();
Void method2();
/*void method3() //error—because no complete method
{
System.out.println(“Inside method3()”);
}
*/
Class InterfaceExample implement Interface1
{
Public void method1()
{
System.out.println(“Inside method 1()”);
}
Public void method2()
{
System.out.printnln(“Inside method2()”);
}
Public static void main(String args[])
{
Interface obj1=new Interface1(); //error no-object
Interface1 obj1;
InterfaceExample obj2=new interfaceExample();
Obj2.method1();
Obj2.method2();
//obj2.x=obj2.x+10 //if final in interface
System.out.println(“X:::”+obj2.x);
//Interface1.y=Interface1.y+10; //by default its final
//obj2.y=obj2.y+10; //by default its final
System.out.println(“y:::”+ Interface1.y);
Obj=obj2;
Obj.method1();
Obj.method2();
Interface1 obj3=new InterfaceExample();
Obj3.Method1();
Obj3.method2();
System.out.println(obj3);
}
}
Output
Multiple Inheritance in Java through interfaces.
Java doesn't support multiple inheritance through classes i.e, 2 or more base classes giving derivation to one sub-class. i.e not possible in Java.
However, this concept can be implemented through interfaces.
Ex
class C extends A implements B
Here Sub-Class 'C' should redefine all the abstract methods of base-class as well as interface otherwise such class becomes an abstract class and it can't be instantiated.
Note:
i)A class extends class for inheritance
Ex
class B extends A
{---
---
}
ii)A class implements interface for inheritance.
Ex.
class implements A
{---
----
}
iii)An interface extends another interface for inheritance.
Ex
Interface B extends A
{
---
---
}
iv) Interface can't inherit class.
This case is not possible.
Because class contains complete methods and such methods are inherited to interface but interface can’t have complete methods.
Ex
//Multiple inheritance through interface
interface Interface1
{
void method1();
void method2();
}
class Example1
{
void method3()
{
System.out.println("Inside Method3()");
}
}
class MultipleInheritance extends Example1 implements Interface1
{
public void method1()
{
System.out.println("Inside Method()");
}
public void method2()
{
System.out.println("Inside method2()");
}
public static void main(String args[])
{
//Interface1 obj=new Interface1()
Interface1 obj;
MultipleInheritance obj2=new MultipleInheritance();
obj2.method1();
obj2.method2();
obj2.method3();
obj=obj2;
obj.method1();
obj.method2();
//obj.method3();
Interface1 obj3=new MultipleInheritance();
obj3.method1();
obj3.method2();
//obj3.method3();
System.out.println(obj3);
//Reference variable of Example1 class
Example1 obj4=new MultipleInheritance();
//obj4.method1();
//obj4.method2();
obj4.method3();
}
}
Hi Friends, Please comment down your views in the comment section below. Thank you...