0% found this document useful (0 votes)
15 views

Interface

Java Concept

Uploaded by

shivanka2516
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Interface

Java Concept

Uploaded by

shivanka2516
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

We know earlier, Java does not support multiple inheritance, basically.

But
to support multiple inheritance, partially, Java introduced "interfaces". An
interface is not altogether a new one for us; just it is a special flavor
of abstract class where all methods are abstract. That is, an interface
contains only abstract methods.
1interface Suzuki
2{
3 public abstract void body();
4}
5interface Ford
6{
7 public abstract void engine();
8}
9public class MotorCar implements Suzuki, Ford
10{
11 public void body()
12 {
13 System.out.println("Fit Suzuki body");
14 }
15 public void engine()
16 {
17 System.out.println("Fit Ford engine");
18 }
19 public static void main(String args[])
20 {
21 MotorCar mc1 = new MotorCar();
22 mc1.body();
23 mc1.engine();
24 }
25}

Output screen of MotorCar.java

In the above code there are two interfaces – Suzuki and Ford. Both are
implemented by MotorCar because it would like to have the features of
both interfaces. For this reason (to have the functionality of many classes)
only, Java supports multiple inheritance through interfaces. Just to inform
there are multiple interfaces and not classes, tell the compiler by replacing
"extends" with "implements". MotorCar, after implementing both the
interfaces, overrides the abstract methods of the both
– body() and engine(); else program does not compile. Java supports
multiple inheritance partially through interfaces. Following figure
gives the view of the above interfaces.

Features of Interfaces
Interfaces are derived from abstract classes with a special additional rule
that interfaces should contain only abstract methods. Let us see some more
properties.

1. Interfaces support multiple inheritance which is not possible with


concrete and abstract classes.
2. "implements" keyword is used in place of "extends". "extends" comes
with concrete and abstract classes.
3. Interfaces must contain abstract methods only.
4. As in abstract classes, all the abstract methods of the interfaces
should be overridden by the subclass.
5. As with abstract classes, with interfaces also, objects cannot be
created but reference variables can be created.
6. Interface reference variable can be assigned with concrete subclass
objects. Once assigned, the interface reference variable works like an
object. This feature applies to abstract classes also.
7. As all methods must be abstract and public, these two keyword can be
omitted in method declaration. If omitted, JVM takes by default.
8. All interface variables must be public, static and final. If omitted, they
are taken by default.
9. All interface methods should be overridden with public only as
overridden method cannot have a weaker access specifier.
Later you get a table where actual differences between abstract classes and
interfaces are given.
Defining an Interface
We know earlier, interface should have only abstract methods (without any
body or implementation code). The abstract methods give a template
structure of methods from which subclasses can be developed easily.
Subclass job is just to override (or give the code) as per its convenience and
need not think about the design part (how many methods it should contain,
what are they, how they are related etc). Interfaces play a vital role in Java
coding and designing modules.
The interface methods can be implemented by various subclasses as per the
code appropriate to them. For example, the following "interface Mammal"
can be implemented by thousands of mammals as per their nature.
Following program is one of the generic implementations.

1interface Mammal
2{
3 String chambers = "4-chambered";
4 void offSpring();
5 void feed();
6 void blood(String str);
7}
8public class Nature implements Mammal
9{
10 public void offSpring()
11 {
12 System.out.println("Only 3 mammals lay eggs and others give birth to young ones");
13 }
14 public void feed()
15 {
16 System.out.println("Mammals feed the offspring with milk");
17 }
18 public void blood(String str)
19 {
20 System.out.println("Mammals are " + str + " and contains " + cambers + " heart");
21 }
22 public static void main(String args[])
23 {
24 Nature n1 = new Nature();
25 n1.offSpring();
26 n1.feed();
27 n1.blood("warm-blooded");
28 }
29}
Output screen of Nature.java

"interface Mammal" includes three methods and one variable. In the


method declaration, the keywords public and abstract are omitted as a
style and if omitted, we know earlier, they are taken by default. Similarly,
the string variable "chambers" is, by default, public, static and final. That
is, interface variables cannot be reassigned (as they are implicitly final).
The interface methods offSpring(), feed() and blood(String) can be
implemented by different species of animals in different ways as per their
nature. The above code is just one example of such implementations.
Compare and Contrast Abstract classes with Interfaces?
Even though the interface is a special derivative of abstract class; it differs a lot with
abstract class. Following table gives their differences.

Abstract class I

1. Multiple inheritance is not supported M

2. To inherit "extends" keyword is used T

3. May include concrete methods also A

4. Methods may be of any specifier except private. That is, may be


M
public, protected or default

5. Access specifier should be written I

6. Access modifier abstract should be written I

7. Variables can be any access specifier including private V

8. Constructors can be included in code N

9. main() can be included in code m


Table: Differences between abstract classes and interfaces
After knowing the differences, let us see the similarities.
1. With abstract classes and interfaces, objects cannot be crea
- See more at: http://way2java.com/oops-concepts/interfaces-partial-
implementatin-of-multiple-inheritance/#sthash.omkuhEIX.dpuf

You might also like