Part 11: Java Interface tutorials with Example.

JAVA

Interface in Java

Interface is a similar kind of java class. It can be called as collection of abstract methods or another way of achieving abstract method as it contains empty bodies. It can declare as:  interface <any-name>{}.

Example
/* interface */
interface Box

{

/*interface method does not have a body */
public void boxHieght();

/*interface method does not have a body */
public void run();
}

Interface Feature:

  1. Class can contains variable, method etc. Similarly for interface.
  2. Like class interface have method, variable but these method does not contain any body part. This is main difference between interface and class.
  3. When we use any class, this can extends only one class. But a class can use multiple interface and all the interface method will be used as override. These override method can’t be contained any ambiguous problem.

Class which are used interface need to use implements such as

Class Example implements Shadow {}         package interfaccepackage;

public interface Shadow{

/*implicitly public abstract method*/

void manshadow();

/*implicitly public static final*/

int x = 100;

}

 

1. Here we show that the method is contain void but implicitly here the method actually used as public abstract if we do not write down in manshadow() method.

2. Variable x is not only int but also public static final either we write it or not.

When we declare any interface and any class implements this interface then compiler show that the interface has implementations and method has override. Below we use a class named as Example, where we implements Shadow interface.

When any class implements any interface must be override all the interface methods in the class otherwise will compilation error.

 

package interfaccepackage;
public Class Example implements Shadow {
int y;
public int noOflegs();
return 10;
}
public void printmn(){
System.out.println(“Class method println statements : “);
}
public static void main(String args[]){
Example ex = new Example ();
System.out.println(ex.y);
System.out.println(x);
System.out.println(ex.noOflegs());
ex.manshadow();
ex.printmn();
}
@Override
public void manshadow(){
System.out.println(“Interface method manshadow statements prints: “);
}
}

noOflegs(), printmn() are Example class method. Manshadow () method is override and this is a public method due to interface this method is default access method. When we use method of interface we need to override the method less restricted in the class. In Example class public is less restricted than default so we override manshadow () method as public.

Like Example class more class can be implements Shadow() interface and can override all the methods as user requirements

Output of the program

run:

0

100

10

Interface method manshadow statements prints:

Class method println statements :

 

0 is for the glabal variable y’s default initialization value.

100 interface variable x’s value

10 is noOflegs() method of Example class value.

 

First println is Example class

Second println is Override method

Interface is mainly used for polymorphism. Multiple classes can implements multiple interface. Besides multiple interface can extends multiple interfaces. But a java class cannot extends multiple interface because java support single inheritance.

These interface feature are explain with example below:

Interface TextA:

public interface TextA{
public abstract void print();
public static final int var=0;
}

Interface TextB:

public interface TextB{
void print();
}

There is two interface TextA and TextB both contains same method but there will be no ambiguity problem. But if we use it in C++ then there will show ambiguity problem. Now we check ambiguity when we implements these interfaces in a class named Neon {}

public Class Noon implements TextA, TextB{
void method(){
System.out.println(“Outside method in java : “);
}
public static void main(String args[]){
Neon n = new Neon();
n.print();
n.method();
}
@Override
public void print(){
System.out.println(“Outside method in java: “);
}
}

Here is the override method print(). If we click print() method ‘I’ key this will show implements from both interface TextA and TextB and work properly what we use in this method. That means interface has no interface ambiguity problem. Another uses of interface is class Neon can be abstract so print() method need not mandatory override. But if we wish we can use override. There is a problem of using abstract class because In abstract class no object can be created. Similarly

  • Interface no object can be created.
  •  An interface can extends another interface

public interface TextA extends TextB

{
public abstract void bprint();
public static final int var=0;
}

That is an interface can used another interface using extends.

Another feature of interface is marker interface. In this interface no method or variable included for example Cloneable(). This interface send special information to JVM. Again @Override in one kind of interface.

Cloneable() interface

package java.lang;
public interface Cloneable{
}

Cloneable() is default interface. We can implement user defined empty interface too.

package java.lang;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Target(value = (ElementType.METHOD))

@Retention(value =RetentionPolicy.SOURCE )

public @interface Override{

}

More other tutorials on JAVA in Draftsbook  Article series on :

  • JAVA : Dart is similar feature with JAVA  language. Java Classes and objects are the two main aspects of object-oriented programming. The Principles of OOP are:
    1. Encapsulation
    2. Inheritance
    3. Abstraction
    4. Polymorphism

 

0 Comments

You may find interest following article

Chapter 4 Relational Algebra

Relational Algebra The part of mathematics in which letters and other general symbols are used to represent numbers and quantities in formula and equations. Ex: (x + y) · z = (x · z) + (y · z). The main application of relational algebra is providing a theoretical foundation for relational databases, particularly query languages for such databases. Relational algebra...

Chapter 3 Components of the Database System Environment

Components of the Database System Environment There are five major components in the database system environment and their interrelationships are. Hardware Software Data Users Procedures Hardware:  The hardware is the actual computer system used for keeping and accessing the database. Conventional DBMS hardware consists of secondary storage devices, usually...

Chapter 2: Database Languages and their information

Database Languages A DBMS must provide appropriate languages and interfaces for each category of users to express database queries and updates. Database Languages are used to create and maintain database on computer. There are large numbers of database languages like Oracle, MySQL, MS Access, dBase, FoxPro etc. Database Languages: Refers to the languages used to...

Database basic overview

What is DBMS? A Database Management System (DBMS) is a collection of interrelated data and a set of programs to access those data. Database management systems (DBMS) are computer software applications that interact with the user, other applications, and the database itself to capture and analyze data. Purpose of Database Systems The collection of data, usually...

Laravel – Scopes (3 Easy Steps)

Scoping is one of the superpowers that eloquent grants to developers when querying a model. Scopes allow developers to add constraints to queries for a given model. In simple terms laravel scope is just a query, a query to make the code shorter and faster. We can create custom query with relation or anything with scopes. In any admin project we need to get data...