官术网_书友最值得收藏!

Creating and implementing an abstract class

Many developers have heard about abstract classes, but their implementation is a mystery. How can you as a developer identify an abstract class and decide when to use one? The definition is quite a simple one actually. Once you understand this fundamental definition of an abstract class, when and why to use one becomes obvious.

Imagine for a moment that you are developing an application that manages the animals in a cat sanctuary. The cat sanctuary rehabilitates lions, tigers, jaguars, leopards, cheetahs, pumas, and even domestic cats. The common noun that describes all these animals is the word cat. You can, therefore, safely assume that the abstraction of all these animals is a cat, and thus, this word identifies our abstract class. You would then create an abstract class called Cat.

However, you need to keep in mind that you will never ever create an instance of the abstract class Cat. All the classes that inherit from the abstract class also share some functionality. This means that you will create a Lion class and a Tiger class that inherit from the abstract class Cat. In other words, the inherited classes are a kind of cat. Both classes share functionality in the form of Sleep(), Eat(), Hunt(), and various other methods. In this way, we can ensure that inherited classes all contain this common functionality.

主站蜘蛛池模板: 重庆市| 舞阳县| 青铜峡市| 汽车| 沂源县| 东丰县| 通榆县| 丰县| 彭水| 龙门县| 沿河| 四子王旗| 双桥区| 额济纳旗| 普宁市| 克东县| 晴隆县| 从化市| 茂名市| 仁怀市| 海宁市| 三门峡市| 紫云| 镇远县| 彭山县| 永仁县| 宜君县| 大姚县| 台江县| 黄石市| 隆林| 赫章县| 思茅市| 丰城市| 徐水县| 秭归县| 大英县| 玛沁县| 凌海市| 临安市| 清河县|