EE 422C Interfaces Day 5. 2 Announcements SVN has Project 2. –Partly due next week. –SVN update to get it in your repository. See Piazza for Grocery List.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 9: Inheritance and Interfaces.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 slides created by Marty Stepp
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Object-oriented design Part 4: More UML. Interfaces An interface is a language construct specific to Java Java does not support multiple inheritance Interfaces.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Building Java Programs Interfaces, Comparable reading: , 16.4, 10.2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-4: Interfaces reading: self-check: exercises: #11.
Perimeter and Area o Perimeter is the distance around the outside of a flat object. o Area is the amount of surface space that a flat object has.
UML Basics & Access Modifier
Unit 7 Textbook Chapter 9.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Building Java Programs Chapter 9 Inheritance and Interfaces Copyright (c) Pearson All rights reserved.
1 CSE 331 Introduction; Review of Java and OOP slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
AD Lecture #2 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism Abstract.
CS305j Introduction to Computing Inheritance and Polymorphism 1 Topic 26 Introduction to Inheritance and Polymorphism "One purpose of CRC cards [a design.
CSE 143 Lecture 9 Interfaces; Stacks and Queues slides created by Marty Stepp
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
FORMULAS FORMULAS Moody Mathematics. Let’s review how to “set up” a formula with the numbers in their correct places: Moody Mathematics.
1.8: Perimeter, Circumference, and Area
1 Building Java Programs Chapter 9: Inheritance and Interfaces These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 9: Inheritance and Interfaces.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Chapter 10 Inheritance and Polymorphism
Salman Marvasti Sharif University of Technology Winter 2015.
Building Java Programs Chapter 9 Inheritance and Interfaces Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh) reading: 9.5, 11.1, slides created by Marty Stepp
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Area and Perimeter.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
Area & Perimeter An Introduction. AREA The amount of space inside a 2-dimensional object. Measured in square units cm 2, m 2, mm 2 Example: 1 cm 2 cm.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
Perimeter and Area Formulas.  Perimeter is the distance around an object. It is easily the simplest formula. Simply add up all the sides of the shape,
Abstract Classes. Recall We have the ability to create hierarchies of classes. Good design practice suggests that we move common methods as high as possible.
Building Java Programs Interfaces reading: , 16.4.
1 More About Derived Classes and Inheritance Chapter 9.
Building Java Programs Chapter 9 Inheritance and Interfaces Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 9 Interfaces Copyright (c) Pearson All rights reserved.
Lecture 21: Interfaces and Abstract classes
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2
Web Design & Development Lecture 9
Inheritance ITI1121 Nour El Kadri.
Building Java Programs
Interfaces.
Building Java Programs Chapter 9
Review: Two Programming Paradigms
Wednesday Notecards What’s your credit card number?
Interfaces EE 422C.
Interfaces CS163 Fall 2018.
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh)
two-dimensional shape.
Shapes Consider the task of writing classes to represent 2D shapes such as Circle, Rectangle, and Triangle. Certain operations are common to all shapes:
Based on slides by Alyssa Harding & Marty Stepp
Chapter 14 Abstract Classes and Interfaces
Binary search (13.1) binary search: Locates a target value in a sorted array/list by successively eliminating half of the array from consideration. How.
CSE 142 Lecture Notes Inheritance, Interfaces, and Polymorphism
CSE 373 Implementing a Stack Reading: Weiss Ch. 3; 3.6; 1.5
Lecture 20: Interfaces and Abstract classes
slides created by Marty Stepp
By- Sabrina,Julianna, and Killian
CSE 143 Lecture 6 interfaces; eclipse; testing
Introduction to Methods and Interfaces
Presentation transcript:

EE 422C Interfaces Day 5

2 Announcements SVN has Project 2. –Partly due next week. –SVN update to get it in your repository. See Piazza for Grocery List project – extra work (no credit).

3 Agenda Interfaces Linked List start

4 iClicker Familiar with linked lists, and their structure (in any programming language, not just Java)? A: Not familiar at all B: Slightly familiar C: Have programmed the linked list structure.

5 Relatedness of types Write a set of Circle, Rectangle, and Triangle classes. Certain operations that are common to all shapes. perimeter- distance around the outside of the shape area- amount of 2D space occupied by the shape Every shape has them but computes them differently.

6 Shape area, perimeter Rectangle (as defined by width w and height h): area= w h perimeter= 2w + 2h Circle (as defined by radius r): area=  r 2 perimeter= 2  r Triangle (as defined by side lengths a, b, and c) area= √(s (s - a) (s - b) (s - c)) where s = ½ (a + b + c) perimeter= a + b + c

7 Common behavior Write shape classes with methods perimeter and area. We'd like to be able to write client code that treats different kinds of shape objects in the same way, such as: –Write a method that prints any shape's area and perimeter. –Create an array of shapes that could hold a mixture of the various shape objects. –Write a method that could return a rectangle, a circle, a triangle, or any other shape we've written. –Make a DrawingPanel display many shapes on screen.

8 Interfaces interface: A list of methods that a class can implement. –Inheritance gives you an is-a relationship and code-sharing. A Lawyer object can be treated as an Employee, and Lawyer inherits Employee 's code. –Interfaces give you an is-a relationship without code sharing. A Rectangle object can be treated as a Shape. –Analogous to the idea of roles or certifications: "I'm certified as a CPA accountant. That means I know how to compute taxes, perform audits, and do consulting." "I'm certified as a Shape. That means I know how to compute my area and perimeter."

9 Declaring an interface public interface name { public type name ( type name,..., type name );... } Example: public interface Vehicle { public double speed(); public void setDirection(int direction); } abstract method: A header without an implementation. –The actual body is not specified, to allow/force different classes to implement the behavior in its own way.

10 Shape interface public interface Shape { public double area(); public double perimeter(); } –This interface describes the features common to all shapes. (Every shape has an area and perimeter.)

11 Implementing an interface public class name implements interface {... } –Example: public class Bicycle implements Vehicle {... } A class can declare that it implements an interface. –This means the class must contain each of the abstract methods in that interface. (Otherwise, it will not compile.) (What must be true about the Bicycle class for it to compile?)

12 Interface requirements If a class claims to be a Shape but doesn't implement the area and perimeter methods, it will not compile. –Example: public class Banana implements Shape {... } –The compiler error message: Banana.java:1: Banana is not abstract and does not override abstract method area() in Shape public class Banana implements Shape { ^

13 Complete Circle class // Represents circles. public class Circle implements Shape { private double radius; // Constructs a new circle with the given radius. public Circle(double radius) { this.radius = radius; } // Returns the area of this circle. public double area() { return Math.PI * radius * radius; } // Returns the perimeter of this circle. public double perimeter() { return 2.0 * Math.PI * radius; }

14 Complete Rectangle class // Represents rectangles. public class Rectangle implements Shape { private double width; private double height; // Constructs a new rectangle with the given dimensions. public Rectangle(double width, double height) { this.width = width; this.height = height; } // Returns the area of this rectangle. public double area() { return width * height; } // Returns the perimeter of this rectangle. public double perimeter() { return 2.0 * (width + height); }

15 Complete Triangle class // Represents triangles. public class Triangle implements Shape { private double a; private double b; private double c; // Constructs a new Triangle given side lengths. public Triangle(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } // Returns this triangle's area using Heron's formula. public double area() { double s = (a + b + c) / 2.0; return Math.sqrt(s * (s - a) * (s - b) * (s - c)); } // Returns the perimeter of this triangle. public double perimeter() { return a + b + c; }

16 Interfaces + polymorphism Interfaces don't benefit the class so much as the client. –Interface's is-a relationship lets the client use polymorphism. public static void printInfo(Shape s) { System.out.println("The shape: " + s); System.out.println("area : " + s.area()); System.out.println("perim: " + s.perimeter()); } –Any object that implements the interface may be passed. Circle circ = new Circle(12.0); Rectangle rect = new Rectangle(4, 7); Triangle tri = new Triangle(5, 12, 13); printInfo(circ); printInfo(tri); printInfo(rect); Shape[] shapes = {tri, circ, rect};

17 Interface diagram Arrow goes up from class to interface(s) it implements. –There is a supertype-subtype relationship here; e.g., all Circles are Shapes, but not all Shapes are Circles. –This kind of picture is also called a UML class diagram.