lec15 - 1.00 Lecture 15 Interfaces Reading for next time:...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1.00 Lecture 15 Interfaces Reading for next time: Big Java: sections 14.1-14.6, review 9.3 Interfaces Interface is a specification for a set of methods a class must implement Interfaces specify but do not implement methods A class that implements the interface must implement all its methods You may then invoke methods on this class that rely on the interface. Examples: If your class implements a Printable interface that has a printData() method, you can put objects of your class into arrays or ArrayLists and call that method You will use interfaces frequently in Swing (GUI) and numerical methods 1 Interfaces, p.2 Interfaces are like an abstract class but: If they were implemented as an abstract class, a subclass could only inherit from one superclass Multiple interfaces can be inherited (e.g., Drawable and Rotatable) in your class Interfaces cannot be instantiated Drawable Drawable Drawable Drawable shape1= new shape1= new shape1= new shape1= new Drawable Drawable Drawable Drawable(); (); (); (); // Error // Error // Error // Error You can declare objects to be of type interface Drawable Drawable Drawable Drawable shape1; shape1; shape1; shape1; // OK // OK // OK // OK They can be names for objects of a class that implements the interface. Assume Rectangle implements Drawable: Drawable Drawable Drawable Drawable shape1= new Rectangle(); shape1= new Rectangle(); shape1= new Rectangle(); shape1= new Rectangle(); // OK // OK // OK // OK Interfaces may contain methods and constants public interface public interface public interface public interface Rotatable Rotatable Rotatable Rotatable { void rotate(double theta); void rotate(double theta); void rotate(double theta); void rotate(double theta); // Required method(s) // Required method(s) // Required method(s) // Required method(s) double MAX_ROTATE= 360; } double MAX_ROTATE= 360; } double MAX_ROTATE= 360; } double MAX_ROTATE= 360; } // Implicitly final // Implicitly final // Implicitly final // Implicitly final // Methods and fields default to be public // Methods and fields default to be public // Methods and fields default to be public // Methods and fields default to be public...
View Full Document

Page1 / 8

lec15 - 1.00 Lecture 15 Interfaces Reading for next time:...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online