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

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.

主站蜘蛛池模板: 丰城市| 齐河县| 禹州市| 宁德市| 隆安县| 石屏县| 灵丘县| 四子王旗| 安庆市| 行唐县| 五原县| 虎林市| 佛学| 南宫市| 永平县| 洛浦县| 平阴县| 栖霞市| 东山县| 华阴市| 喀什市| 龙口市| 巨野县| 鄢陵县| 黎川县| 滨海县| 平邑县| 左贡县| 龙里县| 定陶县| 当阳市| 新泰市| 义马市| 洱源县| 阿巴嘎旗| 秦皇岛市| 绥滨县| 延庆县| 淅川县| 阳城县| 巴彦淖尔市|