Lecture 13 - Interfaces, packages and jar files Flashcards

1
Q

What are interfaces?

A

An interface behaves like a programming ‘contract’.
Client: provides the interface
Service provider: provides implementation(s)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you declare interfaces?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Rules for interfaces ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Java does not support multiple inheritance through classes, but allows implementing multiple interfaces. why?

A

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().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The separation of interface and implementation enables better:

A

oftware design and ease in expansion.
Specialised implementors can be employed and instantiated dynamically depending on context – no need to modify existing codebase.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a package?

A

A package is a group of related classes, interfaces and resources.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Examples of built in Java packages ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Adavantages of packages:

A

Reusability

Better organisation

Name conflicts: we can define two classes with the same name in different packages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Java packages conventions:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a package name and what do companies use ?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can use use a class in a package?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Are packages inn java hierarchical ?

A

Packages in Java are not hierarchical

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
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?
A

> > javac -d bin/ src/shape/*.java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a jar file ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly