Sie sind auf Seite 1von 11

Introduction

Interface is basically a kind of a class,


interfaces contain methods and variables.
Java does not support multiple inheritance,
interface is an alternate approach to
support the concept of multiple inheritance.

Difference between Class and Interface


Class can implement
more than one class.
Class contain design
and implementation
of code.
Contains constants,
variable and methods.
Class can be
instantiated

Interface can extend


more then one
interface .
Pure design
Contain constants and
abstract methods
Interface cant be
instantiated

Defining Interfaces
The general form of an interface is given below:
Access interface name
{
Return-type method-name1 (parameter-list);
Type final-varname1=value;
Return-type method-name2 (parameter-list);
Type final-varname2=value;
//.
Return-type method-name N (parameter-list);
Type final-varnameN=value;
}

Interface Implementation
Once an interface has been defined, any number of
classes can implement using that interface.
To implement an interface, we have to include to the
implements clause in a class definition and then add
the method defined by the interface.
The methods that implement an interface should be
declared public.
Java allows the classes that implement interfaces to
define additional members of their own.
If a class implements more than one interface, then
the interfaces are separated with a comma.

Accessing Interfaces
For accessing Interface, we can declare a
variables as object references of an interface
type. This variable can be used to store any
class instance that implements the declared
interface. Using this variable or the object
reference, if you call a method, then the
method will be invoked based on the actual
or dynamic object reference. This is one of
the key features of interface.

Packages
Java allows to group several class definitions
together in a logical grouping called Package.
Java packages are classified into two types:
a. Java API packages
b. User defined packages
Java API provides a large number of classes
grouped into different packages according to
functionality.

Creating Packages
Creating of our own packages, involves the following steps:
Declare the package at the beginning of the file using the
form: package packagename
Define the class that is to be put in the package and
declare it public.
Create sub directory , under the sub directory where the
main sources file are stored.
Store the listing as the classname.java file in the sub
directory created.
Compile the file using javac
Change the previous level directory and use it java
packagename.classname

Accessing the Packages


To access package import statement is used.

Syntax: import
package1[.package2][.package3].classname

Statement must end with a semicolon.


The import statement should appear before any

class definition in a source file.


Multiple import statement are allowed.

Conclusion
Interface is an alternate approach to
support the concept of multiple inheritance,
so we can use interface when we need
multiple inheritance concept in Java.
Classes contained in packages can be
reused. Way to hide classes, thus preventing
other programmers from accessing classes
meant for internal use. Providing a way of
separating design from coding.

Das könnte Ihnen auch gefallen