Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Lecture 5: Interfaces.
Computer Science and Engineering College of Engineering The Ohio State University Classes and Objects: Members, Visibility The credit for these slides.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Road Map Introduction to object oriented programming. Classes
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Software Testing and Quality Assurance
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 10 Classes Continued
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Ranga Rodrigo. Class is central to object oriented programming.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Computer Science and Engineering College of Engineering The Ohio State University Lot More Inheritance and Intro to Design Patterns Lecture 12.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Inheritance in the Java programming language J. W. Rider.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ECE122 Feb. 22, Any question on Vehicle sample code?
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Object Oriented Programming
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces and Inner Classes
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
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.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Inheritance and Polymorphism
Chapter 3: Using Methods, Classes, and Objects
Interface.
Java Programming Language
Conditional Statements
Interfaces.
Inheritance.
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
CS148 Introduction to Programming II
SPL – PS3 C++ Classes.
Introduction to Classes and Objects
Presentation transcript:

Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti at The Ohio State University. The slides have been modified a bit for use at Clemson by Professors Murali Sitaraman and Jason Hallstrom.

Computer Science and Engineering The Ohio State University Syntax  An interface is a set of requirements Describes what classes should do Does not describe how they should do it  Example public interface Salaried { void setSalary(BigDecimal d); BigDecimal getSalary(); }  To satisfy this interface, a class must provide setSalary and getSalary methods with matching signatures (checked by compiler) matching behaviors (up to you)

Computer Science and Engineering The Ohio State University Declaring an Interface  Looks like a class definition, except: Keyword interface replaces class Methods have no body No constructors  The interface itself is public or package visible

Computer Science and Engineering The Ohio State University Examples public interface Salaried { void setSalary(BigDecimal d); BigDecimal getSalary(); } interface Voter { int MINIMUM_AGE = 18; Voter(short age); //compile-time error void Register(District d); boolean isRegistered(); }

Computer Science and Engineering The Ohio State University Implementing an Interface  Declare a class that implements the interface public class Employee implements Salaried {...}  Supply definitions for all interface methods public void setSalary (BigDecimal d) { … } public BigDecimal getSalary() { … }  Class can declare more methods than required by interface

Computer Science and Engineering The Ohio State University Interfaces and Implementations  Interfaces allow separation of client-side view and implementer’s view  Client-side Description of what a component does Abstract state, the “model” Requires and ensures clauses (later)  Implementer’s side Description of how component works Concrete state, the “representation”  Matching concepts in Java Interface: Client-side Class: Implementer

Computer Science and Engineering The Ohio State University Role of Interfaces vs Classes  Interfaces (should) provide Method signatures Model (later) Constraints (later) Method specifications (later)  Classes (should) provide Concrete representation (in private fields) Concrete implementation (in method bodies) Representation invariant Correspondence (later) Method specifications (later)

Computer Science and Engineering The Ohio State University Good Practice: Naming Interfaces  How should interfaces be distinguished from classes in their names?  One approach Classes end in “Impl” e.g., Stack vs FastStackImpl  Microsoft approach Interfaces start with “I” eg IStack vs Stack  Java approach No difference, both are nouns or adjectives

Computer Science and Engineering The Ohio State University Instantiating an Interface  The declared type of a variable can be an interface public interface Salaried {... } Salaried payee; //ok  But interfaces cannot be instantiated directly payee = new Salaried(); //compile-time error  Only classes can be instantiated directly  Variable of type I can refer to an instance of a class that implements I Public class Employee implements Salaried {…} Salaried payee = new Employee(); //ok  (This might remind you of widening!)

Computer Science and Engineering The Ohio State University Interfaces and Classes Consultant Employee Salaried implements Salaried s = new Employee(); Salaried s2 = new Consultant(); Salaried s3 = s;

Computer Science and Engineering The Ohio State University Declared vs Dynamic Type  Declared type = set at compile time (by declaration)  Dynamic type = set at run time (by new) Type1 variable = new Type2(); Examples Employee p = new Employee(“Pierre”); Salaried s = new Employee(“Liz”, 12345); s = p; //dynamic type of s is:  Compiler can not infer dynamic type public void select (Salaried s) { //declared type of s is: Salaried //dynamic type of s is: ???... }  Operator instanceof tests the run-time type (later) if (s instanceof Employee) {... } else if (s instanceof Consultant) {... }

Computer Science and Engineering The Ohio State University Role of Declared Type  Declared type determines which members can be used public class Employee implements Salaried { public void setSalary (BigDecimal d) {...} public BigDecimal getSalary() {...} public void promote (int r) {...} }... public void select (Salaried s) { s.setSalary(new BigDecimal(“ ”)); s.promote(0); //compile-time error }  Only interface members can be called/accessed by client Class method is the code to execute when called That method code can access all class members

Computer Science and Engineering The Ohio State University Simple Rule #1  Rule: Interfaces can only be used as declared types = Interfaces are never dynamic types = Interfaces are never instantiated = All dynamic types are classes = All run-time objects are constructed from a class, not an interface Declared TypesDynamic Types Interfaces Classes

Computer Science and Engineering The Ohio State University  “Coding to the interface” means all declared types are interface types All variable and field declarations use interface types Salaried lastHire = new Employee(); All argument and return types in method signatures are interface types public Voter choose(Salaried[] s) {...} Good Practice: Code to Interface Dynamic Types Declared Types ClassesInterfaces

Computer Science and Engineering The Ohio State University Implementing Multiple Interfaces  One class can implement several interfaces public class Employee implements Salaried, Voter { … }  Class must provide functionality from all interfaces it implements Union of method signatures Satisfies the behavioral contracts of all interfaces it implements

Computer Science and Engineering The Ohio State University Multiple Interfaces Consultant Employee Voter Salaried implements Voter v = new Employee(); Salaried s = new Employee(); Salaried s2 = new Consultant(); Salaried s3 = v; //compile-time error

Computer Science and Engineering The Ohio State University Summary  Declaring an interface Method signatures without implementation Fields too, but this is less common All implicitly public  Implementing an interface Class provides implementation for all methods  Separation of client-side and implementation Interface has abstract state, invariant, specs Classes have concrete representation, convention  Declared vs dynamic type Interfaces can not be instantiated