Lecture 13 - Interfaces, packages and jar files Flashcards
What are interfaces?
An interface behaves like a programming ‘contract’.
Client: provides the interface
Service provider: provides implementation(s)
How do you declare interfaces?
An interface is a reference type, similar to a class, which variables can be declared as. Contains:
Constructors: NO constructor(s)
Attributes:
Only static constants
int A = 1;//implicitly public, static, and final
Methods:
Abstract methods
double area();//implicitly public and abstract
Since Java 8, two types of non-abstract (i.e. concrete)
methods are allowed.
static void help(){…}//with body
default void newFunction(){…}//with body
Rules for interfaces ?
Interfaces cannot be instantiated.
Interfaces cannot contain instance fields.
If any non-abstract class implements an interface, it must implement all the abstract methods in the interface. Otherwise, the class must be declared as abstract.
Java does not support multiple inheritance through classes, but allows implementing multiple interfaces. why?
Ambiguity may occur in the case of class for the concrete method to use, but not for an interface.
For example, there will be no conflicts if both the Printable
interface and Showable interface has the same method: print().
The separation of interface and implementation enables better:
oftware design and ease in expansion.
Specialised implementors can be employed and instantiated dynamically depending on context – no need to modify existing codebase.
What is a package?
A package is a group of related classes, interfaces and resources.
Examples of built in Java packages ?
Built-in packages
java.lang (imported automatically)
Object, String, Math, primitive wrapper classes, …
java.util
Scanner, List, ArrayList, Random, HashSet, HashMap, …
java.io
File, FileReader, FileWriter, InputStream, OutputStream,
IOexception
Adavantages of packages:
Reusability
Better organisation
Name conflicts: we can define two classes with the same name in different packages
Java packages conventions:
package shape;//package declaration must be at the beginning of the program
public class Rectangle implements Shape {…}
A class can have only one package declaration.
Package name is all lowercase, by convention.
What is a package name and what do companies use ?
A package name corresponds to the directory structure for storing the class files.
import shape.Triangle; //compiler will look for
shape/Triangle.class
In general, a company uses its reversed Internet domain name for its package names.
import com.apple.computers.; //compiler will look for
com/apple/computers/.class
How can use use a class in a package?
No import, use fully qualified name:
shape.Shape rt = new shape.Rectangle();
Import the class needed. import shape.Shape;
Import all classes and interfaces in one package:
import shape.*;
Note : It is generally not good practise to import package name.* unless you really need many package members — as will lead to bloat in compiled class.
Are packages inn java hierarchical ?
Packages in Java are not hierarchical
It is common to arrange your source and class directories separately. Example: The package source code: ./src/shape/*.java The package class files: ./bin/shape/*.class What is the command for the above directory structure?
> > javac -d bin/ src/shape/*.java
What is a jar file ?
The Java archive file (jar) is a compressed archive Java uses to store all the resources needed to run an application
It is a standard format which allows you to easily distribute applications and packages/libraries
Contains .class files (and sometimes, but not always corresponding source files), along with any other files needed (e.g. images, data)
A jar file can also be configured to be ‘run’ as an application directly, e.g.(On the command line) java -jar
widget-application.jar
Or by ‘double-clicking’ with the mouse