Home | | Object Oriented Programming | Interface - Java

Chapter: Object Oriented Programming(OOP) : Java Exception Handling

Interface - Java

An interface may be considered a “pure” abstract class.

INTERFACE

 

An interface may be considered a “pure” abstract class.

         {

                       public void  scale(double amt);

         }

 

public class It provides method names, parameter lists, and return types, none of which are implemented.

 

An interface may contain data fields (attributes), but they are implicitly static and final.

 

An interface provides to the client a description of what the classes that implement the interface must look like.

 

public interface Comparables {

 

public boolean lessThan(Object x);

public boolean greaterThan(Object x);

}

 

public class Rectangle extends Shape, implements Comparables {

 

//methods and attributes from previous slide

public boolean lessThan(Object x) throws

 

IncompatibleTypeException{

 

 

if ( x instanceof Rectangle)

return area( ) < x.area( );

 

else throw new

IncompatibleTypeException( ); }

 

//similarly for method greaterThan( )

}

 

public class Complex implements Comparables {

private double re, im, modulus, theta; //other methods

 

public boolean lessThan(Object x) throws

IncompatibleTypeException {

if (x instanceof Complex)

return modulus < x.modulus;

 

else throw new IncompatibleTypeX ( ); }

}

 

In the previous example, the classes Rectangle and Complex implemented the interface Comparables by first determining that the object being compared was an object of the same class, then performing a test on an appropriate attribute.

 

A class that implements an interface must implement ALL of the methods in the interface. Note! Interface Comparables was developed here strictly for explanatory purposes. It

 

could be created and implemented just as described, but it must be noted that there exists an interface Comparable found in java.util that has a single method – compareTo( ) – that returns a negative integer if less than, a positive integer if greater than, or 0 if equal to.

 

A class may implement multiple interfaces

 

public interface Scalable Rectangle extends Shape, implements Comparables, Scalable {

 

private double length, width; //methods previously developed public void scale(double amt) {

 

length *= amt; width *= amt;

}

}

 

An interface can inherit from another interface public interface MyInterface2 extends MyInterface1 { public void myNewMethod(double param);

 

}

 

public class Base implements InterfaceA, InterfaceB {

 

//base class attributes //base class methods

 

//base class implements methods // of the two interfaces

 

}

 

public class Derived extends Base {

//implement interfaces A and B too! //my additional attributes and methods

 

}

 

One may use the fact that the data fields (attributes) in an interface are static and final to create “enumerated types”

 

public interface Months {

int

JANUARY = 1, FEBRUARY = 2, MARCH = 3, APRIL = 4,MAY = 5, JUNE = 6, JULY = 7, AUGUST = 8, SEPTEMBER = 9, OCTOBER = 10, NOVEMBER = 11, DECEMBER = 12;

 

}

In an application you may have code that uses this interface

if (!(Months.MAY || Months.JUNE || Months.JULY || Months.AUGUST))

System.out.println(“Eat Oysters! “);

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming(OOP) : Java Exception Handling : Interface - Java |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.