Skip to main content

Posts

Showing posts from October, 2016

Interface-1

Interface: -I nterface is a blue print just like a class. Interface is mainly used for achieving abstraction. Interface contains public static and final variables and public abstract methods. An interface can inherit by another interface. An interface can inherit more than one interface but a class cannot inherit interface, class can implements one or more than one interfaces. Instantiation of interface is not possible, we can create object of implemented class so interface reference can hold object of all implemented classes. All the variables of interface are by default public static and final, all the methods of interface are automatically public and abstract. Interface may be empty, called marker interface. interface A { } Interface can contains only variables. interface A { int a=100; int b=200; } Interface can contains only abstract methods. interface A { void disp(); void sum(); } Interface can contain variables and abstract m

Interface-2

Example1:-    A.java interface A { int a; } Output:- C:\JAVATECH>javac A.java A.java:3: error: = expected int a;      ^ 1 error Explanation: - Interface variable must initialize at declaration time because by default interface variables are public static and final. Example2:-    A.java interface A { int a=100; } Output:- C:\JAVATECH>javac A.java C:\JAVATECH>javap A Compiled from "A.java" interface A {   public static final int a; }  Explanation: - by default interface variables are public static and final. Example3:-    A.java interface A { public static final int a=100; } Output:- C:\JAVATECH>javac A.java C:\JAVATECH>javap A Compiled from "A.java" interface A {   public static final int a; } Example4:-    A.java interface A { static public final int a=100; } Output:- C:\JAVATECH>javac A.java C:\JAVATECH>javap A Compiled from "

Interface-3

Example6:-    A.java interface A { void disp(); } Output:- C:\JAVATECH>javac A.java C:\JAVATECH>javap A Compiled from "A.java" interface A {   public abstract void disp(); } Explanation: - Interface contains only abstract methods only and all interface methods are by default public and abstract. Example7:-    A.java interface A { public abstract void disp(); } Output:- C:\JAVATECH>javac A.java C:\JAVATECH>javap A Compiled from "A.java" interface A {   public abstract void disp(); } Example8:-    A.java interface A { abstract public void disp(); } Output:- C:\JAVATECH>javac A.java C:\JAVATECH>javap A Compiled from "A.java" interface A {   public abstract void disp(); } Example9:-    A.java interface A {   void disp();   int set(); } Output:- C:\JAVATECH>javac A.java C:\JAVATECH>javap A Compiled from "A.java"

Interface-4

Example11:-    Test.java interface A { void disp(); } class Test implements A { } Output:- C:\JAVATECH>javac Test.java Test.java:5: error: Test is not abstract and does not override abstract method disp() in A class Test implements A ^ 1 error Explanation: - Interface methods must be override in implemented class otherwise class must be declared as abstract. Example12:-    Test.java interface A { void disp(); } abstract class Test implements A { } Output:- C:\JAVATECH>javac Test.java C:\JAVATECH>javap Test Compiled from "Test.java" abstract class Test implements A {   Test(); } Example13:-    Test.java interface A { void disp(); } abstract class Test implements A { void disp() { System.out.println("Implemented method"); } } Output:- C:\JAVATECH>javac Test.java Test.java:7: error: disp() in Test cannot implement disp() in A void disp()      ^  

Interface-5

Example16:-    TestMain.java interface A { void disp(); } class Test implements A { public void disp() { System.out.println("Implemented method"); } } class TestMain { public static void main(String[]args) { Test t=new Test(); t.disp(); } } Output:- C:\JAVATECH>javac TestMain.java C:\JAVATECH>java TestMain Implemented method Example17:-    TestMain.java interface A { void disp(); } class Test implements A { public void disp() { System.out.println("Implemented method"); } } class TestMain { public static void main(String[]args) { A t=new Test(); t.disp(); } } Output:- C:\JAVATECH>javac TestMain.java C:\JAVATECH>java TestMain Implemented method Explanation: - we cannot create object of interface but interface reference variable can hold object of implemented class. Example18:-    TestMain.java interface A { void disp(); } class Test