Introduction
In this article, we will discuss the interface
in Java. As multiple inheritances in Java only achieve through Interface.
So we discuss Interface in Java.
What is Interface?
- In computing, the interface acts like a
communicator between peripheral devices(monitor, keyboard) and computer
system.
- The infield of computer science, an
interface refers to an interactor between components.
- In computer any type of communication of
computer system with the user we say it is an interface.
Interface in Java
An interface is a blueprint of a class. It is a java core part and a way to achieve data abstraction in Java along with the abstract class. Since multiple inheritances are not allowed in Java, the interface is the only way to implement multiple inheritances. At in basic level interface in java is a keyword but that time it is an object-oriented term to define contracts and abstraction, This contract is followed by any implementation of Interface in Java.
Key Point of Interface in JAVA
- You can fully abstract a class using
keyword interface.
- While declaring an interface in a class
there is no need for a keyword abstract.
- All methods in an interface are implicitly
public.
- The interface is not a class defining the interface is similar to defining a class, but they are two different concepts. A class defines the attributes and behaviors of an object. An interface contains behaviors that a class implements.
- We have to define all the abstract methods of the interface
in the class.
Declaring Interfaces
Using the Interface keyword to declare an interface.
Example
Define a simple interface
- interface Ex1
- {
- public static final int min=10;
- public abstract void print();
- }
Define another interface
- interface Ex2
- {
- public static final int min=5;
- public abstract void show();
- }
Implementing Interfaces
A class uses the implements keyword to implement an interface.
If a class implements more than one interface, they are separated with a comma.
The methods that implement an interface must be declared public (to be accessed by other members). Also, the type signature of the implementing method must match
exactly the type signature specified in the interface definition.
Sample Program
- class InheritanceEx implements Ex1
- {
- public void print()
- {
- System.out.println("Hello");
- }
- public static void main(String args[])
- {
- InheritanceEx obj=new InheritanceEx();
- obj.print();
- }
- }
Output
Multiple Inheritance
Multiple inheritances are the ability of a single class to inherit
from multiple classes. Multiple
inheritance is a feature of some object-oriented computer programming languages
in which a class can inherit characteristics and features from more than one
superclass. It is distinct to single inheritance, where a class may only inherit
from one particular superclass.
Java does not support multiple inheritances.
For achieving multiple inheritance JAVA use interfaces.
In the below figure, we show Multiple Inheritance.
This figure contains multiple animal class, this animal class are inherited
according to there nature (like Rabbit in Herbivore, Lion and Hyena in
Carnivore), and after that, all these class inherited in a single class called
Animal Class.
Implementing Multiple Interfaces (i.e
Multiple Inheritance)
Sample program
- class MulInheritanceEx implements Ex1,Ex2
- {
- public void print()
- {
- System.out.println("Hello");
- }
- public void show()
- {
- System.out.println("Welcome");
- }
- public static void main(String args[])
- {
- MulInheritanceEx obj=new MulInheritanceEx();
- obj.print();
- obj.show();
- }
- }
Output
Note 1-For overriding methods there are certain rules to be followed
-
The argument the parameter should be exactly the same as that of the overridden method.
-
A method declared
static cannot be overridden but can be re-declared.
-
The return type
should be the same or a subtype of the return type declared in the original an overridden method in the superclass.
-
A method declared
final cannot be overridden.
-
Constructors
cannot be overridden.
-
If a method cannot
be inherited then it cannot be overridden.
-
A subclass within
the same package as the instance's superclass can override any superclass method that is not declared private or final.
Summary
In this article, we learned about Introduction to Interface In Java.