interface in java what is interface in java interface in java with example example of interface in java use of interface in java interface example in java interface program in java define interface in java interface and abstract class in java interface in java tutorial interface in java examples interface in java example interfaces in java examples interfaces in java example interfaces in java with example example of interfaces in java interfaces example in java interfaces program in java
In java programming each and every program must start with concept called class.
Such that have two classes in java. They are
a) Concrete class
b) Abstract class.
Concrete class are always recommended to perform specific requirements but not
recommended to perform common requirements.
By using abstract class we can achieve common requirements, then we get advantage
over Concrete class regarding common requirements.
By using abstract class for dealing with common requirements, we come across the following disadvantages.
i) Abstract class never participates in multiple inheritance.
ii) Abstract class provides only common reusable features but not universal
common reusable features.
iii) We know that abstract class is collection of defined and undefined methods.
It is not recommended to override defined methods of abstract class and it is
recommended to override defined methods of abstract class and it is recommended to override the undefined method. i.e; abstract class containing both recommended and un recommended Features due to this we get less partial execution time.
Definition Of Interface:
Interface is a collection of universal common reusable data members and universal
common reusable methods.
An Interface is a collection of public static final Xxx data members and public
abstract methods.Here Xxx represents data type, variable name, variable value.
An Interface is a collection public static final data members and public abstract methods .
variable declaration with initialization;
In the above syntax interface is a keyword used for developing user defined data types.
<interface-name> represents a java variable name treated as name of interface.
Advantages of Interface:
If we develop any java application then we get the following advantages.
a) Interface participates in mu;tiple inheritance.
b) Interface features are by default belongs to universal common reusable.
c) Interface always provides completely less execution time because interface are purely
containing abstract methods and whose overhead cost of abstract method is very less.
d) Hence the scope of interface is more compared to abstract classes.
Points To Remember:
1) According to industry standards it is highly recommended to override instance
Methods because they are meant for performing repeated operations. But
not recommended to override static methods because static methods are
meant for performing one time operations.
2) Abstract methods of abstract class and interface must be instance methods
only but not static.
Inheriting the features of interface:
In java programming we have three approaches for inheriting the feature of interfaces into
derived classes / interfaces.
a) Inheriting the features into class by using Implements keyword:
1) This approach makes us to understand how to inherit the features of base class into
[abstract] class <class-name> implements <interface-1>,<interface-2>…..<interface-n>
method definition / declaration;
2) Implements is a keyword used for inheriting the features of base interface into derived
class can implements either one or more than one interface.
Because java programming never supports the concept of multiple inheritance through
the concept of classes, but it can supported through the concept of interfaces.
b) Inheriting the features into another interface by using extends keyword:
By using this approach how to inherit the features of base interface into derived interface.
This is a interface inheritance in which inheriting features of base interface into derived
interface is known as interface inheritance.
interface <interface-name> extends <interface-name1>,<interface-name2>….<interface-name n>
variable declaration with initialization;
c) Inheriting the features into one class from another classes and different interfaces by using extends and implements keyword:
By using this approach we parallel inherit from single base class and from base interfaces.
[abstract] class <class-name> extends <class-name 1> implements <interface 1>….<interface n>
Methods definition / declaration;
Note: when we use extends and implements keywords in single syntax or for getting parallel inheritance, it is mandatory to java programmer to write always first extends keyword then implements keyword.
Rules for using interface:
i) Interfaces are always contain universal common reusable features.
ii) In java programming final interface does not exists.
iii) Interfaces concept of java makes use of polymorphism along with method overriding
for business logic development and makes use of dynamic binding for execution logic.
iv) While we are defining interface methods must be preceded by public keyword
otherwise we get compile time error.
v) Interfaces contains only abstract instance methods but not abstract static methods.
vi) Interfaces concept of java does not contan constructor because of following reasons .
a) Interface data members are already initialized.
b) We know that constructors are special defined methods, so that defined
constructors are not permitted in interface
vii) Programmatically interface objects can be declared but cannot be referenced directly
and they can be referenced indirectly w.r.t concrete class only.
An object of an interface is equal to an object of its sub class
An object of an interface is equal to an object of that class which will implement that interface.
An object of sub class of interface is an object of an interface.