Chapter 3 – Implementing ClassesIntroductionSlide 3Example ProgramSlide 5Black BoxesSlide 7Slide 8EncapsulationExample: CarsExample: CarSlide 12Example: DoorbellSlide 14Software DesignSlide 16Slide 17Slide 18AbstractionReviewDesigning a ClassSlide 22MethodsSlide 24Slide 25Methods: SyntaxSlide 27ConstructorsSlide 29Constructors vs. MethodsConstructors: SyntaxPublic InterfaceSlide 33Class Definition SyntaxSlide 35Javadoc CommentsSlide 37Slide 38Slide 39Slide 40CommentingSlide 42Slide 43Instance FieldsSlide 45Slide 46Access SpecifiersSlide 48 Instance FieldsAccessing Instance FieldsSlide 51Slide 52Constructor BodiesConstructors – What Happens?Method BodiesSlide 56Implementing MethodsMethods – What Happens?Return Statement SyntaxSlide 60Slide 61Slide 62Slide 63Three Types of ClassesClasses: Type 1Slide 66Classes: Type 2Slide 68Classes: Type 3Slide 70Testing Instantiable ClassesSlide 72Slide 73Slide 74Categories of VariablesSlide 76Garbage CollectorLocal VariablesParameter variablesScopeSlide 81Slide 82The Implicit ParameterSlide 84Slide 85Slide 86Implicit Parameters and thisSlide 88Advanced InfoExampleSlide 91Recap on Designing ClassesSlide 93Slide 94Notes on StyleSlide 96Slide 97Slide 98Chapter 3 – Chapter 3 – Implementing ClassesImplementing ClassesIntroductionIntroductionIn the previous chapter, we saw how to In the previous chapter, we saw how to useuse objectsobjectsDeclare an objectDeclare an objectBook aBook;Book aBook;Create an objectCreate an objectaBook = new Book(“Beloved”,“Toni Morrison”);aBook = new Book(“Beloved”,“Toni Morrison”);Call methods on an objectCall methods on an objectaBook.getAuthor();aBook.getAuthor();IntroductionIntroductionRemember our goal is to write programs Remember our goal is to write programs that accomplish something usefulthat accomplish something usefulHow do objects fit in?How do objects fit in?EncapsulationEncapsulationEfficiencyEfficiencyWe can also make our own types of objects…We can also make our own types of objects…Example ProgramExample Programpublic class Examplepublic class Example{{public static void main(String args[])public static void main(String args[]){{Path p = new Path(3,4);Path p = new Path(3,4);System.out.println(“The distance is ”System.out.println(“The distance is ” + p.calcDistance()+ p.calcDistance() + “ miles and the”+ “ miles and the” + “ angle is ”+ “ angle is ” + p.calcAngle());+ p.calcAngle());}}}}Example ProgramExample ProgramThis is great, except… there is no This is great, except… there is no PathPath class in the Java APIclass in the Java APIThe program would be easy if we had that The program would be easy if we had that type of object…type of object…So we’ll just write a So we’ll just write a PathPath class ourselves class ourselvesBlack BoxesBlack BoxesWhat is a black box?What is a black box?Why do we call it a black box?Why do we call it a black box?What are some instances of a black box?What are some instances of a black box?Black BoxesBlack BoxesA black box works magically – we press a A black box works magically – we press a button and something happensbutton and something happensWe don’t know how it actually works We don’t know how it actually works EncapsulationEncapsulation: the hiding of unimportant : the hiding of unimportant details detailsBlack BoxesBlack BoxesNot everything is hidden in a black boxNot everything is hidden in a black boxWe can input some numbersWe can input some numbersThere is some sort of outputThere is some sort of outputThere are ways to interact with it, but it’s There are ways to interact with it, but it’s all done on the outsideall done on the outsideInteraction possibilities are well definedInteraction possibilities are well definedEncapsulationEncapsulationWhat is the right What is the right conceptconcept for each for each particular black box? particular black box? Concepts are discovered through Concepts are discovered through abstraction abstraction AbstractionAbstraction: taking away inessential : taking away inessential features, until only the essence of the features, until only the essence of the concept remains concept remains How much does a user really need to know?How much does a user really need to know?Example: CarsExample: CarsBlack boxes in a car: Black boxes in a car: transmission, transmission, electronic control electronic control module, etc module, etcExample: CarExample: CarUsers of a car do not need to understand how Users of a car do not need to understand how black boxes workblack boxes workInteraction of a black box with outside world is Interaction of a black box with outside world is well-defined well-defined Drivers interact with car using pedals, buttons, etc. Drivers interact with car using pedals, buttons, etc. Mechanic can test that engine control module Mechanic can test that engine control module sends the right firing signals to the spark plugs sends the right firing signals to the spark plugs For engine control module manufacturers, For engine control module manufacturers, transistors and capacitors are black boxes transistors and capacitors are black boxes magically produced by an electronics component magically produced by an electronics component manufacturer manufacturerExample: CarExample: CarEncapsulation leads to efficiency: Encapsulation leads to efficiency: Mechanic deals only with car components Mechanic deals only with car components (e.g. electronic control module), not with (e.g. electronic control module), not with sensors and transistors sensors and transistors Driver worries only about interaction with car Driver worries only about interaction with car (e.g. putting gas in the tank), not about motor (e.g. putting gas in the tank), not about motor or electronic control module or electronic control moduleExample: DoorbellExample: DoorbellWhat can we do to a doorbell?What can we do to a doorbell?What output do we get from a doorbellWhat output do we get from a doorbellDo we actually know how a doorbell Do we actually know how a doorbell works?works?Do we need to?Do we need to?EncapsulationEncapsulationIn In Object-oriented programmingObject-oriented programming (OOP) (OOP) the black boxes we use in our programs the black boxes we use in our programs are objectsare objectsString String is a black boxis a black boxRectangleRectangle is a black box is a black boxSoftware DesignSoftware
View Full Document