DOC PREVIEW
Berkeley COMPSCI 61B - Inheritance

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS61BL - InheritanceImportant DatesSlide 3Estimating TimeSlide 5Test Driven DevelopmentWhy do Test Driven Development?InheritanceSlide 9Slide 10Slide 11Slide 12Slide 13Subclasses (Children) CanInheritance PuzzlesSlide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22CS61BL - InheritanceCS61BL Summer 20096/30/2009Important Dates•Project 1 released–Tuesday 6/30/2009 – 10pm •Project 1 - Check-off –Thursday 7/02/2009 ready BEFORE lab•Review Session–Sunday 7/05/2009 – 306 Soda 1-4pm •Midterm 1–Tuesday 7/07/2009 – 10 Evans 5-6pm•Project 2 released–Thursday 7/09/2009•Project 1 due–Monday 7/13/2009 – 10pmEstimating Time•Most people grossly underestimate how long it takes to program (not thinking about time to debug and test)•You appear incompetent at programming (not true) if you can’t estimate how long something will take.•This is one of the hardest things to learn!!! Start to practice now! For each task estimate how long it will take and keep track of how wrong you wereTest Driven Developmentstatic void beAGoodProgrammer(){while (true) // for each method{writeTest();writeCode();writeAFewMoreTests();writeCode();}}Why do Test Driven Development?•It is a way to brainstorm what your method is going to do.•It is hard to write test cases•Write your test cases before you’re biased by how your code is writtenInheritanceA Dog is an AnimalDog extends AnimalA Cat is an AnimalCat extends AnimalA Dalmatian is an Dog Dalmatian extends Dog A Poodle is an DogPoodle extends DogAnimalage 0Dogagebites 0trueAnimalage 0Animal myAnimal= new Animal();myAnimal.becomeOlder();myAnimal.eat();Dog myDog = new Dog();myDog.eat();myDog.bark(); // ↓ does this work? ___myDog.becomeOlder();Animal myAnimal= new Animal();myAnimal.bark();↑Does this work???? _________Subclasses (Children) Can•Declare new instance/class variables•Declare new methods•Override old implementations with new implementations•Access instance/class variables of the superclass (parent) *•Access methods of the superclass* * only public, protected or default onesInheritance PuzzlesDog d = new Animal();Does this work? _______No it doesn’t compile, If I want a Dog, an Animal will be NOT be acceptable because I might want to call a method that only a Dog can do!Inheritance PuzzlesAnimal a = new Dog();Does this work? _______YES it compiles and runs - If I want an Animal , a Dog will be acceptable because everything an Animal can do a Dog can do too! Animal a = (Animal) new Dog();Inheritance PuzzlesAnimal a = new Dog();Dog d = a;Does this work? _______No it doesn’t compile. The compiler doesn’t know it is really a Dog!but we can PROMISE the compiler that it is with a cast Dog d = (Dog) a;Inheritance PuzzlesAnimal a = new Animal();Dog d = (Dog) a;Does this work? _______YES and NO - The compiler trusts your PROMISE (cast) but then if you run your program it has a RUN TIME ERROR because a is not a DogInheritance PuzzlesAnimal a = new Cat();Dog d = (Dog) a;Does this work? _______YES and NO. It compiles, but then when we try to do the cast we get a RUN TIME ERROR, before we even try to call any dog methods on a.Inheritance PuzzlesAnimal a = new Dog();a.bark();Does this work? _______NO it does not compile - The compiler thinks it is an Animal, and an Animal does not have a method barkDog d = (Dog) a; d.bark();((Dog) a).bark();Inheritance PuzzlesAnimal a = new Dog();a.eat();Does this work? _______YES- The compiler is happy - an Animal has an eat() method.Which method is called the Animal eat() or the Dog eat() ?Inheritance PuzzlesAnimal a = new Dog();a.eat();Which method is called the Animal eat() or the Dog eat() ? Dogagebites 0truemainAnimal


View Full Document

Berkeley COMPSCI 61B - Inheritance

Documents in this Course
Lab

Lab

4 pages

Matrix

Matrix

3 pages

Numbers

Numbers

14 pages

Lectures

Lectures

12 pages

Project 1

Project 1

24 pages

Exam

Exam

8 pages

Load more
Download Inheritance
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Inheritance and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Inheritance 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?