used to enforce the naming of method prototypes for a class • When a class implements a particular interface, this ensures that designers of the class and users of the class are using the same method names
ice machine is the ice machine’s interface • Syntax for an interface: public interface InterfaceName{ methodPrototypes1(); methodPrototypes2(); methodPrototypesN(); }
as a “contract” for methods • The class that implements the interface must fulfill the interface “contract” by defining methods with the same method prototype (same method name, return type, and parameter types) as the interface
methods of FractionInterface.java, so it has the code for these 9 methods Each method prototype has the same name, return type, and parameters as the method prototypes listed in FractionInterface.java
declare variables, you can use an Interface name when declaring variables • Remember that a declaration is on the left side of the equals sign when initializing a variable
Interface name instead of the Class name for the declaration is that the Class or Interface name is used to tell the compiler what methods can be legally used by a particular variable
• Fraction.java method definition: public FractionInterface add (FractionInterface f2){ . . . //method’s code } • Both fraction2 and f2 point to the same object (call by reference)
reuse the methods and data fields of an already existing class • Don’t have to “reinvent the wheel” (start from the beginning) when creating a new class
and a subclass • A superclass (base class) is the class from which other classes will be built • A subclass (derived class) is a new class built from an existing class
the example code public class IllegalFractionException extends RuntimeException{…} • Our IllegalFractionException class inherits the methods and data fields of superclass RuntimeException
the superclass’s constructor • Use the keyword “super” to call superclass’s constructor super(message); • If not done, the compiler will automatically call this constructor super();
your very own exception class! • Choose superclass Exception or RuntimeException to determine if exception must be handled or not • Personally, I like to use RuntimeException as the superclass
begins exception propagation • Syntax for throwing an exception: throw new ExceptionName("msg"); • If an exception is thrown, code following the throws statement will NOT be executed
the exception’s type (exception's class name) must be listed in the method’s prototype • Syntax: public void myMethod() throws MyException{ /* code */ throw new MyException(); }
Fraction.java public Fraction(Integer n, Integer d) throws IllegalFractionException{ //method code if(denominator.equals(0)){ throw new IllegalFractionException( "Cannot have 0 as the denominator!"); }
caught within a method: Then the exception propagates to the method that called this method The process continues, until the exception is propagated to the main method
method, it is caught in the main method of class FractionCalculator • Run the program and enter 0 (zero) as the denominator to see the result • Also run DoNotCatch.java to see what happens if the exception is not caught
keyword “private” with the keyword “protected” for all data fields in the superclass • We only have to do this for subclasses that we have created ourselves • We cannot change the code for classes in the Java API
superclass, use keyword “extends” and superclass’s name public class ThreeNames extends Name{ ... } • To call the superclass’s constructor, use the keyword “super” super(aFirst, aLast);
superclass and subclass have the same method prototype • In other words, the first line of both methods in both classes is the same, but the code in the body of the methods is different
classes related by inheritance to respond differently to the same method call • Literally means “many forms” • Polymorphism only works with overridden methods
the address to any superclass or subclass object • For overridden methods, the method that is called depends on the object’s class, but not on the class of the variable declaration
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture 4.Smell the flowers!