Interface Java 7 COMP 112 2017T1.

Slides:



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

CS 211 Inheritance AAA.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Interface COMP T1.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
© 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.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Generics, Proxy, and The Compile Time Type Checking Debate You are either with us or against us. Please snarf the code for today’s class.
Java Implementation Software Construction Lecture 6.
M1G Introduction to Programming 2 4. Enhancing a class:Room.
JVM And CLR Dan Agar April 16, Outline Java and.NET Design Philosophies Overview of Virtual Machines Technical Look at JVM and CLR Comparison of.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Object Oriented Analysis & Design & UML (Unified Modeling Language)1 Part V: Design The Design Workflow Design Classes Refining Analysis Relationships.
Object-Oriented Modeling Chapter 10 CSCI CSCI 1302 – Object-Oriented Modeling2 Outline The Software Development Process Discovering Relationships.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
ICOM 4035 – Data Structures Lecture 3 – Bag ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
Object Oriented Software Development
Part VII: Design Continuous
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Lesson 1 1 LESSON 1 l Background information l Introduction to Java Introduction and a Taste of Java.
Object-Oriented Design Concepts University of Sunderland.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
Java Distributed Object Model A remote object is one whose methods can be invoked from another JVM on a different host. It implements one or more remote.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Compound Types for Java Object Oriented Systems and Languages © 2001 Marcus Handte.
Computer Science Victoria University of Wellington Copyright: david streader, Victoria University of Wellington Simple Design COMP
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Lecture 5:Interfaces and Abstract Classes
TK1924 Program Design & Problem Solving Session 2011/2012
CSC 222: Object-Oriented Programming
CSC 222: Object-Oriented Programming
Modern Programming Tools And Techniques-I
Data Abstraction: The Walls
Objects First with Java A Practical Introduction using BlueJ
University of Central Florida COP 3330 Object Oriented Programming
Lecture 1: Introduction to JAVA
Inheritance and Polymorphism
Software Engineering Fall 2005
CS101 Introduction to Computing Lecture 19 Programming Languages
CSC 222: Object-Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Types of Programming Languages
EE 422C Java Reflection re·flec·tion rəˈflekSH(ə)n/ noun
Objects First with Java A Practical Introduction using BlueJ
Java Collections Overview
Interfaces.
Chapter 9 Inheritance and Polymorphism
Chapter 12 Collections.
Java Programming Language
COS 260 DAY 2 Tony Gauvin.
Lecture 1: Multi-tier Architecture Overview
Object Oriented Practices
Object-Oriented Programming Paradigm
Event loops 17-Jan-19.
More About Inheritance & Interfaces
Chapter 12 Collections.
Creating and Modifying Text part 3
TCSS 143, Autumn 2004 Lecture Notes
Event loops.
CSE 143 Lecture 21 Advanced List Implementation
Presentation transcript:

Interface Java 7 COMP 112 2017T1

Overview Interface as Object with only empty method bodies. Signature or Type Contract Interface used to Decouple software components Hide internal detail Provide data abstraction Provide a kind of multiple inheritance

Interface and Signature All you know to call a method is its signature <return type> name [<parameter type>] +exceptions The signature of an Object is the set of its method signatures To use an object know its signature. An Interface is a signature Implementing an interface means implementing the methods. One Object can implement several interfaces Several Objects can implement the same interface

Refresh Java is an Object Oriented Language Objects are collections of Fields and attached methods When you pass an Object as a parameter you: Pass the values held in the fields Pass the methods attached to the object What design options are opened up by passing methods? Dynamic (Runtime) dispatch. This means what code is actually run is decided while a program is running!

Holes in code! Hole The Person Interface has method greater() The Sorted method has an array of Persons as parameter! What shape is the hole? Code decided at run time Hole in code All we need know is its signature

Decomposing Java projects Java has a predefined Comparator interface. Many Java sort functions have a Comparator shaped hole that can be plugged by passing a Comparator as a parameter This means Oracle write the sort program not knowing anything about you or your code. You write the comparator and now your code will work with code written by a remote group. When working as part of a large team you will need to decompose a problem into parts connected via an interface. So that code developed by different team members has some chance of working together.

Colored Plugs Different objects implement the interface differently Two objects implement an interface then either can be used when the interface is expected Different objects can be different types of Plug Recall (equals, reading objects from files) you can: check which type of object you have by using instanceof for example if (s instanceof Student) {… cast an object to fix its type for example Student s = (Student) p; Both techniques become more useful when you are using interfaces

A Student is a Person Person is an interface Student implements a Person The sorted method will accept a Student[] parameter. If Lecturer implements person it too can be used.

Why Use Interfaces? Interfaces allow different parts of a large system to be implemented separately. For example: The java virtual machine JVM was developed independently of your software and publicized a MouseListener interface. When you implement this interface the JVM is able to call you back when the mouse is clicked. Interfaces allow new objects to be designed that provide new functionality (methods) with out recompiling the system (the code that calls the methods).

Interfaces and Design Flexibility with out recompilation Add an ArrayList<Walls> of the 4 Walls refactor code to be able to collide with any wall in list. We want to add the central obstacle to the list Refactor to an ArrayList<Hittable> of things that can be hit. Make Wall and obstacle implement the Hittable interface With this design: New walls and obstacles can be added at runtime New objects that implement the Hittable interface can be added.

Summary An Interface is a Class without method bodies A Class implements an interface if it implements all the methods Interfaces help decouple code. The Java Run Time System and your GUI code. After a system is up and running you can design new Objects with new functionality and the system can use this new functionality with out being recompiled. Sorting Lectures based on age.

Empty methods and Contracts An Object without Fields and with empty method bodies is an interface An interface defines a weak sort of contract. An object that implements the interface can be used where ever interface was expected. What the object does is not guaranteed

Example Scanner implement an iterator interface. While code could be From an ArrayList you can construct an iterator While code could be applied to an ArrayList