TCSS 143, Autumn 2004 Lecture Notes

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
1 TCSS 143, Autumn 2004 Lecture Notes Creating Java Classes.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Interfaces and Inner Classes
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Inheritance and Polymorphism
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Chapter Goals To be able to declare and use interface types
Chapter 11 Inheritance and Polymorphism
Software Development Java Classes and Methods
Polymorphism, Abstract Classes & Interfaces
Lecture 5: Some more Java!
Chapter 20 Generic Classes and Methods
Lecture 17: Polymorphism (Part II)
Chapter 3: Using Methods, Classes, and Objects
A tree set Our SearchTree class is essentially a set.
Interfaces Professor Evan Korth.
Chapter 11 – Interfaces and Polymorphism
Type Systems Terms to learn about types: Related concepts: Type
Interfaces and Inheritance
Continuing Chapter 11 Inheritance and Polymorphism
Object Oriented Programming
Chapter 9 Inheritance and Polymorphism
CSE 143 Lecture 22 The Object class; Polymorphism read
CMSC 202 Interfaces.
A tree set Our SearchTree class is essentially a set.
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
CSE 143 Lecture 22 The Object class; Polymorphism read
Polymorphism, Abstract Classes & Interfaces
Lecture 16 - Interfaces Professor Adams.
Building Java Programs
Defining Classes and Methods
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Workshop for Programming And Systems Management Teachers
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
CMSC 202 Interfaces.
Chapter 14 Abstract Classes and Interfaces
Defining Classes and Methods
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Special instance methods: toString
Chapter 11 Inheritance and Polymorphism Part 2
Creating and Modifying Text part 3
CSE 143 Lecture 23 Polymorphism; the Object class read
Chapter 13 Abstract Classes and Interfaces Part 01
Type Systems Terms to learn about types: Related concepts: Type
Object-Oriented Design Part 2
CMPE212 – Reminders Assignment 2 due next Friday.
Chapter 11 Inheritance and Encapsulation and Polymorphism
CMSC 202 Interfaces.
Building Java Programs
Presentation transcript:

TCSS 143, Autumn 2004 Lecture Notes Java Interfaces

Java interfaces interface: a set of declared (but not defined) methods and fields that a class can promise to implement declared: given a name and header defined: declared, plus given a body provides a way to treat many classes as though they were the same examples: java.util.Iterator, java.awt.event.ActionListener, java.lang.Comparable

Recall: compareTo public int compareTo(Object o) Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. In summary: negative integer: this object is less than o zero: this object is equal to o positive integer: this object is greater than o

Comparable interface actually, a class with the compareTo method should implement the java.lang.Comparable interface Comparable declares one method: public int compareTo(Object o) by declaring that your class implements Comparable, you allow many classes in Java to compare your objects intelligently

Implementing an interface write implements InterfaceName on class's header define all of the methods in the interface in your class a class may implement many interfaces; separate their names by commas public class ImaginaryNumber implements Comparable, Serializable, Observer { ... }

compareTo example public class Person implements Comparable { private String myName; private int myPhoneNumber; /* Compares Person objects by name, breaking ties by phone number. */ public int compareTo(Object o) { Person p = (Person)o; int nameCmp = myName.compareTo(p.getName()); if (nameCmp != 0) return nameCmp; else return myPhoneNumber - p.getPhoneNumber(); } }

Polymorphism interfaces are useful because they provide a way to achieve polymorphism in Java polymorphism: the ability to use identical syntax on different data types, causing possibly different underlying behavior to execute objects that agree to behave the same on the outside, but might be very different on the inside examples? encapsulation, polymorphism, and reusability are the foundations of object-oriented programming telephones vs. TV remotes; cars

Interface reference variables it is illegal to construct an object of an interface type Comparable c1 = new Comparable(); however, it is legal to declare a variable of an interface's type Comparable c1 = "hello"; Comparable c2 = new Fraction(2, 3); observation: the type of a reference variable need not necessarily match the type of the object that it refers to

Interface reference variables a variable of an interface type can be assigned to refer to any object that implements that interface using the interface variable, one may call any of the interface's methods (or methods that exist in class Object, such as equals and toString) on the object it is illegal to call methods of the object that aren't in the interface (or class Object), through the interface variable to regain the ability to call the other methods, you can cast the reference variable to the appropriate type

Interface reference variables Comparable c1 = "hello"; System.out.println(c1.compareTo(c1)); System.out.println(c1.toString()); System.out.println(((String)c1).substring(1, 4)); it is legal to call these on c1: compareTo equals, toString any String method on ((String)c1) it is illegal to call these on c1: any String method on c1 itself, without casting

Variables of type Object just as it is legal to declare variables of an interface type, you can also have variables of type Object that can refer to any object using a variable of type Object, one may only call methods from the Object class! to use any other methods, do a type-cast Object o = "hello"; Object o2 = new BankAccount("Marty", 1.0); Why would we ever want this kind of variable?

Interface reference variables practice problems: Which lines are legal? Object obj = "hello"; Comparable cmp = "hello"; System.out.println(cmp.compareTo("goodbye")); System.out.println(obj.indexOf('e')); System.out.println(cmp + ", world"); System.out.println(cmp.toUpperCase()); System.out.println(((String)cmp).trim()); System.out.println(obj.equals(null)); System.out.println(("" + cmp).toLowerCase()); System.out.println(obj.toString().indexOf('h')); legal illegal

Class cast exceptions type-casting a reference variable from an interface type into the wrong class type causes a ClassCastException to occur at runtime Why doesn't the compiler catch this error? Comparable c = new Fraction(2, 3); System.out.print(((String)c).toLowerCase()); when the program is run: Exception in thread "main" java.lang.ClassCastException at temp.main(temp.java:4)

compareTo search / sorting practice problem: Write a static method isSorted that accepts an array of Fractions as its argument, and returns true if the Fractions in the array are stored in ascending order by value, and false if not. Make the isSorted method above generalized so that it will work for an array of any type of comparable objects.

Declaring an interface header: public interface InterfaceName methods written with header, semicolon ; (no body) interfaces may only have public methods no constructors no methods that exist in class Object interfaces may contain public static final constants public interface Vehicle { public int getWeight(); public boolean setSpeed(double newSpeed); public void go(City place, String message); }

References Koffman, Chapter 1, pp. 14-19