Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
AP Computer Science TOPICS TO DISCUSS.equals() == instanceof operator compareTo Interfaces Abstract Classes.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
Lecture 10: Inheritance Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Abstract Classes and Interfaces
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Polymorphism & Interfaces
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
1 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
1 Interfaces Reading for today: Sec and corresponding ProgramLive material. Also, compare with previous discussions of abstract classes (Sec. 4.7).
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces and Inner Classes
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Object Oriented programming Instructor: Dr. Essam H. Houssein.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
Object Oriented Programming in Java Habib Rostami Lecture 10.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
Interfaces & Abstract Classes (For CS III AP) March 2014.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 5:Interfaces and Abstract Classes
Inner Classes.
Chapter 15 Abstract Classes and Interfaces
Abstract Classes and Interfaces in Java Reference: COS240 Syllabus
Inheritance and Polymorphism
Methods Attributes Method Modifiers ‘static’
Interfaces Professor Evan Korth.
Chapter 13 Abstract Classes and Interfaces
Abstract Classes.
Week 6 Object-Oriented Programming (2): Polymorphism
Chapter 12 Abstract Classes and Interfaces
CSC 113: Computer programming II
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 13 Abstract Classes and Interfaces Part 01
Abstract Classes and Interfaces
Inner Classes 25-Oct-19.
Presentation transcript:

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 If a class has an abstract method, it must also be abstract

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 }

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

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)

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

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

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.

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

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

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)