concepts of object oriented programming
play

Concepts of Object-Oriented Programming 7 January 2019 OSU CSE 1 - PowerPoint PPT Presentation

Concepts of Object-Oriented Programming 7 January 2019 OSU CSE 1 Recall... Standard extends NaturalNumber- Kernel extends NaturalNumber implements implements NaturalNumber1L NaturalNumber2 7 January 2019 OSU CSE 2 The


  1. Concepts of Object-Oriented Programming 7 January 2019 OSU CSE 1

  2. Recall... Standard extends NaturalNumber- Kernel extends NaturalNumber implements implements NaturalNumber1L NaturalNumber2 7 January 2019 OSU CSE 2

  3. 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 method 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 } 7 January 2019 OSU CSE 3

  4. The “Implements” Relation • The implements relation may hold between The implements relation allows you to separate contracts from a class and an interface their implementations — a best • If C implements I then class C contains practice for component design. code for the behavior specified in interface I – This means C has method 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 } 7 January 2019 OSU CSE 4

  5. The “Implements” Relation The Java compiler checks that C • The implements relation may hold between contains bodies for the methods in a class and an interface I , but does not check that those • If C implements I then class C contains bodies correctly implement the method contracts! code for the behavior specified in interface I – This means C has method 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 } 7 January 2019 OSU CSE 5

  6. The “Extends” Relation • The extends relation may hold between: – Two interfaces (as on the earlier slide), or – 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 (for an interface) or all the method bodies (for a class) that A has – B can then add more method contracts (for an interface) or method bodies (for a class) 7 January 2019 OSU CSE 6

  7. Caveats About Java Interfaces • “If B extends A then B inherits all the methods of A ” – Interfaces cannot have constructors • So there is no good place to write separate contracts for the constructors of classes that implement an interface – Interfaces cannot have static method contracts without also providing corresponding method bodies • So there is no good place to write separate contracts for public static methods of classes that implement an interface 7 January 2019 OSU CSE 7

  8. Caveats About Java Classes • “If B extends A then B inherits all the methods of A ” – Constructors are not inherited • So in the situation above, the class B must include bodies for any constructors that are expected, even if they would be identical to those of A • The bodies of the constructors in B generally would simply invoke the constructors of A , which is done using the special notation super (…) – Static methods are inherited 7 January 2019 OSU CSE 8

  9. “Implements” May Be Inferred I1 extends I2 implements C3 extends C4 7 January 2019 OSU CSE 9

  10. “Implements” May Be Inferred I1 extends implements I2 implements C3 We may infer in extends this case that C3 implements I1 . C4 7 January 2019 OSU CSE 10

  11. “Implements” May Be Inferred I1 We may also infer in this case that extends C4 implements I2 . I2 implements C3 implements extends C4 7 January 2019 OSU CSE 11

  12. Interface Extension • If I1 and I2 are interfaces and I2 extends I1 , then the code for I2 looks like this: interface I2 extends I1 { // contracts for methods added in I2 } I1 extends I2 7 January 2019 OSU CSE 12

  13. Interface Extension Remember, for interfaces all such methods are instance methods! • If I1 and I2 are interfaces and I2 extends I1 , then the code for I2 looks like this: interface I2 extends I1 { // contracts for methods added in I2 } I1 extends I2 7 January 2019 OSU CSE 13

  14. Interface Extension • If I1 and I2 are interfaces and I2 extends I1 , then the code for I2 looks like this: interface I2 extends I1 { // contracts for methods added in I2 } Other terminology for this I1 situation: I2 is a subinterface of I1 extends I2 is a derived interface of I1 I2 I2 is a child interface of I1 7 January 2019 OSU CSE 14

  15. Interface Extension • If I1 and I2 are interfaces and I2 extends I1 , then the code for I2 looks like this: interface I2 extends I1 { // contracts for methods added in I2 } Other terminology for this I1 situation: I1 is a superinterface of I2 extends I1 is a base interface of I2 I2 I1 is a parent interface of I2 7 January 2019 OSU CSE 15

  16. Example: Interface Extension clear newInstance Standard transferFrom + extends multiplyBy10 NaturalNumber- divideBy10 Kernel isZero = clear multiplyBy10 newInstance divideBy10 transferFrom isZero 7 January 2019 OSU CSE 16

  17. Example: Interface Extension clear NaturalNumberKernel actually newInstance Standard has all these methods, even though transferFrom their contracts are in two separate + extends interfaces. multiplyBy10 NaturalNumber- divideBy10 Kernel isZero = clear multiplyBy10 newInstance divideBy10 transferFrom isZero 7 January 2019 OSU CSE 17

  18. Example: Interface Extension clear The extends relation for interfaces newInstance Standard allows you to separate contracts into transferFrom smaller chunks — arguably a best + extends practice for component design. multiplyBy10 NaturalNumber- divideBy10 Kernel isZero = clear multiplyBy10 newInstance divideBy10 transferFrom isZero 7 January 2019 OSU CSE 18

  19. Class Extension • For classes, extension can serve two different purposes: – To add method bodies that are not already in the class being extended (similar to the use of extension for interfaces) – To override methods that are already implemented in the class being extended, by providing new method bodies for them 7 January 2019 OSU CSE 19

  20. Class Extension When pronounced, this may sound • For classes, extension can serve two like “overwrite”, but that is not a different purposes: correct interpretation! – To add method bodies that are not already in the class being extended (similar to the use of extension for interfaces) – To override methods that are already implemented in the class being extended, by providing new method bodies for them 7 January 2019 OSU CSE 20

  21. Class Extension • For classes, extension can serve two For now, we are concerned only with this use of class extension. different purposes: – To add method bodies that are not already in the class being extended (similar to the use of extension for interfaces) – To override methods that are already implemented in the class being extended, by providing new method bodies for them 7 January 2019 OSU CSE 21

  22. Class Extension Important note: Overriding a method is different from overloading a method! A method (name) is overloaded when two or • For classes, extension can serve two more methods have the same name, in which case the methods must differ in the number different purposes: and/or types of their formal parameters (which – To add method bodies that are not already in the compiler uses to disambiguate them). the class being extended (similar to the use of extension for interfaces) – To override methods that are already implemented in the class being extended, by providing new method bodies for them 7 January 2019 OSU CSE 22

  23. Class Extension • If C1 and C2 are classes and C2 extends C1 , then the code for C2 looks like this: class C2 extends C1 { // code for methods added or // overridden in C2 } C1 extends C2 7 January 2019 OSU CSE 23

  24. Class Extension Remember, for classes these may be either static methods or instance methods. • If C1 and C2 are classes and C2 extends C1 , then the code for C2 looks like this: class C2 extends C1 { // code for methods added or // overridden in C2 } C1 extends C2 7 January 2019 OSU CSE 24

  25. Class Extension • If C1 and C2 are classes and C2 extends C1 , then the code for C2 looks like this: class C2 extends C1 { // code for methods added or // overridden in C2 } Other terminology for this situation: C1 C2 is a subclass of C1 C2 is a derived class of C1 extends C2 is a child class of C1 C2 7 January 2019 OSU CSE 25

  26. Class Extension • If C1 and C2 are classes and C2 extends C1 , then the code for C2 looks like this: class C2 extends C1 { // code for methods added or // overridden in C2 } Other terminology for this situation: C1 C1 is a superclass of C2 C1 is a base class of C2 extends C1 is a parent class of C2 C2 7 January 2019 OSU CSE 26

  27. Example: Overriding a Method ... power NaturalNumber2 ... extends NaturalNumber2- power Override 7 January 2019 OSU CSE 27

  28. Example: Overriding a Method ... power NaturalNumber2 ... extends NaturalNumber2- power Override There is a method body for power in NaturalNumber2 ... 7 January 2019 OSU CSE 28

  29. Example: Overriding a Method ... power NaturalNumber2 ... extends NaturalNumber2- power Override ... and there is another method body for power in NaturalNumber2Override . 7 January 2019 OSU CSE 29

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