Interfaces 1. Interfaces are (parts of) contracts Interfaces are contracts between implementers and consumers Consumers: Programmers using a class implementing.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Chapter 10: Introduction to Inheritance
Advanced Programming in Java
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Chapter 10 Classes Continued
5. OOP. 2 Microsoft Objectives “Classes, objects and object-oriented programming (OOP) play a fundamental role in.NET. C# features full support for the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Module 7: Essentials of Object-Oriented Programming.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
References types and Value types With a very brief introduction to struct and enum Reference types and Value types1.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Introduction to Building Windows 8.1 & Windows Phone Applications.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between.
Advanced C# Types Tom Roeder CS fa. From last time out parameters difference is that the callee is required to assign it before returning not the.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Generics in C# 1. Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”);
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Introduction to Object-Oriented Programming Lesson 2.
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.
Abstract Classes and Interfaces Week 17.  Computer simulations  Abstract methods  Abstract classes  Interfaces  Multiple inheritance Abstract Classes.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Microsoft Code Contracts How to program Pre-conditions, Post-conditions, and Object Invariants Microsoft Code Contracts1.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
LINQ Language Integrated Query LINQ1. LINQ: Why and what? Problem Many data sources: Relational databases, XML, in-memory data structures, objects, etc.
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 5:Interfaces and Abstract Classes
C# for C++ Programmers 1.
Classes (Part 1) Lecture 3
Advanced Object-Oriented Programming Features
2.7 Inheritance Types of inheritance
Critical sections, locking, monitors, etc.
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
Chapter 5: Programming with C#
Delegates/ Anders Børjesson
Interfaces.
Interface.
Conditional Statements
Interfaces.
Generics in C# / Anders Børjesson
How to organize and document your classes
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Classes CS 21a: Introduction to Computing I
Interface 11: Interface Programming C# © 2003 DevelopMentor, Inc.
Chengyu Sun California State University, Los Angeles
5. OOP OOP © 2003 Microsoft.
Presentation transcript:

Interfaces 1

Interfaces are (parts of) contracts Interfaces are contracts between implementers and consumers Consumers: Programmers using a class implementing the interface The interfaces specifies the syntax of methods, etc. But not the semantics Missing: Class invariants, pre- and post-conditions, etc. It is not the full contract!! Example: LayeredLibrary, IRepository The interface IRepository defines an contract between the upper layers (GUI etc.) and the lower layers (database, file, etc.) in the application. Example: Producer-Consumer, IBuffer Interfaces2

Defining an interface public interface ISomeName { void Method1(); // no body int Method2(String a); // no body } Naming convention Names of interfaces usually start with the letter ‘I’ Example: LayeredLibrary Interfaces3

Implementing interfaces Public class A : ISomeInterface { // implement ALL the methods, etc. from the interface // The compiler check that the class implements ALL the methods } The colon should be read as “implements” A class an implement any number of interfaces Bu only extend one other class The more interfaces a class implements, the more contracts the class must fulfill More methods in the class … Example: The class List implements 8 interfaces Interfaces4

Interfaces vs. inheritance Inheritance means ”is a” Example class Teacher : Person Teacher is a Person. Base class describe what an object is Interfaces means ”behaves like” Example class Teacher : IComparable Teacher behaves like an IComparable A Teacher object is comparable to other Teacher objects Interface describe one way in which it behaves Interfaces5

Implementing multiple interfaces “By using interfaces, you can, for example, include behavior from multiple sources in a class.” In C# there is no multiple inheritance of classes. List implements 8 interfaces. Interfaces6

What you can have in an interface Method signatures No body Properties declarations No body Interfaces7

What you cannot have in an interface No fields No constructors No access modifiers All method are implicitly public Nothing static No nested types No struct, enum, class, or interface inside an interface No inheritance However, you can extends other interfaces Meaning extending another “contract” Adding to the “contract” Interfaces8

Some popular interfaces IDisposable One method: void Dispose() Object implementing this interface can be used in a using statement Using (someDisposable) { …. } IEnumerable Object implementing this interface can be iterated with a foreach statement One method: IEnumerator GetEnumerator() Extended by other interfaces like Collection IEnumerator Property: Current Methods: MoveNext, etc. Collection Implemented by many collections Extended by many other interfaces like IList IList Implemented List Interfaces9

Interface vs. abstract class vs. class: The “Body” Rule Interface No methods have a body An interface is more abstract than an abstract class! No-body Abstract class Some methods have a body, some methods do not have a body Some-body Class Every method have a body Every-body Interfaces10

Implementation hierarchy: Partial implementations Interface, class, abstract class Interfaces defines common behavior Classes implement this behavior Different classes can do different implementations Duplicate code in the implementations can be refactored into an Abstract base class The abstract class implements some of the methods from the interface Example: Buffers from Producer- Consumer Interface IBuffer {... } Class AbstractBuffer : IBuffer { Class BoundedBuffer : AbstractBuffer { … } Class UnboundedBuffer : AbstractBuffer { … } Interfaces11

Single-method and zero-method interfaces Single-method interface Usually a delegate type is better Example: ThreadStart Zero-method interface Aka. Marker interface Usually annotations are better Example: Unit testing [TestClass], [TestMethod], etc. Interfaces12

Dogma rule: Every class should have an interface Every class should be defined by an interface. The interface should be public The class should not be public The interfaces should be used everywhere possible That is everywhere in the program, except after the keyword “new” The use of “new” can be hidden through the use of a “factory” Inspiration: Dogma 95 Interfaces13

References and further readings MSDN Interfaces (C# Programming Guide) MSDN interface (C# Reference) John Sharp: Microsoft Visual C# 2013, Step by Step, Microsoft Press 2012 Chapter 13 Creating Interfaces and Defining Abstract Classes, page Bart De Smet: C# 5.0 Unleashed, Sams 2013 Chapter 14 Object-Oriented Programming Interface Types, page Interfaces14