Interfaces and an Array List

Slides:



Advertisements
Similar presentations
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
Advertisements

CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
© 2006 Pearson Addison-Wesley. All rights reserved 4-1 Chapter 4 Data Abstraction: The Walls.
Programming Progamz pls. Importance VERY IMPORTANT.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
The Java Collections Framework (JCF) Introduction and review 1.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
Data Structures and Abstractions with Java, 4e Frank Carrano
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
Methods.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Java Methods and Applications CSIS 3701: Advanced Object Oriented Programming.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
An Array-Based Implementation of the ADT List
Linked Data Structures
Data Abstraction: The Walls
Web Design & Development Lecture 9
Sixth Lecture ArrayList Abstract Class and Interface
Arrays 3/4 By Pius Nyaanga.
Interface.
Lists Chapter 4.
Stacks.
Java collections library
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Abstract Data Types (ADTs)
Chapter 4 Procedural Methods.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
TCSS 143, Autumn 2004 Lecture Notes
More inheritance, Abstract Classes and Interfaces
null, true, and false are also reserved.
Interface.
Chapter 13 Collections.
Chapter 8 Slides from GaddisText
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Adapted from Pearson Education, Inc.
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Code Animation Examples
Sorted Lists and Their Implementations
Recursive GCD Demo public class Euclid {
Chapter 7 Procedural Methods.
class PrintOnetoTen { public static void main(String args[]) {
slides created by Ethan Apter
Queues CSC212.
Based on slides by Alyssa Harding & Marty Stepp
Chapter 14 Abstract Classes and Interfaces
CSE 142 Lecture Notes Inheritance, Interfaces, and Polymorphism
Developing Java Applications with NetBeans
Creating and Modifying Text part 3
Developing Java Applications with NetBeans
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
Abstract Data Types Stacks CSCI 240
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
A List Implementation that Uses An Array
Presentation transcript:

Interfaces and an Array List Dan Fleck 2007

Java Interfaces An interface Specifies methods and constants, but supplies no implementation details Can be used to specify some desired common behavior that may be useful over many different types of objects The Java API has many predefined interfaces Example: java.util.Collection (from Carrano slides)

Java Interfaces A class that implements an interface must Include an implements clause Provide implementations of the methods of the interface To define an interface Use the keyword interface instead of class in the header Provide only method specifications and constants in the interface definition (from Carrano slides)

Example Interface public interface AreaStructure { /** Returns the area of this object. */ public double getArea(); /** Returns how may square feet this thing is. */ public double getSquareFeet(); }

Building class public class Building implements AreaStructure { private int numFloors; private double floorSquareFeet; private int numDoors; public Building(int floors, int doors, double sqFeetPerFloor) { numFloors = floors; floorSquareFeet = sqFeetPerFloor; numDoors = doors; } public int getNumDoors() { return numDoors; public double getArea() { return floorSquareFeet *numFloors; public double getSquareFeet() { return floorSquareFeet;

Main class public static void main(String args[]) { Building building1 = new Building(10, 2, 1800.0); AreaStructure area1 = new Building(5, 1, 500.0); System.out.println("Building 1 Area is :"+building1.getArea()); System.out.println("Area1 area is:"+area1.getArea()); System.out.println("Building 1 Doors are: "+building1.getNumDoors()); // This does not work because Java treats the Object as a // AreaStructure which does NOT have a getNumDoors() method!!! System.out.println("Area1 doors are:"+area1.getNumDoors());

Now lets create a List interface A List is an ordered group of Objects that you can request by index. Lists allow duplicate objects. public interface List { // Add to the List // Remove from the List // Remove everything from the list // Is the list empty? // Insert into the list // Get an item from the list }

List Example See example Program in InterfacesAndLists Netbeans project.