java interfaces
play

Java Interfaces 6 May 2019 OSU CSE 1 Conceptual Framework A - PowerPoint PPT Presentation

Java Interfaces 6 May 2019 OSU CSE 1 Conceptual Framework A firm conceptual foundation for understanding and designing modern software recognizes: Modern software consists of potentially large numbers of components that are composed


  1. Java Interfaces 6 May 2019 OSU CSE 1

  2. Conceptual Framework • A firm conceptual foundation for understanding and designing modern software recognizes: – Modern software consists of potentially large numbers of components that are composed into “larger” components/systems – In Java, one ordinarily thinks of a class as a component, with its client-visible specification in the interface(s) it implements 6 May 2019 OSU CSE 2

  3. Conceptual Framework An interface contains a • A firm conceptual foundation for description of what understanding and designing modern software does. software recognizes: – Modern software consists of potentially large numbers of components that are composed into “larger” components/systems – In Java, one ordinarily thinks of a class as a component, with its client-visible specification in the interface(s) it implements 6 May 2019 OSU CSE 3

  4. Conceptual Framework An interface contains a A class contains a • A firm conceptual foundation for description of what description of how the understanding and designing modern software does. software does it. software recognizes: – Modern software consists of potentially large numbers of components that are composed into “larger” components/systems – In Java, one ordinarily thinks of a class as a component, with its client-visible specification in the interface(s) it implements 6 May 2019 OSU CSE 4

  5. Anatomy of an Interface /** * An informal Javadoc comment. * [additional Javadoc comments as needed; in our * case: the mathematical model, contract(s) for * constructor(s), optional contract for iterator] */ public interface I { // contract(s) for method(s) } 6 May 2019 OSU CSE 5

  6. Anatomy of an Interface /** An interface may * An informal Javadoc comment. extend one or more * [additional Javadoc comments as needed; in our other interfaces. * case: the mathematical model, contract(s) for * constructor(s), optional contract for iterator] */ public interface I extends I1, I2 { // contract(s) for method(s) } 6 May 2019 OSU CSE 6

  7. Anatomy of an Interface An interface may be /** generic , as seen here; * An informal Javadoc comment. formal parameter T may * [additional Javadoc comments as needed; in our have any name, but T is * case: the mathematical model, contract(s) for typical (for “type”). * constructor(s), optional contract for iterator] */ public interface I<T> extends I1<T>, I2 { // contract(s) for method(s) } 6 May 2019 OSU CSE 7

  8. Technicalities • Interfaces may be used to define: – instance methods – static final variables (“constants”) – Not constructors – Not static methods – Not instance variables 6 May 2019 OSU CSE 8

  9. Technicalities • Interfaces may be used to define: – instance methods – static final variables (“constants”) Instance methods in an – Not constructors interface are by default – Not static methods public abstract . – Not instance variables ( private instance methods and public / private static methods are allowed—but they cannot be abstract.) 6 May 2019 OSU CSE 9

  10. Technicalities • Interfaces may be used to define: – instance methods – static final variables (“constants”) – Not constructors Variables are – Not static methods automatically – Not instance variables public static final ; it is unusual for an interface to define variables. 6 May 2019 OSU CSE 10

  11. Technicalities • Interfaces may be used to define: – instance methods – static final variables (“constants”) – Not constructors – Not static methods A constructor always – Not instance variables has the name of a class, and is not an instance method. 6 May 2019 OSU CSE 11

  12. Technicalities • Interfaces may be used to define: Interfaces allow static – instance methods methods with their – static final variables (“constants”) implementations . But – Not constructors providing code in interfaces goes – Not static methods against our use of – Not instance variables interfaces for client views (this biases designs to instance methods). 6 May 2019 OSU CSE 12

  13. Technicalities • Interfaces may be used to define: – instance methods – static final variables (“constants”) – Not constructors – Not static methods This is a good thing: – Not instance variables instance variables are not client-view information! 6 May 2019 OSU CSE 13

  14. Example: Queue Component Family Standard Iterable extends extends QueueKernel extends Queue 6 May 2019 OSU CSE 14

  15. Example: Queue Component Family Standard Iterable extends extends QueueKernel Which methods are in Standard , extends and why? Queue 6 May 2019 OSU CSE 15

  16. Example: Queue Component Family We call the “core interface” in this design style the kernel Standard Iterable interface. extends extends QueueKernel extends Queue 6 May 2019 OSU CSE 16

  17. Example: Queue Component Family We call the “most powerful interface” in this design style the Standard Iterable enhanced interface. extends extends QueueKernel extends Queue 6 May 2019 OSU CSE 17

  18. Let’s Examine... • Standard Ask about details, e.g.: • QueueKernel • Design choices (math model, constructor, kernel • Queue methods, other methods) • Javadoc (compare code in the Java interface with the generated documentation) 6 May 2019 OSU CSE 18

  19. Interface Design • The kernel interface defines: – The mathematical model for the type – Contract(s) for the constructor(s) – Contract(s) for kernel methods – Contract(s) for methods inherited from Java library interfaces that do not have their own contract specifications (if applicable; e.g., an iterator or a comparator) • The enhanced interface defines contracts for all other methods for the type 6 May 2019 OSU CSE 19

  20. Kernel Design Guidelines • Kernel methods generally should be: – A minimal set of methods that is functionally complete , i.e., powerful enough to: • Give a variable of the type any allowable value • Determine the value of a variable of the type – Powerful enough to allow a client to: • Implement equals and toString • Check every kernel method’s precondition 6 May 2019 OSU CSE 20

  21. Kernel Design Guidelines • Kernel methods generally should be: – A minimal set of methods that is functionally complete , i.e., powerful enough to: “Minimal” means if any proposed • Give a variable of the type any allowable value kernel method were left out, you • Determine the value of a variable of the type could not satisfy one of the completeness tests—though – Powerful enough to allow a client to: sometimes an “extra” kernel • Implement equals and toString method is so parsimonious it is • Check every kernel method’s precondition included in the kernel anyway! 6 May 2019 OSU CSE 21

  22. Notice: Circularity public interface QueueKernel<T> extends Standard<Queue<T>>, Iterable<T> { ... } public interface Queue<T> extends QueueKernel<T> { ... } 6 May 2019 OSU CSE 22

  23. Notice: Circularity public interface QueueKernel<T> extends Standard<Queue<T>>, Iterable<T> { ... Using Queue<T> is necessary because it is the } return type of newInstance and the interface type we always use to declare Queue variables. public interface Queue<T> extends QueueKernel<T> { ... } 6 May 2019 OSU CSE 23

  24. What “Mentions” What? Standard Iterable “mentions” “mentions” QueueKernel “mentions” “mentions” Queue 6 May 2019 OSU CSE 24

  25. Interface = Type • Java permits the circularity here because: – A Java interface defines a type , i.e., the type name (which we consider to denote a set of mathematical model values that certain variables might have) along with the set of its instance methods – An interface type may be used in Java even if there is no implementation of it in scope 6 May 2019 OSU CSE 25

  26. Interfaces: Only At Compile-Time • Interfaces are a compile-time construct – Used by the Java compiler for type-checking with declared types: to make sure variables are used only where they make sense • Recall the rules for declared types and object (dynamic) types – Once a Java program compiles, only object types are kept at run-time • Declared types literally “disappear” in the JVM 6 May 2019 OSU CSE 26

  27. Interfaces: Only At Compile-Time • The declared type of a variable may be an interface type • The object type can never be an interface type, because you may not instantiate a variable using new followed by • Interfaces are a compile-time construct an interface type • If the declared type is an interface type, then the object – Used by the Java compiler for type-checking type (a class) must implement the declared type (an with declared types: to make sure variables interface) are used only where they make sense • Recall the rules for declared types and object (dynamic) types – Once a Java program compiles, only object types are kept at run-time • Declared types literally “disappear” in the JVM 6 May 2019 OSU CSE 27

  28. Javadoc Tags • Recall that the short one-sentence informal overview, and the following standard Javadoc tags, are “expected”: – @param – @return 6 May 2019 OSU CSE 28

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend