Java Interface and Abstract Class


Interface Class
  • An interface is a definition of a named Application Programming Interface (API) with no associated implementation
  • An interface is not a class it is a set of requirements for a class
  • You can never instantiate an interface with the new operator
  •  An interface does not have instance variables
  • Methods in an interface are never implemented
  • All methods of an interface are automatically public (it is not necessary to supply the keyword public when declaring a method in an interface)
 
The Reasons Behind An Interface
 
An interface is a way to clearly separate the implementation details from the behavior of an implementation -- it separates the how something is done from the what is done.  For example, when you look at a clock, you don't necessarily know or want to know how it keeps time, you just know that it keeps time.  Another example is an interface that makes something move.  This interface reveals that it can move, but it does not reveal how it moves.
 
  Public interface Movable {

                        Void move(double x, double y);

            }

 
  

In the same way that you can build class hierarchies, you can extend interfaces.  This allows for multiple chains of interfaces that go from a greater degree of generality to a greater degree of specialization.  For example, the interface Powered can do everything that Movable can do AND it can also calculate miles per gallon.  (Powered extends Movable as shown in the example code below.)

            Public interface Powered extends Movable {

                        Double milesPerGallon( );

            }

 Note:  You cannot put instance variables or static methods in an interface, however, you can supply constants for them.

            Public interface Movable {

                        Double milesPerGallon ( );

                        Double SPEED_LIMIT = 95; // a public static final constant

            }

 
 
 
Abstract Class
 
An abstract class can be thought of as a template that is used by other classes.  It can have one or more abstract methods, however, it is not required to have any abstract methods.  An abstract method has no implementation (no method body) as this is handled by the subclass implementation of the abstract class.  In general an abstract class should not contain method implementations except for methods that provide access to private data fields or methods that express functionality common to all of the subclasses.
 
Unlike an interface any method within an abstract class that is undefined must also be declared as abstract.
 
For example the abstract person class below contains characteristics that apply to all types of people, in this case that happens to be employees.
 

abstract

class Person {

 

    // concrete data within the abstract class 

    private String name;

    public Person(String n)  {    

        name = n;

    }

    // abstract method

    public abstract String getDescription();

    // concrete method to return the concrete data name

    public String getName( ) {    

        return name;

    }

}

  
The subclass Employee extends the Person class inheriting its abstract method.  The abstract method getDescription in the Person class is overwritten in the subclass Employee providing the method with functionality.
 

public

class Employee extends Person { 

    private String department;

    public Employee(String n, String d) {

        // pass name to superclass constructor

        super(n);

        department = d;

    }

    public String getDescription( ) {

        return "an employee in " + department;

       }

}

 
 
 
 
 References
 
        Herbert Schildt. _Java2 A Beginner's Guide_. California: Osborne/McGraw-Hill, 2001.
 
        Cay S. Horstmann, Gary Cornell. _Core Java_. Volume I - Fundamentals.California:                                
            Sun Microsystems,    Inc., 2001.
 
        Frank M. Carrano, Janet J. Prichard. _Data Abstraction & Problem Solving with java_. 2nd Edition.
            New York: Pearson Addison Wesley, 2006.
 
        D.S. Malik. _Java Programming From Problem Analysis to Program Design_. 2nd Edition. Boston,
            Massachusetts: Thomson Course Technology, 2006.
 
        Laura Lemay, Charles L. Perkins. _teach yourself Java in 21 days_. 1st Edition. Indianapolis, IN:
            Sams.net Publishing, 1996.
 

        "Java Abstract class and Interface." Srini Appikatla. java beginner.com "java learning begins here". 2007-            2008. google. 5 November 2008. <http://www.javabeginner.com/abstract-class-interface.htm>.

 

Comments