Chapter 6 Interfaces. Class Status CU will be close for winter break from Dec. 22. till Jan.2 We have 3 classes left after tonight (Jan 8,15, and 22)

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 10 Abstract Classes.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.
Multiple Choice Solutions True/False a c b e d   T F.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
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.
Inheritance using Java
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
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.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 10 Abstract Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 9 Abstract Classes.
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,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
Interfaces and Inner Classes
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
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 Interface Design. 2 concept An interface is a way to describe what classes should do, without specifying how they should do it. It’s not a class but.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Classes Revisited Chapter 8.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
1 Chapter 5 Abstract Classes and Interfaces. 2 Objectives u To design and use abstract classes. u To process a calendar using the Calendar and GregorianCalendar.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 5:Interfaces and Abstract Classes
Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Chapter 15 Abstract Classes and Interfaces
Abstract Classes and Interfaces in Java Reference: COS240 Syllabus
Chapter Goals To be able to declare and use interface types
Interface.
IST311 / 602 Cleveland State University – Prof. Victor Matos
Chapter 13 Abstract Classes and Interfaces
Lecture 4: Interface Design
Interface.
Introduction interface in Java is a blueprint of a class. It has static constants and abstract methods only. An interface is a way to describe what classes.
Group Status Project Status.
Chapter 12 Abstract Classes and Interfaces
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Interfaces.
IST311 / 602 Cleveland State University – Prof. Victor Matos
Chapter 8 Class Inheritance and Interfaces
Chapter 13 Abstract Classes and Interfaces Part 01
Corresponds with Chapter 5
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Chapter 6 Interfaces

Class Status CU will be close for winter break from Dec. 22. till Jan.2 We have 3 classes left after tonight (Jan 8,15, and 22) Books still aren’t in yet.  Start looking at Chapter 11 next

Generic Array List Dynamically add/remove array elements No over-allocation No manual reallocation

Assignment 4 Using ArrayList New Solution – See Eclipse

Methods With Variable Number of Parameters

Varargs Method’s Cont public static double max(double... values) { double largest = Double.MIN_VALUE; for (double v : values) if (v > largest) largest = v; return largest; } Si mply call the function like this: double m = max(3.1, 40.4, -5); The compiler passes a new double[] { 3.1, 40.4, -5 } to the max function.

Welcome to Interfaces What is a class? – Defines methods with inputs and outputs – Describes HOW to do something Compare and contrast: interfaces – Also defines the inputs and outputs of methods – is not a class but a set of requirements for classes that want to conform to the interface Describes WHAT a class should do In other words - Interfaces are a way to specify common behavior for objects Classes can implement one or more interfaces

Interface Syntax modifier interface InterfaceName { /* constant declarations */ /* abstract method signatures */ } Public interface Edible { /** describe how to eat */ public abstract String howToEat(); }

9 Creating Custom Interfaces public interface Edible { /** Describe how to eat */ public String howToEat(); } class Animal { } class Chicken extends Animal implements Edible { public String howToEat() { return "Fry it"; } } class Tiger extends Animal { } class abstract Fruit implements Edible { } class Apple extends Fruit { public String howToEat() { return "Make apple cider"; } } class Orange extends Fruit { public String howToEat() { return "Make orange juice"; } }

10 class Chicken extends Animal implements Edible, Comparable { int weight; public Chicken(int weight) { this.weight = weight; } public String howToEat() { return "Fry it"; } public int compareTo(Object o) { return weight – ((Chicken)o).weight; } } Implements Multiple Interfaces

11 Creating Custom Interfaces, cont. public interface Edible { /** Describe how to eat */ public String howToEat(); } public class TestEdible { public static void main(String[] args) { Object[] objects = {new Tiger(), new Chicken(), new Apple()}; for (int i = 0; i < objects.length; i++) showObject(objects[i]); } public static void showObject(Object object) { if (object instanceof Edible) System.out.println(((Edible)object).howToEat()); } }

Example – Arrays.sort() Now suppose we want to use the sort method of the Arrays class to sort an array of Employee objects. Then the Employee class must implement the Comparable interface. To make a class implement an interface, you carry out two steps: 1. You declare that your class intends to implement the given interface. 2. You supply definitions for all methods in the interface.

Example – Arrays sort() The sort() method of the Arrays class can sort objects of any arbitrary class To be sortable, a class must implement the Comparable interface

Employee Class Sorting see sample eclipse code

Properties of Interfaces An interface variable must refer to an object of a class that implements the interface: x = new Employee(...); // OK provided Employee implements Comparable Cannot instantiate interface Objects i.e. can’t call new

Object Cloning Copying – Makes a reference to the same object – Contents of original and copy are always the same Cloning – Makes a true copy – Contents of original and clone can diverge The clone method is a protected method of Object, which means that your code cannot simply call it. Only a given class can clone instances of that class

Difference between copying and cloning Employee original = new Employee("John Public", 50000); Employee copy = original; copy.raiseSalary(10); // oops--also changed original Employee copy = original.clone(); copy.raiseSalary(10); // OK--original unchanged

Assignment 5 Due Jan 8th