Você está na página 1de 15

SWT and JFace

SWT Standard Widget Toolkit


SWT is platform dependent. You must download and configure
swt.jar contains java classes Native library (platform specific)

You then import the swt and JFace


classes in your code

Platform-dependent
Applications look different across
operating systems Why might this be good? Why might this be bad?
In SWT Labs, note the differences between swing & swt. Also, notice the differences between mac & pc.

Inheritance and Interfaces


A review

Inheritance (extends)
java.lang.object is the root of all
classes Every class extends (inherits) a superclass (parent) If you dont designate extends, then you are simply inheriting java.lang.object extends can be chained. Inheriting one class will inherit all non-private members up the hierarchy to java.lang.object Interfaces can also be extended

Code Reuse
The point of allowing inheritance through
an extended class is to support code reuse How is reuse accomplished in the Car() class and the Porche() and Pinto() subclasses example discussed last quarter?

Access Specifiers Instance data and Methods


Access Levels Specifier Class Package Subclass World

private
no specifier

Y
Y

N
Y Y Y

N
N Y Y

N
N N Y

protected Y public Y

Review Question Set 1


1. What is the hierarchy of the classes? 2. Fill in the following:
1. 2. 3. 4. 5. 6. C1 is a ____ of C2 (subclass/superclass) C1 is a ____ of C3 (subclass/superclass) C2 is a ____ of C1 (subclass/superclass) C2 is a ____ of C3 (subclass/superclass) C3 is a ____ of C1 (subclass/superclass) C3 is a ____ of C2 (subclass/superclass)

3. Which instance data is accessible in each class? 4. What is true of z?

The super() method


This method is used in constructors to call
the parent classs constructor. Note that a super() exists with a method signature for each of the parent classs overloaded constructors You can also call a parents methods from the child utilizing super.methodName().
This is good for reuse also (see questions)

Review Question Set 2


1. Why will the constructor of B3 compile and run
correctly, even though B3 is accessing protected data of B2? 2. If the programmer of B3 wanted to allow it to be extended, what must they do to the class? 3. How is reuse achieved in B2s constructor? What is a direct byproduct of this reuse in terms of lines of code? 4. How could you make B2s toString() function better utilize toString?

Multiple inheritance and Interfaces


Multiple inheritance would be to have two
parents for a single child (i.e. extend two classes at the same time) Java does not directly allow this! Instead, java allows you to implement multiple interfaces. This satisfies some of the abilities of multiple inheritance

Two types of abstraction


Abstract classes
public abstract class C1 { } An abstract class can not be instantiated directly. It is meant to be extended by child classes (example: Car class)

Abstract methods
Interface classes define abstract methods because they only define the signature and allows a class that implements the interface to provide the details.

Question Set 3
1. Is it ok for X2 to extend X1 and redefine
x to be a different type? 2. Except for java.lang.Object, all classes (may/must) have (one/many/one or many) parent class(es) and (may/must) have (one/many/one or many) child classes

Question set 4
1. In the javax.swing library, what is the
relationship between a JApplet and an Applet? 2. Why is java.awt.Component an abstract class? What are some classes that inherit from it (directly and indirectly)? 3. Why are event listeners interface classes instead of regular classes?

Polymorphism
Using the same name to refer to methods
that perform different tasks
Method Overloading Method Overriding

Polymorphic Variables
public void useObject(Object o) In this example, you could pass any type of object into the method The real type is determined at run time by the Java Run-time Environment

Você também pode gostar