1 / 11

Lecture 17

Lecture 17. Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it. Abstract classes and abstract methods. Abstract methods define a method signature but nothing else They must be implemented in subclasses

Télécharger la présentation

Lecture 17

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 17 • Abstract classes • Interfaces • The Comparable interface • Event listeners • All in chapter 10: please read it

  2. Abstract classes and abstract methods • Abstract methods define a method signature but nothing else • They must be implemented in subclasses • If a class has an abstract method, it must also be abstract

  3. Example from chapter 10 [public] abstract class GeometricObject{ private String name; private String dateCreated; private Color color; [public] abstract double getArea(); // don’t know how to compute it until we know what kind of object it is [public] abstract double getPerimeter(); // same story }

  4. Implementation is in subclass [public] class Circle extends GeometricObject{ private double radius; … public double getArea(){ return radius*radius*Math.PI; } }

  5. Points to note • You cannot construct an object of an abstract class • Instead, you construct an object of one of the subclasses that implements the abstract methods • Do not use the keyword implements • It isn’t needed because the subclass must implement the abstract methods of the superclass– unless it is also abstract • An abstract class can still have constructors, which can then be invoked using super in the subclasses (constructor chaining)

  6. Points to note, continued • An abstract class can be used as a datatype, for example: • GeometricObject[] geoObjs = new GeometricObject[10]; • Here it is not the geometric object that is being constructed with “new”, but the array of references to possible future GeometricObjects

  7. Interfaces • An interface is like an abstract class, but different. • It contains only constants and abstract methods • This is different from an abstract class which can have data fields and concrete (non-abstract) methods • You cannot construct an interface object. Instead you construct objects of classes that implement the interface • These classes are not subclasses of the interface, so the keyword implements is used • You cannot construct an interface object • However, you can declare variables to have a type interface, and such a variable can reference any instance of a class that implement the interface

  8. The Comparable interface • Here is the declaration in java.lang: public interface Comparable{ public int compareTo(Object o); } • The documentation explains that compareTo should return -1 if the implicit parameter object is “less than” the explicit parameter object, +1 if it is “greater than”, and 0 if it is “equals” • It also “strongly recommends” that it should return 0 only in the case that the equals method would returns true for the same pair of objects • Actually any int value can be returned; only the sign is relevant.

  9. Implementing Comparable • in GeometricObject • in Date (instead of “later”)

  10. A Max Function //returns the “bigger” of 2 objects publicstatic Comparable maxObj(Comparable o1, Comparable o2){ if(o1.compareTo(o2)>= 0){ return o1; } return o2; }

  11. The ActionListener Interface • In our GUI program, we can add a new inner class which implements ActionListener, providing the actionPerformed method class YesListener implements ActionListener{ public void actionPerformed(ActionEvent e){ // take some action, such as print msg } } • After we construct such a listener we can add it to our “yes” button (for example)

More Related