interface printable{ void print(); } class splesson implements printable{ public void print(){System.out.println("Hello Splesson.");} public static void main(String args[]){ splesson obj = new splesson(); obj.print(); } }
Where printable is an interface and it has only one method that is print().
interface printable{ void print();}
Output:
When compile the code the result will be as follows.
Hello Splesson.
package com.spl.absI; public class AbstractInterface { public static void main(String[] args) { System.out.println("Program starts"); DerivedClass dref=new DerivedClass(); dref.test1(); dref.test2(); System.out.println("Program ends"); } } interface Sample1 { void test1(); void test2(); } abstract class A implements Sample1 { public void test1() { System.out.println("test1() method"); } } class DerivedClass extends A { @Override public void test2() { // TODO Auto-generated method stub System.out.println("test2() method"); } }
Program starts test1() method test2() method Program ends
interface Sample1{ void test1(int a); } interface Sample2 extends Sample1{ void test2(); }
For more detailed overview on inheritance click here .
package com.spl.concrete; public class Interface3 { public static void main(String[] args) { System.out.println("Program starts"); B dref=new B(); dref.test1(); System.out.println("Program ends"); } } class BaseClass { public void test1() { System.out.println("Running test1() mehod"); } } interface Sample1 { void test1(); } class B extends BaseClass implements Sample1 { //inheriting from both Sample1 and BaseClass }
In the above example, the class B is becoming concrete because the test 1() method inherited from base class is full filling the contract of Sample 6 interface.
Output:
Program starts Running test1() method Program ends
package com.spl.int4; public class Interface4 { public static void main(String[] args) { System.out.println("Program starts"); E dref=new E(); dref.test1(); System.out.println("Program ends"); } } interface Sample7 { void test1(); } interface Sample8 { void test1(); } class E implements Sample7,Sample8 { @Override public void test1() { // TODO Auto-generated method stub System.out.println("Running test1() method"); } }
In the above example, the class is becoming concrete by overriding only test1() method.The overrided method fulfills the contract of both Sample 7 & Sample 8 interface,because the method signature are same.
Output:
When compile the code output will be as follows.
Program starts Running test1() method Program endsThe following is an example to achieve multiple inheritance by using interface.
package com.splessons; interface Printable{ void print(); } interface Showable{ void show(); } class Splesson implements Printable,Showable{ public void print(){System.out.println("Hello");} public void show(){System.out.println("Welcome To Splessons");} public static void main(String args[]){ Splesson obj = new Splesson(); obj.print(); obj.show(); } }
In the above example, Printable and Showable are the two interfaces that have their own methods and where Splesson is the class name that implements two interfaces(there is no ambiguity).
Output:
Hello Welcome To Splessons