Go through this program once ...
I will explain the use of interface at the end...
Interface Interf
{
void m1();
void m2();
}
class InterfaceTest implements Interf
{
public void m1()
{
System.out.println("implementation m1");
}
public void m2()
{
System.out.println("implementation m2");
}
public void m3()
{
System.out.print("YOU WONDER ! I knew why????? I am not instatiated by interface instance I okkkk I am an instance of InterfaceTest ..... Well done you understand interfac concept now");
}
public static void main(String arg[])
{
Interf I=new InterfaceTest();
I.m1();
I.m2();
InterfaceTest b = (InterfaceTest)I;
b.m3();
}
}
Now forget the INTERFACE part in my program and follow me...it is simple..... Come on scroll...
If you want to reuse the method m1 and m2 in some other classes means , what would you do????
Way 1 : I can instantiate an object for class InterfaceTest at my class .
DISADVANTAGE : If you instantiate another object means its implicitly tells that you create an copy of InterfaceTest class. So it wont come under the category REUSE..
Way 2 : I can extend InterfaceTest in my class ...well done good but
DISADVANTAGE : If interfaceTest contains lot of methods other than m1 and m2 means you have to allocate memory for that methods also(waste na) in your class
Way 3 : USING INTERFACE >>>>>>
make m1 and m2 methods inside an interface (now see above program once again completely)
so you can accesses it from another class like extend but one small difference you are not pointing the unwanted method m3.....good luck...
Ask questions if you need clarifications...
I will explain the use of interface at the end...
Interface Interf
{
void m1();
void m2();
}
class InterfaceTest implements Interf
{
public void m1()
{
System.out.println("implementation m1");
}
public void m2()
{
System.out.println("implementation m2");
}
public void m3()
{
System.out.print("YOU WONDER ! I knew why????? I am not instatiated by interface instance I okkkk I am an instance of InterfaceTest ..... Well done you understand interfac concept now");
}
public static void main(String arg[])
{
Interf I=new InterfaceTest();
I.m1();
I.m2();
InterfaceTest b = (InterfaceTest)I;
b.m3();
}
}
Now forget the INTERFACE part in my program and follow me...it is simple..... Come on scroll...
If you want to reuse the method m1 and m2 in some other classes means , what would you do????
Way 1 : I can instantiate an object for class InterfaceTest at my class .
DISADVANTAGE : If you instantiate another object means its implicitly tells that you create an copy of InterfaceTest class. So it wont come under the category REUSE..
Way 2 : I can extend InterfaceTest in my class ...well done good but
DISADVANTAGE : If interfaceTest contains lot of methods other than m1 and m2 means you have to allocate memory for that methods also(waste na) in your class
Way 3 : USING INTERFACE >>>>>>
make m1 and m2 methods inside an interface (now see above program once again completely)
so you can accesses it from another class like extend but one small difference you are not pointing the unwanted method m3.....good luck...
Ask questions if you need clarifications...