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

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.

主站蜘蛛池模板: 齐河县| 黑河市| 河北省| 大足县| 东至县| 南召县| 班戈县| 都昌县| 旌德县| 无为县| 界首市| 太仆寺旗| 三河市| 长丰县| 太和县| 永定县| 翼城县| 陇川县| 铁岭市| 宁远县| 淳安县| 乌拉特后旗| 昌宁县| 景洪市| 桓台县| 新郑市| 麦盖提县| 任丘市| 安达市| 遵化市| 油尖旺区| 桃江县| 肇庆市| 沂源县| 石狮市| 海淀区| 呈贡县| 鄂州市| 志丹县| 耿马| 平安县|