Information from lesson 13
Introduction to Interfaces:
- **Interface: ** An abstract type that defines a set of methods without providing their implementation.
- Allows multiple classes to directly invoke a set of methods
- Can contain abstract and concrete methods
- All methods are public
- If a superclass implements an interface, so does its subclasses
- A class must define all abstract methods when implementing an interface
- No restrictions on how many interfaces a class can implement
- Allows for more flexiblity than inheritence
//Example interface
public interface interfaceName {
//methods here
}
//Interface implementation
public class className implements interfaceName {
//same methods from interface defined here
}
Sorting Objects:
compareTo() method:
Strings have their own compareTo() method built-in. This is referring to writing one for a class. Click HERE if you are looking for the String method.
- Used to compare two different objects of the same class
- Returns an integer value:
- A negative int if the calling object is less
- A positive int of the calling object is greater
- A zero if both objects are equal
java.lang.Comparable:
- Not all classes will have a compareTo() method
- Use an interface to force classes to have a compareTo() method as it is not a part of the Object class
- java.lang.Comparable is a built-in interface that contains a compareTo() method
Writing a compareTo() method:
Remember to typecast the inserted object to the proper class type before calling any methods or getting values from it. You may also need to check to make sure that the inserted object is of the proper class. Check the .equals() method to see how this is done. If you do not want to do this manually, check example two
- Example 1 (Basic compareTo() method):
public class className implements Comparable {
private int comparingValue; //Value being used to compare the two objects
public int compareTo(Object anotherClassNameObject) {
//Remember that compareTo must return one of three possible values listed above. Take note of this when you are writing your return value
return comparisonEquation;
}
}
- Example 2 (compareTo() method with class checks implemented):
public class className implements Comparable<className> {
private int comparingValue;
//Removes the need for typecasting
public int compareTo(className anotherClassNameObject) {
return comparisonEquation;
}
}
Arrays.sort():
- Part of the Arrays class
- Must be imported
- Sorts in ascending order
- It works by typecasting an object into a Comparable object, then it calls compareTo() on it
- By typecasting into a Comparable object, it allows the object to run the compareTo() method
Usage Example:
import java.util.Arrays;
public static void main(String args[]) {
//Assume objectArray is an array full of objects
Arrays.sort(objectArray);
}
Information from lesson 14
Default Methods:
- Every time a method is added to an interface class, it must also be added to every method that implements it
- Must manually write and recompile each class
- Instead of writing a normal method, you can write a default method
- Applies to all classes that inherit the interface class
- Default methods can be overrided and changed for specific classes
//Interface
public interface interfaceName {
default returnType methodName() {
//method info
}
}
//Implementing class
public class className() {
//Overriding default method
public returnType methodName() {
//new method info
}
}
Static Methods:
- Can be placed inside of interfaces
- Can’t be overriden
- Can’t be default
- Must be called using inteface name
//Calling static method
InterfaceName.methodName();
Constants in Interfaces:
- Any variable defined in an interface is automatically public, static, and final
- Can be done to share certain constant information between different classes
- Avoids redundant code
Constant Interfaces:
- An interface that only contains constants
Interface Hierarchies:
- Unlike other classes, Interfaces can have multiple parent classes