So far, we’ve learnt a lot in Java. We know how to define
classes, their member variables and methods. In all the instances we have dealt
with so far, we have created methods for each class, and the only time classes
seemed to have somewhat common methods, has been through inheritance.
Let us imagine that we have two classes that are totally
unrelated. I will create a class called Person and another class called Season.
public class Person {
private
String name;
private int
age;
public
static void main (String[] args) {
}
}
public class Season {
private
String seasonName;
public
static void main (String[] args) {
}
}
Now as you can see, the following two classes are independent
of one another. They have nothing to do with each other. However, what if we
wanted to have a function with the same name that the two classes can implement
differently? Here is where an interface comes in handy.
We can create a new Interface by going to File -> New -> Interface
Give the interface a name that starts with a capital letter.
In in this case, I will call the interface Info. Within the body of the
interface, we will create a method called showInfo() that will be shared between
the Season and Person class like this:
public interface Info {
public void
showInfo();
}
Notice that in the interface file, we don’t specify the
parameters or define the method body. What we do is just to declare the message
as shown above. An interface can have as many methods as you want. Just
remember that you will be forced to define all of them in the classes that
implement the interface.
Another thing to note is that a class can implement an
unlimited number of interfaces. You just need to separate the interface names
using commas. For example, if your class implements an interfaces called Info,
Fall, Uprising etc, here is how you would declare the interfaces:
public class Example implements Info, Fall, Uprising {
}
However, remember that while your class can implement many
interfaces, it can only extend one class. So if the above Example class extends
another class Sample and implements the interfaces Info, Fall and Uprising,
here is how you would define it:
public class Example extends Sample implements Info, Fall,
Uprising {
}
In Java, we usually indicate that a class uses a certain
interface by adding the keyword implements
after the class declaration. So now, go to each class, i.e. Person and Season
and add the following in the class declaration to show that the class uses the
Info interface.
Java will now force you to implement within the class, all
the methods listed in the interface or interfaces that it implements. So this
means that you will have to define your methods based on how you want them to
behave in the classes that implement the interface or interfaces. In the
following example, the classes implement the Info interface and because the
interface only has one method, i.e. showInfo(), I am forced to define the
method in the two classes.
As you can see, the methods do different things in the two classes.
public class Person implements Info{
private
String name;
private
int age;
public
Person (String name, int age) {
this.name
= name;
this.age
= age;
}
@Override
public
void showInfo() {
System.out.println
("My name is " + name + " and I’m " + age);
}
public
static void main(String[] args) {
Person
person1 = new Person("Brian", 26);
person1.showInfo();
}
Figure 1: Class Person’s implementation of
the Info interface
public class Season implements Info {
private
String seasonName;
public
Season (String seasonName) {
this.seasonName
= seasonName;
}
@Override
public
void showInfo() {
System.out.println("The
current season is: " + seasonName);
}
public
static void main(String[] args) {
Season
season1 = new Season("Spring");
season1.showInfo();
}
Figure 2: Class Season’s implementation of the Info interface
It is important to state that some software developers design
entire programs around interfaces. They start by thinking about the classes
they are going to create, and thus, what methods those classes will require.
The methods that will be common between the classes will be defined in the
interface.
In the next tutorial, we are going to look at exceptions in
Java and how to handle them. If you have any questions regarding interfaces,
please feel free to drop them in the comments section below and I will address
them.


No comments:
Post a Comment