Inheritance and Encapsulation

To make the snowball fight and other programs in the book, you must learn a few tricks about building classes. To illustrate these new concepts, I’ll build a few more critters like the ones in Chapter 4, “Objects and Encapsulation: The Critter Program.” The basic critter is interesting, but what if you want to make new kinds of critters that share the same behavior but exhibit different characteristics? For example, you might want to make a new kind of critter that is grumpy.

When programmers began using objects, they quickly realized the importance of being able to make changes to objects. Computer scientists (who love to obfuscate simple ideas by using techie terms) think that objects should support inheritance and polymorphism. These fancy words describe some simple but important ideas. Inheritance works much like genetics. Objects can have children and grandchildren, and an object’s descendants will inherit traits from the parents and grandparents. Polymorphism means that an object can have the same kind of behavior as its relatives but can implement that behavior differently. Don’t worry if these explanations leave you cold for now. I just want you to have a bird’s-eye view of these concepts before digging into specific examples later in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.206.225