CPSC 2150 - DAY 13 OCTOBER 11, 2016 ================================================================================ A good interface design provides a functionally sufficient set of primary ops. .E.g. stack objects would not be very usable if the interface is missing push, pop, or depth operation But additional secondary functionality is useful Reusable Code for Secondary Ops .But additional secondary functionality is useful (last lecture) .E.g. stack top operation contract How to implement the additional operation so the code is reusable?? .Reduces the amount of code that you have to write. The "implements" Relation: .The implements relation may hold between a class and an interface .If C implements I, then class c contains code for the behavior specified in interface I. .This means C has meathod bodies for instance methods whose contracts are specified in I .the code for c looks like this: class C implements I { // bodies for methods specified in I } ***Only instance methods -- not static methods can be specified in Java interfaces*** @Override Annotation -------------------- .When writing the code for the body of either a method: .whose contract is from an interface being implemented, or .that overrides a method in a class being extended. you preface the method body with an @Override annotation. ***Overriding and overloading are NOT the same thing*** A method (name) is overloaded when two or more methods have the same name, in which case the methods must differe in the number and/or types of their formal parameters (which the compiler uses to disambiguate them) Overriding is when they are both identical Recap: Inheritance ------------------ Why? .Need a way to extend existing interfaces (and classes) to include new features Why can't we just edit existing interfaces (and clases) .because edits can compromise the quality of previously correct code! How to do this in Java? .rest of lecture... public interface IntegerStackwTop extends IntegerStack { } The extends relation may hold between: .Two interfaces .Two classes In either case, if B extends A, then B inherits all the methods of A .This means B implicitly starts out with all the method contracts (from an interface) or all the method bodies (for a class) that A has .B can add more method contracts (and method bodies) Abstract Classes ---------------- Java permits you to write a kind of "partial" or "incomplete" class that contains bodies for some but (typically) not all of the methods of the interfaces it claims to implement Such a class is called an abstract class: abstract class AC implements I { ... } Because some methods might not have bodies, Java will not let you instantiate an abstract class; that is, you cannot use an abstract class as a normal class. Method bodies that can be written once and work for any implementation of IntegerStack because they are programmed to that interface THE UBIQUITIOUS CLASS: Object ----------------------------- Every class in Java extends Object, which is a special built-in class that provides default implementations for the following instance methods (amond a few others that are not so important): boolean equals(Object obj) int hashCode(); equals compares the reference, so we almost always need to override this method ***Write this in the abstract class so you only have to write it once!*** int hashCode() .Returns an int value that is functionally determined by value fo this The abstract class can include code that overrides the default implementations of equals, hashCode, tostring for IntegerStacks so their behaviors are based on object instead of reference