From the course: Computer Science Principles: Programming

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Extend classes

Extend classes

From the course: Computer Science Principles: Programming

Start my 1-month free trial

Extend classes

- When you create a class, you aren't limited to just that class. Using that basic blueprint, you can improve that blueprint and add more capabilities to it. Take, for example, a class that defines an animal. An animal has weight, color, or hair color, a gender and an age. An animal needs to do some basic things. It needs to eat, sleep and move. But different animals can extend that definition. A cat can run, a fish can swim and a bird can fly. By using a class for an animal, we can extend it and add new capabilities to a class, while at the same time, bring along everything that is part of another class. So if we have the animal class, I can create a cat class that extends the animal class. In the cat class, I can create a run method. And everything else that is part of an animal is part of the cat. The same thing works for a fish or a bird. In each one I add what is unique to that class. And I extend the animal class. As a result, you end up with a hierarchy. You have a class that…

Contents