Gadgets

Showing posts with label ocjp. Show all posts
Showing posts with label ocjp. Show all posts

Wednesday, 18 March 2015

Interfaces in java



Interface
Any service requirement specification (or) any contract between client and service provider (or) 100% pure abstract class is known as interface.
Example:-

JDBC API acts as requirement specification to develop DB Driver. DB vendor is responsible to provide implementation.
Example2:-
Banks provide ATM services to the client, here ATM act as contract between client and bank.
Interface Methods:-
In interface we can take only abstract methods only and interface is consider as 100% pure abstract class.
Every method present inside interface is always public and abstract whether we are declaring or not.
interface InterfDemo {
            public abstract void add();
            public abstract void sub();
            void mul();//by default considered as public abstract
}
Why public and abstract:-
Public: Access this methods to everywhere in the implementation classes.
Abstract: Implementation class is responsible to provide implementation.
Interface variables:-
Inside interface we can declare variables, the main purpose of interface variables is to define requirement level constant.
Every interface variable public static final whether we declared or not. We should perform initialization at time of declaration only.

interface InterfDemo {
            public static final int a=10;
            int b=20;//by default considered as public static final
            public abstract void add();
}
Why public static final:-
Public: Access this variable in every implementation class.
Static: Without existing object, Implementation classes can access these variables.
Final: Implementation class can access but not modify. If one class will modify then remaining classes will be affected.
Which of the following method and variable declarations are valid and invalid?
interface InterfDemo {
            public static final int a=10;//valid
            static String s="interface";//valid
            private int i=10;//invalid
            int b=20;//valid
            final public char ch='a';//valid
            double d=10.5;//valid
           
            public abstract void add();//valid
            void sub();//valid
            abstract void mul();//valid
            public abstract native void div();//invalid
            private void msg();//invalid
}
Method Naming Conflicts:-
Case-1:-
If two interfaces contain same method with same signature and same return type then in the implementation class one method implementation is enough.
interface DemoOne {
            public abstract void add();
}
interface DemoTwo {
            public abstract void add();
}
class InterfDemo implements DemoOne,DemoTwo {
            public void add() {
                        //implementation                  
            }
}
Case-2:-
If two interfaces contain same method with different arguments and same return type then in the implementation class we have to implementation two methods, this two methods act as overloaded methods.
interface DemoOne {
            public abstract void add();
}
interface DemoTwo {
            public abstract void add(int i);
}
class InterfDemo implements DemoOne,DemoTwo {
            public void add() {
                        //implementation                  
            }
            public void add(int i) {
                        //implementation                  
            }
}

Variable Naming Conflicts:-
If two interfaces can contain a variable with same name then may be a chance of variable naming conflict. We can solve this problem by using interface name.
interface DemoOne {
            public static final int i=10;
}
interface DemoTwo {
            public static final int i=20;
}
class InterfDemo implements DemoOne,DemoTwo
{
            public static void main(String arg[]){
                        System.out.println(DemoOne.i);
                        System.out.println(DemoTwo.i);
            }
}
Marker Interface:-
If an interface doesn’t contain any methods and by implementing that interface our object will get some ability, such type of interface are called marker (or) tag (or) ability interface.
Example:- Serializable, Cloneable, RandomAccess, SingleThreadModel ect.
Without having method how the object will get ability in marker interface?
The above interface doesn’t contain any methods but JVM is responsible to provide required ability internally.
Why JVM is Responsible to Provide Ability to Marker interface?
Because to reduce complexity of the programming.
Is it possible to create our own marker interface?
Yes, we can create our own marker interface but our customization JVM is required.

Example-1:- By implementing Serializable interface we can able to save object to the file and we can able to send across the network.
Example-2:- By implementing Cloneable interface we can create exactly duplicate cloned objects.
Adapter Class:-
Adapter class is a simple java class that implements an interface with only empty implementation.
Advantage of Adapter Class:-
If we want implement an interface directly for each and every of that interface compulsory we should provide implementation whether it is required or not.
interface Demo {
            public abstract void m1();
            public abstract void m2();
            ;;;;;;;;;;;;;
            public abstract void m100();
}
class Test1 implements Demo
{
            public void m1(){
                        //implementation
            }
             //provide implementation to all methods from m1() to m100()
            public void m100(){
            }
           
}
class Test2 implements Demo
{
            public void m1(){                  
            }
            public void m2(){
                        //implementation
            }
             //provide implementation to all methods from m1() to m100()
            public void m100(){
            }
           
}
//class Test3 to implement m3()...Test100 to implement m100()

The above program compulsory we provide implementation to all methods in every class, so it will increase length of the code and decrease readability.
To overcome the above problem we use adapter classes. By using adapter class we provide empty implementation to all methods in adapter class only and then use required methods in other class, so it will improve readability and reduce length of the code.
interface Demo {
            public abstract void m1();
            public abstract void m2();
            ;;;;;;;;;;;;;
            public abstract void m100();
}
abstract class AdapterDemo implements Demo
{
            //only empty implementation
            public void m1(){
            }
            public void m2(){
            }
            ;;;;;;;;
            public void m100(){
            }
           
}
class Test1 extends AdapterDemo
{
            public void m1(){      
                        //implementation
            }
}
class Test50 extends AdapterDemo
{
            public void m50(){    
                        //implementation
            }
}
class Test100 extends AdapterDemo
{
            public void m100(){  
                        //implementation
            }
}
In the above program we can implement required methods only.
Marker interface and adapter classes are big utilities to the programmer.
Interface: it is for build planning and doesn’t contain any implementation.
Abstract: it contains partial implementation.
Concrete(our own) class: it contains fully implementation.


Difference between interface and abstract classes:-
Interface:-
We build plan only, it contain method declaration only doesn’t contain any implementation.
In side interface we can take only abstract methods, so it is considered as 100% abstract class.
Every method inside interface is public abstract and variables are public static final only, it doesn’t allow other modifiers.
For interface variables compulsory we should perform initialization at the time of declaration.
interface Demo{
            public abstract void m1();
            public static final int i=10;
}
Inside interface we can’t declare instance, static blocks and constructors.
While implementing interface we can extend any other class.
class Demo implements Demo1,Demo2{
}
We can implements any number of interfaces simultaneously.
class Demo implements InterfDemo extends Test{
}
No object creation for interface.
Abstract class:-
Abstract class contains abstract methods and normal implementation methods (i.e. it contains both abstract and concrete methods) so that it is partial implementation.
abstract class Demo
{
public abstract void m1();
void m2(){
            System.out.println("hello");
}
}
Every variable inside abstract is need not be public static final.
No need to initialize variables inside abstract class.
Abstract class allows static, instance block and constructor.
While extending abstract class we can’t extend other classes.
No object creation here but abstract constructor is executing through implementation class object.

Extend versus implementation:-
A class extends only one class at a time.
class A extends B
{
           
}
A class can implement any number of interfaces simultaneously.
class B implements C,D,E
{
}
A class can extend one class and implement any number of interfaces simultaneously.
class A extends B implements C,D,E
{
}
An Interface can extend any number of interfaces simultaneously.
interface A extends B,C,D,E
{
}

Example for interface
interface DemoOne{
public abstract void add();
public abstract void mul(int i);
}
interface DemoTwo{
            public static final String s="interface demo";
            public abstract void add();
            public abstract void sub();
}
class InterfDemo implements DemoOne,DemoTwo{
            int a=20,b=10;
            public void add(){
                        System.out.println("addition : "+(a+b));
                        System.out.println(s);
            }
            public void sub(){
                        System.out.println("subtraction : "+(a-b));
                        System.out.println(s);
            }
            public void mul(int i){
                        System.out.println(i);
                        System.out.println(s);
            }
            public static void main(String[] arg){
                        InterfDemo id=new InterfDemo();
                        id.add();
                        id.sub();
                        id.mul(50);
            }
}

Example for Adapter Classes  
interface InterfOne{
public abstract void div();
public abstract void mul(int i);
}
interface InterfTwo extends InterfOne{
            public static final String s="interface demo";
            public abstract void add();
            public abstract void sub();
}
abstract class AdapterDemo implements InterfOne,InterfTwo {
            public void div(){}
            public void mul(int i){}
            public void add(){}
            public void sub(){}

}

class Test extends AdapterDemo
{
            int a=20,b=10;
            public void add(){
                        System.out.println("addition : "+(a+b));
                        System.out.println(s);
            }
}

class Test1 extends AdapterDemo
{
            int a=20,b=10;
            public void sub(){
                        System.out.println("subtraction : "+(a-b));
                        System.out.println(s);
            }
}

class Test2 extends AdapterDemo
{          
           
            public void mul(int i){
                        System.out.println(i);
                        System.out.println(s);
            }
            public void div(){
                        System.out.println(s);
            }
            public static void main(String[] arg){
                        Test t=new Test();
                        t.add();
                        Test1 t1=new Test1();
                        t1.sub();
                        Test2 t2=new Test2();
                        t2.mul(50);
                        t2.div();
            }
}