Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Pick an interface in the JDK and describe a good use of it. Java 8 stuff - I wil

ID: 667644 • Letter: P

Question

Pick an interface in the JDK and describe a good use of it.

Java 8 stuff - I will probably be adding to this as we go along since 8 has so many new features.

Interface Default Methods in Java 8 | Javalobby

Lesson: Interfaces and Inheritance (The Java™ Tutorials > Learning the Java Language)

Also, I think you will find some of my thoughts and examples of interfaces helpful:

Musing A - Java Stuff - Intefaces survey

Musing A - Java Stuff - an extended example using applets

Tutorial on interfaces, Part 1 - Abstract, new Interfaces, Event Handling - pdf

Tutorial on interfaces, Part 2 - Vector class, sorting and collections - pdf

Explanation / Answer

Inheritance:

Inheritance refers to a feature of Java programming that lets you create

classes that are derived from other classes. A class that's based on

another class inherits the other class. The class that is inherited is

the parent class, the base class, or the superclass.


java interface


Java GUI programming involves two packages: the original abstract

windows kit (AWT) and The Newer Swing toolkit. Swing components have the

prefix to distinguish them from the original AWT onesfor example JFrame

instead of Frame. To include Swing components and methods in your

project we must import the java.awt.*, java.awt.event.*, and

javax.swing.* packages. Displayable frames are top-level containers such

as JFrame, JWindows, JDialog, and JApplet, which interface with the

operating systems window manager. Non-displaying content panes are

intermediate containers such as JPanel, JOptionsPane, JScrollPane, and

JSplitPane. Containers are therefore widgets or GUI controls that are

used to hold and group other widgets such as text boxes, check boxes,

radio buttonsetc.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*; //notice javax
public class Frame1 extends JFrame
{
JPanel pane = new JPanel();
Frame1() // the frame constructor method
{
super(" Simple Frame"); setBounds(100,100,300,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane(); // inherit main frame
con.add(pane); // add the panel to frame
  
// pane.add(someWidget);
setVisible(true); // display this frame
}
public static void main(String args[]) {new Frame1();}
}

APPLET:

Swing is built on top of AWT and is entirely written in Java. It is part of the Java Foundation Classes (JFC), which is an API for providing GUIs for Java programs.

When we derive our Applets from JApplet, we have at our disposal almost all of the functionality that comes with Swing. In fact, it is usually trivial to convert an Applet into a standalone application using the following boilerplate code.

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class SwingHelloWorld extends JFrame {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
SwingHelloWorld self = new SwingHelloWorld();
self.init();
self.setSize(500, 150);
self.setVisible(true);
self.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);
}
});
}

public void init() {
JTextArea area = new JTextArea("Hello, world!");
this.add(area);
}
}

Abstract Methods and Classes

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract void moveTo(double deltaX, double deltaY);
If a class includes abstract methods, then the class itself must be declared abstract, as in:

public abstract class GraphicObject {
// declare fields
// declare nonabstract methods
abstract void draw();
}
When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.


Abstract Classes Compared to Interfaces

Abstract classes are similar to interfaces. Cannot instantiate them, and they may contain a mix of methods declared with or without an implementation, with abstract classes, can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define as default methods are public. EXtend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.

You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
You want to take advantage of multiple inheritance of type.
An example of an abstract class in the JDK is AbstractMap, which is part of the Collections Framework. Its subclasses (which include HashMap, TreeMap, and ConcurrentHashMap) share many methods (including get, put, isEmpty, containsKey, and containsValue) that AbstractMap defines.


Note that many software libraries use both abstract classes and interfaces; the HashMap class implements several interfaces and also extends the abstract class AbstractMap.

An Abstract Class Example

In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic objects. These objects all have certain states (for example: position, orientation, line color, fill color) and behaviors (for example: moveTo, rotate, resize, draw) in common. Some of these states and behaviors are the same for all graphic objects (for example: position, fill color, and moveTo). Others require different implementations (for example, resize or draw). All GraphicObjects must be able to draw or resize themselves; they just differ in how they do it. This is a perfect situation for an abstract superclass. You can take advantage of the similarities and declare all the graphic objects to inherit from the same abstract parent object

First, you declare an abstract class, GraphicObject, to provide member variables and methods that are wholly shared by all subclasses, such as the current position and the moveTo method. GraphicObject also declares abstract methods for methods, such as draw or resize, that need to be implemented by all subclasses but must be implemented in different ways. The GraphicObject class can look something like this:

abstract class GraphicObject {
int x, y;
...
void moveTo(int newX, int newY) {
...
}
abstract void draw();
abstract void resize();
}