This essay continues our discussion of object-oriented programming (OOP) by introducing one of its primary features -inheritance, which is a form of software reuse in which a new class is created by absorbing an existing class`s members and embellishing them with new or modified capabilities. With inheritance, programmers save time during program development by reusing proven and debugged high-quality sofware. This is also increases the likehood that system will be implemented effectively.
When creating a class, rather than declaring completely new members, you can designate that the new class should inherit the members of an existing class. The existing class is called the superclass, and the new class is the subclass. ( The C++ programming language refers to the superclass as the base class and the subclass as the derived class. Each subclass can become the superclass for future subclasses.
A subclass normally adds its own fields and methods. Therefore, a subclass is more specific than its superclass and represent a more specialized group of objects. Typically, the subclass exhibits the behaviors of its superclass and additionaly behaviors that are specific to the subclass. This is why inheritance is sometimes referred to as specialization.
The direct superclass is the superclass from which the subclass explicitly inherits. An indirect superclass is any class above the direct superclass in the class hierarchy, which defines the inheritance reletionships between classes. In java, the class hierarchy begins with class Object (in package java.lang), which every class in java directly or indirectly extends (or "inherits from"). In the case of single inheritance, a class is derived from one direct superclass. Java, unlike C++, does not support multiple inheritance (which occurs when a class is derived from more than one direct superclass).
We distinguish between the is-a relationship and the has-a relationship. Is-a represent inheritance. In an is-a relationship, an object of a subclass can also be treated as an object of its superclass.For example a car is a vehicle. By constrant, has-a represent composition. In a has-a relationship, an object contains as members references to other objects. For example, a car has steering wheel.
Hiç yorum yok:
Yorum Gönder