0% found this document useful (0 votes)
8 views7 pages

Understanding Java Interfaces and Implementation

Uploaded by

shravyar1626
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Understanding Java Interfaces and Implementation

Uploaded by

shravyar1626
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Interfaces

An Interface defines a set of methods that will be implemented by a class. An interface is a


construct that describes functionality without specifying implementation.

The general form of a creating an interface:

Here, access is either public or not used. When no access modifier is included, then default access results,
and the interface is available only to other members of its package.
When it is declared as public, the interface can be used by any other code. name is the name of the
interface and can be any valid identifier.
In the traditional form of an interface, methods are declared using only their return type and signature.
They are, essentially, abstract methods. Thus, each class that includes such an interface must implement
all of its methods. In an interface, methods are implicitly public.

Variables declared in an interface are not instance variables. Instead, they are implicitly public, final,
and static and must be initialized. Thus, they are essentially constants.

Implementing Interfaces
Once an interface has been defined, one or more classes can implement that interface. To implement
an interface, follow these two steps,
1. In a class declaration, include an implements clause that specifies the interface being implemented.
2. Inside the class, implement the methods defined by the interface.

The implement clause specifies the name of an interface that a class will
implement. The general form of a class that includes the implements
clause looks like this:
Example:

interface a
{
void meth1();
void meth2();
}
class b implements a
{
public void meth1()
{
[Link]("hi");
}
public void meth2()
{
[Link]("hello");
}
}
class c implements a
{
public void meth1()
{
[Link]("java");
}
public void meth2()
{
[Link]("java programming");
}
}
class inter
{
public static void main(String[] args)
{
c c1=new c();
c1.meth2();
b b1=new b();
b1.meth1();
}
}
The key point is that once an interface has been defined any number of class can implement the interface.
Interfaces can be extended
One interface can inherit another by use of the keyword extends. The syntax is the same as for
inheriting classes. When a class implements an interface that inherits another interface, it must provide
implementations for all methods required by the interface inheritance chain. Following is an example

In this example, interface A is extended by interface B. and then Myclass implements B. This means
Myclass must implement all of the methods defined by both interfaces A and B.

you might try removing the implementation for meth1( ) in MyClass. This will cause a compile-time
error. As stated earlier, any class that implements an interface must implement all methods required by that
interface, including any that are inherited from other interfaces.
Implementing multiple interfaces
interface a
{
void dosomething();
}
interface b
{
void dosomethingelse();
}
class myclass implements a,b
{
public void dosomething()
{
[Link]("do something");
}
public void dosomethingelse()
{
[Link]("do something else");
}
}
class interdemo
{
public static void main(String[] args)
{
myclass m1=new myclass();
[Link]();
[Link]();
}
}

Nested interface
An interface can be declared a member of another interface or of a class. such an interface is called a
member interface and nested interface.

An interface nested in a class can use any access modifier. An interface nested in another interface is
implicitly public.

When a nested interface is used outside of its enclosing scope, it must be qualified by the name of the
class or interface of which it is a member.
interface a
{
void meth1();
interface nested
{
void meth2();
}
}
class myclass implements [Link]
{
public void meth2()
{
[Link]("java program");
}
}
class indemo
{
public static void main(String[] args)
{
[Link] aa=new myclass();
aa.meth2();
}
}

constants and variables in interface


Variables can be declared in an interface, such variables are not instance variables. instead, they are
implicitly public, static, and final and must be initialized. Thus they are essentially constants.

To define a set of shared constants, create an interface that contains only these constants, without any
methods. Each file that needs access to the constants simply “implements” the interface. This brings the

constants into view. Here is an example:


write down the difference between abstract class and interface with example.

abstract class interface.


Abstract class can have abstract and non- Interface can have only abstract methods.
abstract methods.
An abstract class may contain non-final Variables declared in a Java interface are by
variables. default final.
Does not support multiple inheritance support multiple inheritance
Abstract class can have final, non-final, static Interface has only static and final variables.
and non-static variables.
abstract class can be extended using keyword A Java interface can be implemented using
“extends”. keyword “implements”
A Java abstract class can have class members Members of a Java interface are public by
like private, protected, etc. default.

Example of your choice

You might also like