• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Java Program using Interface | Important Points

Heena Rajpal/12171/0
CodeJAVA

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!

Java
Heena Rajpal
Heena Rajpal is pursuing Computer Science Engineering from Indore. She has a knack for writing and an inquisitive outlook towards Computer Science fields like Database Management Systems, Object Oriented Programming and languages like C, C++, JAVA, Python, HTML etc.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Interview Questions



You can share your interview experience.

Subscribe for FREE Newsletter

Do you want me to send you programing updates for FREE?

Subscribe below…

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard