1 / 15

Java Lecture: Abstract Classes, Interfaces, Cloneable, ActionListener, Event Adapters, Inner Classes, MouseListener

Reviewing the differences between abstract classes and interfaces in Java, and exploring topics such as the Cloneable interface, shallow and deep copies, ActionListener interface, event adapters, inner classes, and the MouseListener interface.

jordanh
Download Presentation

Java Lecture: Abstract Classes, Interfaces, Cloneable, ActionListener, Event Adapters, Inner Classes, MouseListener

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 18 • Review the difference between abstract classes and interfaces • The Cloneable interface • Shallow and deep copies • The ActionListener interface, revisited • Event adapters • Inner classes

  2. Abstract classes • Abstract classes are ones for which the implementation details must be filled in later, in subclasses

  3. Interfaces • Unlike C++, Java does not allow “multiple inheritance”. A class can only have one superclass. • Interfaces are a way to provide a kind of multiple inheritance without the complexity of C++. • When a class implements Comparable, we know we can use the compareTo method • A class can implement several interfaces.

  4. The Cloneable interface • This interface is empty! It does not specify any methods or constants. • It is called a tag interface or marker interface. • A class must implement the Cloneable interface if it overrides the clone() method in the Object class. • When clone() is called at run time, an exception occurs if the implicit parameter object is of a class that does not implement the Cloneable interface.

  5. Overriding clone() in the Date class • First, Date must specify that it implements the Cloneable interface • Then, Date must provide the clone() method. • Simplest way is to construct a new Date and copy the contents of the fields explicitly. • Alternatively, can invoke super.clone() but • then need to catch the CloneNotSupported exception – skip for now • Anyway, how does the super method in the Object class know about the fields in Date()? How can it make a cloned copy? • Answer: the Object clone method is a native method. It is not written in Java, but is implemented in the JVM for the native platform (machine). It works by simply copying chunks of memory.

  6. Shallow and deep copies • Suppose Date has another field which is actually an object reference • If Date’s implementation of clone() just copies the object reference, the cloned copy will reference the same object, not a copy • This is called a shallow copy • If this object is immutable, such as a String, this is fine • But if it’s mutable, it means that if the object is later changed in the cloned copy, it will also change in the original copy. • In this situation, it’s better to make a deep copy by also cloning the object field. • Since the Object clone() does not do this, this must be done in the overriding method

  7. ActionListener interface • extends EventListener interface, which is a tag interface • specifies only one method: • public void actionPerformed(ActionEvent e)

  8. Event handling – 3 ingredients • The event class, such as ActionEvent, provided by Java library • The listener class, such as our class YesListener that implements the ActionListener interface in the Java library • The event source, such as a JButton object where the user clicks on the mouse: YesListener must be added to (registered with) the button by calling the method addActionListener

  9. Inner classes • If the listener wants to do anything interesting, it needs to access the instance variables of the class that calls it - but these are typically private (or local variables, as in our FrameDemo example from last time) • Therefore, we put the listener in an inner class. • Anonymous inner classes: we don’t need to give the name YesListener to the inner class, but add it directly to the button anonymously (see p.471) • advantage: shorter to write • disadvantage: potentially confusing

  10. Returning to FrameDemo • Let’s change it so that the listener writes in the JTextField associated with the button that was clicked, instead of calling System.out.println • Should be possible since each yes button has its own listener • But how, since the body of actionPerformed cannot distinguish which button invoked it? • What can actionPerformed access? • It cannot access local variables • It can access data fields of the class that it is inside • Thus, let’s introduce a class YesNoSet, with data fields yesButton, noButton and text, that contains YesListener as an inner class • Then we can construct YesNoSet objects inside the double loop and, for each one, add its buttons and text field object to the frame window

  11. The MouseListener interface • This specifies five abstract methods • mouseClicked • mouseEntered • mouseExited • mousePressed • mouseReleased • Each returns void and has an explicit parameter of type MouseEvent • MouseEvent and ActionEvent are both descended from AWTEvent, but MouseEvent is much further down the inheritance tree

  12. Event adapters • Suppose we want to write a listener that implements mouseEntered but none of the other methods • Any class that implements the MouseListener interface must implement all the methods • However, there is a MouseAdapter class in the library that already implements all the methods as do-nothing methods • We can then extend this class by overriding only the methods that we want to do something • For example, we can write a class MouseEnteredListener that extends MouseAdapter and overrides only mouseEntered

  13. Panels • JPanel panel = new JPanel() • Good for organizing: can add buttons, etc., to a panel and panels to a frame window • Also good for graphics…

  14. Graphics • Chapter 13 • Easiest way is to draw on panels class drawPanel extends JPanel{ protectedvoid paintComponent(Graphics g){ super.paintComponent(g); g.drawLine(0,0,50,50); count++; System.out.println("drawing, count is "+ count); } } • We don’t call paintComponent: the system does when it needs to redraw the graphics window

  15. HW 7 • Please write something that challenges you but is also fun • Details completely up to you • Don’t forget about making sure user interface is clear, and there are plenty of comments

More Related