Java Interface.
Java Interface generally
- An interface can contain abstract method declarations, default methods, static methods and constant definitions.
-
Interfaces is reference types and cannot be instantiated as we can do with classes.
(the exception is when we are creating an anonymous inner class – see menu selection) - It is up to the classes which are implementing the interface(s) to implement statements for the interface(s) methods.
-
A number of common functionality can be imposed
on a number of different classes this way:
Interface example:
interface Radio { boolean hasRadio(); } class Car implements Radio { @Override public boolean hasRadio() { // Implemented method from the interface Radio: return true; } } class Cow implements Radio { @Override public boolean hasRadio() { // Implemented method from the interface Radio: return false; } } public class RadioApp { public static void main(String[] args) { Car myCar = new Car(); Radio r = myCar; System.out.println(r.hasRadio()); Cow myCow = new Cow(); Radio c = myCow; System.out.println(c.hasRadio()); } }
The result of this is:true false
© 2010 by Finnesand Data.
All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the
correctness in the pages or source codes.
The risk of using this web-site pages or any program
codes from this website is entirely at the individual user.