side effects a side effect is anything that happens in a method other than computing and/or...
TRANSCRIPT
Side effects• A side effect is anything that happens in a method other than computing and/or
returning a value.• Example:
public class hello{
public int sum = 0;public char letter = ‘ ‘;
public int getProduct(int a, int b){
sum = a+b; // SIDE EFFECTreturn (a*b);
}
public void setLetter(char z){
letter = z; // SIDE EFFECT}
}
precondition/postcondition
• Often, you will see comments before a method in which the precondition and postcondition is specified.
• The precondition of a method is what is true before the method executes.
• The postcondition of a method is what must be true after the method executes.
Example
Interfaces• An abstract method is a method with a first line (also called a
signature or a header), but without a body
• An interface is a collection of abstract methods and (sometimes) constants
• An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, usually it is left off
• An interface is used to establish, as a “formal contract”, a set of methods that a class will implement.
• In simple terms: an interface is a blueprint/template that shows programmers how to write a certain type of class.
public interface Sample{ public final double PI = 3.14;
public void times2(int x); public double getPI();}
interface is a reserved word
Notice: none of the methods inan interface are given
a definition (body)
A semicolon immediatelyfollows each method header
An interface can contain constants
public class Z implements Sample{ public void times2(int x) { System.out.println(x*2); }
public double getPI() {
return PI; }
}
Each method listedin Sample must begiven a definition
Z “inherits” PI from Sample
• Open Sample, Z
• An interface cannot be instantiated (meaning you can’t create an object of it from a client program).
• All methods in an interface must be public. It is nonsensical to have a private method in an interface – where would this method be called from? Remember, all methods in an interface are empty.
• A class formally implements an interface by– stating so in the class header (using the word implements)
– providing implementations (writing code) for each abstract method in the interface
• If a class implements an interface, then it MUST define (provide code for) all methods that are in the interface
• A class that implements an interface can implement other methods as well
• In addition to (or instead of) abstract methods, an interface can contain constants
• When a class implements an interface, it gains access to all its constants
• A class can implement multiple interfaces. (Note that this is different from inheritance: a class can only extend (inherit from) ONE parent.)
• The interfaces are listed in the implements clause
• The class MUST implement all methods in all interfaces listed in the header
• Also, many different classes can implement the same interface.
public class Mult implements interface1, interface2{ // all methods of both interfaces
// MUST be defined here}
• The Java standard class library contains many helpful interfaces
• The Comparable interface contains an abstract method called compareTo, which is used to compare two objects
• The String class implements Comparable, giving us the ability to put strings in lexicographic order: compares the letters using their ASCII (aka Unicode) values (see p. 604)
• Demo: compare
• When a programmer writes a class that implements the Comparable interface, it’s up to him/her to determine what makes one object greater or less than another
• Not all objects are compared numerically or lexicographically
• Confusing terminology:– A class that “connects” to an interface is said to implement the interface, by using the keyword implements
– Sometimes, you will see exams and text books mention that a certain method has not been implemented – what they mean is that the method has not been given any code in its body. In other words, the method has a signature, but is otherwise empty.
TL;DR interfaces are confusing.
Assignments
Type these answers in a Word doc called p291-3Qs, save it in your Programs folder:• p. 291-3 multiple choice #4, 6, 7, 9, 10• P. 293 true/false #9 – 10