Sunday, July 1, 2012

Java Interface Rules with Program

package in.ibm;

public class InterfaceRules {
   
//    Inter6 ref1 = new Inter6();    Error :  no constructor allowed for Interface

            // Object creation is not possible in Interface
   
    Inter6 ref;

   // We can create the ref of Interface but cannot create the objects as it is fully abstract class
   
    public static void main(String [] str){
       
    System.out.println(inter7.AB);
    System.out.println(inter8.BC);
    System.out.println(inter9.CA);
   
    new Test2().display();
    }
}

class A{}

interface inter1 {}

interface inter2 extends inter1 {}                 // allowed as interface can extend another interface

interface inter3 extends inter1,inter2 {}                  // allowed as interface allow multiple inheritance

//interface inter4 implements inter1{} // ERROR: as Interface only extend the interface can't implement it

//interface inter5 extends A{} // ERROR : class cannot be the super Interface for a interface

class B extends A implements inter1, inter2{}  // Allowed as class can implement multiple interface but can extend one and only one class

//class C extends A,B implements inter1, inter2,inter3{} // ERROR : as class tries to extend more than one class

interface Inter6{
       
//    inter6(){}         //ERROR: interface doesn't have any constuctor, NO NEED as interface doesn't have any instance member so what is the use of constructor
   
//    {}                 // ERROR : if no object is going to be created for interface do you see any use of object initialization block

//    static     { }     // as for the interface class is not going to be loaded so no need of static block as it will run only once when class is loaded
   
//    int AB;             // ERROR : by default var declared inside interface have signature public static final i.e.
               
// Var is not an instance variable and value is fixed so Compiler will not load class to instantiate the var as it is CONSTANT
   
    int AB=20;     // allowed as we are giving a value to final variable
   
    //void display(){}; ERROR: // by default member of interface is public abstract and Abstract can't have body
   
    int calculate(int a, int b); // Allowed as we are not specifying any body.

    // Method declared is instance member of the interface to access that you require some object that's why without inheriting we cannot

 //access the interface methods while member variables are by default static so no need of instances we can call them directly or with

    //interface name
   
    void display();
   
}


//class Test implements Inter6{}

 // ERROR : Class implementing any interface must implement all the methods of interface

abstract class Test implements Inter6{}

 // allowed as class is declared abstarct, any subclass of TEST have to implement the methods

class Test1 implements Inter6{

   
    //public float calculate(int a, int b) { }

//ERROR: As we are violating the overriding rules that return type should be same for overriden methods
   
    public int calculate(int a, int b) {
        return a+b;
    }

   
    public void display() {
            System.out.println ("Dispaly the result +" + calculate(5,10));
    }
   
}


interface inter7 {
    int AB=10;
    void display();
}

interface inter8 {
    int BC=20;
    int AB =30;

 // going to give ambigiuity in thr class which will implement both inter7 & inter8 as by inheritance rule only one copy od duplicate code shold go to

 // subclass and here we are sending 2 copies of AB to Test2
    void display();
}

 // allowed as interface can extend another interface

interface inter9 {
    int CA=30;
    void display();
}            


class Test2 implements inter7,inter8,inter9{

    //    void display(){ } // ERROR as here visibility is 'default' which is restrictive than by default visibility of methods of interfaces(public)
       
    public void displayValues(){

//        System.out.println(AB); // ambiguity Error

        System.out.println(inter7.AB);
        System.out.println(inter8.AB);
        System.out.println(BC);

        System.out.println(CA);   // no ambiguity so no error
    }

    public void display() {
        System.out.println("display methods inherited from all Interfaces");
       
    }
}

No comments: