Java Program using Interface | Important Points

Java Program using Interface | Important Points

Java Program using Interface | Important Points

In this Java programming article, I will discuss Java Program using Interface and its syntax. We also see some java interface misconception and common questions asked in interviews about it.

Java Program using Interface

Q: What is Interfaces in Java?
Interfaces are the blueprints of a class. Blueprints in the sense that they provide an abstract version or declaration of everything present in a class.

Hence, the purpose of having an interface is achieving 100% abstraction.

Important Points of Java Program using Interface:

Here are some Important points that java programmer should be aware of.

  • It has static constants.
  • All the members of an interface are ‘final’ by default.
  • All the member methods of an interface are abstract by default

Q: Does Multiple Inheritance Allow in Java?

Java does not allow multiple inheritances using classes. However, using interfaces, we can implement multiple inheritances since it is allowed in interfaces.

Syntax of an interface:

Interface InterfaceName
{
//interface data members
//interface member functions (declarations only)
}

The methods (member) functions are always declared in the interfaces. The definition is given in the class where the interface is implemented. However, there is an exception to this condition which is discussed later in this article.

Syntax for implementing interface:

Class ClassName implements InterfaceName
{
//definitions of functions mentioned in the interface
}

Usually, the interfaces are named with a suffix –able. For e.g.: the inbuilt interfaces Clonable, Throwable etc. However, it is not a compulsion. No error message will be generated if the interfaces are not named with this suffix.

Example for Java Program using Interface:

Let us have a look at an example of interface:

interface Square
{
double p=2;
double square(double x);
}
Class SquareImpl implements Square
{
public double square(double x)
{
//p=p+1; This will give an error since 
//p is final by default 
//as it is declared in the interface.
return Math.pow(x,p);
}
}
Here the above interface:
interface Square
{
double p=2;
double square(double x);
}
Shall be converted to the following by the compiler:
interface Square
{
public static final double p=2;
public abstract double square(double x);
}

Hence it gives us the kind of abstraction that we need.

Type of Functions defined within Java Interface:

Now there are two kinds of functions that can be defined within the interface:

  • 1. Static functions
    Defining static functions within the interface will not lead to an error condition.
  • 2. Default functions
    This facility is available only in the versions of java after and including 1.8.

Here default is not an access specifier. It is used to define a kind of function.

For e.g.:

public default void display()
{
}

Here in Java program using interface, public is the access specifier while the default is a type of method.

Q: What are marker interfaces and cloneable interfaces in java?

There are some interfaces predefined in Java those are blank, i.e., they neither have data members nor member functions.

For e.g.: Serializable, Remote, Cloneable etc.

They are called marker interfaces or tagged interfaces. They are used to perform some important information to JVM for performing necessary operations.

For e.g.: Only objects of the classes that implement Serializable interface is serializable. Any general object cannot be serialized.

Hence, the job of the marker interface was to mark the objects of a particular class as serializable so that JVM can perform the required operations on them which are not possible on a general object.

Same goes for the Cloneable interface. It facilitates cloning of objects of a particular class.

That is all about Java program using interface.

You can read my previous article about Java Program for Exception Handling.

The more you implement, the more you learn. Keep practicing and experimenting!

Leave a Reply

Your email address will not be published. Required fields are marked *