Skip to main content

JAVA : W1-M4


Interface

Learning Objective

Welcome Back!. In this Module let us solve Problems related to Interface.
  • Defining an Interface
  • Implementing Interfaces
  • Applying Interfaces
  • Variables in Interfaces
  • Interfaces can be extended

Orientation Video




Introduction

Interface:
In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces. Extension is discussed later in this lesson.
Defining an interface:
Interfaces are defined with the following syntax (compare to Java's class definition).
[visibility] interface InterfaceName [extends other interfaces] {
       constant declarations
       member type declarations
       abstract method declarations
}
The body of the interface contains abstract methods, but since all methods in an interface are, by definition, abstract, the abstract keyword is not required. Since the interface specifies a set of exposed behaviours, all methods are implicitly public.
Thus, a simple interface may be
public interface Predator {
      boolean chasePrey(Prey p);
      void eatPrey(Prey p);
}
The member type declarations in an interface are implicitly static and public, but otherwise they can be any type of class or interface.
Implementing Interfaces:
The syntax for implementing an interface is:
... implements InterfaceName[, another interface, another, ...] ...
Classes may implement an interface. For example,
public class Cat implements Predator {

       public boolean chasePrey(Prey p) {
              // programming to chase prey p (specifically for a cat)
       }

       public void eatPrey (Prey p) {
              // programming to eat prey p (specifically for a cat)
       }
}
If a class implements an interface and does not implement all its methods, it must be marked as abstract. If a class is abstract, one of its subclasses is expected to implement its unimplemented methods. Classes can implement multiple interfaces:
public class Frog implements Predator, Prey { ... }
Interfaces are commonly used in the Java language for callbacks. Java does not allow the passing of methods (procedures) as arguments. Therefore, the practice is to define an interface and use it as the argument and use the method signature knowing that the signature will be later implemented.

Resources

http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
http://www.roseindia.net/java/

Programs for your reference
1.

interface area
{
final static float pi=3.142f;
float compute (float x, float y);
void show();
}
abstract class rectangle implements area
{
    public float compute (float x, float y)
    {
 return (x*y);
    }
}
abstract class Circle implements area
{
    public float compute (float x, float y)
    {
 return (pi*x*x);
    }
}

class InterfaceTest1 extends rectangle
{
  public static void main(String args[] )
   {
        rectangle rect = new InterfaceTest1();
        area cir = new InterfaceTest1();
        System.out.println("Area of rectangle = "+rect.compute(10,20) );
       
       System.out.println("Area of circle = " +cir.compute(10,20));        
   }
 public void show(){}
}
2.

interface area
{
final static float pi=3.142f;
float compute (float x, float y);
}
class rectangle implements area
{
    public float compute (float x, float y)
    {
 return (x*y);
    }
}
class Circle implements area
{
    public float compute (float x, float y)
    {
 return (pi*x*x);
    }
}
class InterfaceTest
{
  public static void main(String args[] )
   {
        rectangle rect = new rectangle();
        Circle cir = new Circle();
        area are;
        are = rect;
        System.out.println("Area of rectangle = " +are.compute(10,20));
        are = cir;
       System.out.println("Area of circle = " +are.compute(10,20));        
   }
}

Task1 Description :

The goal of this assignment is to implement a limited multiple inheritance of Java. You know that the code for a sub-class should be able to access information in any of its super-classes. You have already created two classes MSITStudent, MSITMentor as a part of previous assignment. A TA (Teaching assistant) class extends both the MSITStudent and MSITMentor. Create interfaces of MSITStudent and MSITMentor by adding some more functionalities getGPA()(this function allows the student to get a grade [A,B,C,D]from stdin) and getSalary()allows the Mentor to get his salary from stdin). For example, the MSITStudent class may have a getGPA() method which does not apply to MSITMentor objects, and the MSITMentor class may have a getSalary() method which does not apply to MSITStudent objects. However, getGPA() and getSalary() are both valid methods for an instance of the TA class. Create a TA class that extends MSITStudent and MSITMentor.
      public class TA implements MSITStudent, MSITMentor {
                   ...
     }

Task2 Description :

It is common to create a Java interface that represents shared functionality between classes in order to enforce certain behavior and/or write generic code. You will often need to examine the classes that should implement the interface to determine the best methods to place in the interface. Below you will see the listing for an Add class. Write equivalent classes for Subtract, Multiply, and Divide. Find the common methods in these classes and create the Operator interface.
Add listing:
      public class Add implements Operator {
         private String opString;
             public Add() {
                  opString = "+";
             }
         public int performOperation(int num1, int num2) {
             return num1 + num2;
         }
          public String getOperation() {
          return opString;
         }
     }
TRY TO COMPLETE STUDYING CONCEPTS & COMPLETION OF TASKS WITHIN 4 HOURS

Comments