<< Previous Chapter | Next Chapter >> |
Interface in Java:
Definitions:
An interface is a contract between programmer and programming language. It
defines a set of standards which programmer must follow while developing their
programs. An interface looks like a class, but it is not a class, because it is
not developed to represent an object.
Above is the most suitable definition of the interface, we can define
interface in several other ways like:
- Interface is the blue print of a class.
- Interface is created to achieve the
abstraction.
- Interface is a medium between programmer and
programming language
- Interfaces are used to define standards.
Let’s take a real example - a student has paid some fees in college to take
admission in B Tech. So, the contract between them is that, student will pay some
amount and in return he will get the knowledge and degree from the college, and
proof of this contract is the registration form, which student fills in the college.
Syntax for declaring an interface:
public interface My {
}
Rule - By default the interfaces
are abstract, so we cannot create instance / object of interface. All methods
declared inside interface are by default abstract and public because these
methods are going to be called by others.
There are 2 major uses of inheritance 1) reusability through classes and
2) dynamic binding through interfaces.
Now, to use the functionality of above interface, first we have to implement
it in our classes. Like this:
class Child implements My
{
//definitions
of interface methods goes here
}
Quick Question – why technology
has preferred to use implements word instead of extends, as we did in class?
Answer – because
we extends means increase, but we are not increasing anything here because
there is nothing inside the interface, and we always implement the contracts,
so technology has chosen the word implements instead of extends.
Rule – while giving the body of a
method implemented from an interface, the method should be declared as public
always. A child class has to implement all methods of the interface, an
abstract class partially implements the abstraction but interface fully supports
it.
Enough theory, let’s take some program examples then we will summarize
the characteristics of interface.
interface My {
void show();
}
interface My1
{
void display();
}
public class MyImpl implements My, My1
{
public void show() {
System.out.println("Show
method called.");
}
public void display()
{
System.out.println("Display
method called.");
}
public static void main(String
args[])
{
My
m = new MyImpl();
m.show();
My1
m1 = new MyImpl();
m1.display();
}
}
Output:
Quick Question – What will
happen if we declare the show() method in My1 as well?
Answer – this is allowed, because
we are just getting method prototype from interfaces and we are providing its
definition in our class only once. So, no possibility of ambiguity in this type
of multiple inheritance through interface. Now, by doing this both m and m1
objects can call show method.
This relates to a real example of joint account in a bank, where 2 or
more members can open a joint account and both are allowed to access the
account. So show method is a joint account for both interfaces.
Here is its implementation:
interface My {
void show();
}
interface My1
{
void display();
void show();
}
public class MyImpl implements My, My1
{
public void show() {
System.out.println("Show
method called.");
}
public void display()
{
System.out.println("Display
method called.");
}
public static void main(String
args[])
{
My
m = new MyImpl();
m.show();
My1
m1 = new MyImpl();
m1.display();
m1.show();
}
}
<< Previous Chapter | Next Chapter >> |
No comments:
Post a Comment