1 / 14

Chapter 13 Encapsulation

Chapter 13 Encapsulation. Encapsulation. Good design separates use from implementation . Details are kept hidden from users. We drive cars without knowing how spark plugs work. This gives us flexibility to change an implementation. encapsulation is a package. Procedural abstraction.

kyounger
Download Presentation

Chapter 13 Encapsulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 13Encapsulation

  2. Encapsulation • Good design separates use from implementation. • Details are kept hidden from users. • We drive cars without knowing how spark plugs work. • This gives us flexibility to change an implementation. encapsulation is a package Prof. Dr. Debora Weber-Wulff

  3. Procedural abstraction • Find (almost) common code • use an abstraction, with parameters if necessary • if the code is longer than a page (~50 lines) break it up into bits! Don´t use the copy button! COPY Prof. Dr. Debora Weber-Wulff

  4. Rules of Thumb • Succinct description of what not how • Fit on a single page • needed more than one place •  make a new method Prof. Dr. Debora Weber-Wulff

  5. The Description Rule of Thumb • Each method should have a well – defined purpose. • Each purpose in your program should have its own method. • The user needs to be able to understand what your method does, not have to understand how it works. • There can be multiple methods per description, for example with different parameters. • Succinct description make your code more readable and easier to maintain. Prof. Dr. Debora Weber-Wulff

  6. A method whose function is not succinctly describable is probably not a good method. • Keep • It • Simple • Stupid Prof. Dr. Debora Weber-Wulff

  7. SMASH! bbbjhzt jgjhg hkj jgjhgj uznbk ghlkö jhgkjä kk jhanbbk kk kjbkjkjkk dgkjdkd ggg dkgölddöd dökdölkg dölk ödlf dlö ökgk dfk ölkg gk döfö flgö d gldgdd The Length Rule of Thumb • 1 method ≈ 1 page • (better: 1 screen!) • If it gets larger: describe the work in an outline and break it up! • Don´t worry about inefficiency! • A good compiler (i. e. one that costs money) can optimize it. Prof. Dr. Debora Weber-Wulff

  8. The Repitition Rule of Thumb • If the same code appears twice (or more often!): abstract or encapsulate it! • Inside a class, this method is usually private, but can be public if it turns out to be useful. • Removing redundancy shortens programs! 1 place instead of 4! Prof. Dr. Debora Weber-Wulff

  9. if (theValue < 0) { • System.out.println ("the value is: " + theValue); • global Variable ++; • theAnswer = ((-someConstant) * theValue%2); • } else if (theValue > 0) { • System.out.println ("the value is: " + theValue); • global Variable ++; • theAnswer = ((someConstant) * theValue%2); • } else { • System.out.println ("the value is: " + theValue); • global Variable ++; • theAnswer = 0; • } ??? Prof. Dr. Debora Weber-Wulff

  10. Protecting Internal Structure • private • !An object can call the private method of any other instance of the same class! • packages / libraries • package java.lang; • But any visible name accessable by • java.lang.String • import cs101.util.*; • is just a short form indicator •  Not real encapsulation •  No hierarchy Prof. Dr. Debora Weber-Wulff

  11. Protecting Internal Structure (cont.) • inheritance •  hidden behavior through subclassing •  labeling with superclass hides members • But inheritance can break encapsulation! • interfaces • has no implementation, but defines what has to exist. • But implementator can circumvent this! Prof. Dr. Debora Weber-Wulff

  12. Inner Classes I • An inner class is a class defined inside another. • Static classes • - top – level inside another class • - use new OuterClass.InnerClass() as the constructor • - keyword static • - has access to static members of the containing class Prof. Dr. Debora Weber-Wulff

  13. Inner Classes II • Member classes • - not static • - one inner class per instance of the outerclass • - priveledged access to private data: • Check / Checking Account • - use new Instance.InnerClass() Prof. Dr. Debora Weber-Wulff

  14. Inner Classes III • Local classes • - statement, not a member • - defined in a block • - one class per execution • - can see lots of interesting fields • - DON´T BOTHER with this now • Anonymous classes • - only good for one instance • - new TypeName {<members>} • - good for event handling Prof. Dr. Debora Weber-Wulff

More Related