Base & Derived Classes
When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form −
class derived-class: access-specifier base-class
Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.
A base class is a class, in an object-oriented programming language, from which other classes are derived. It facilitates the creation of other classes that can reuse the code implicitly inherited from the base class (except constructors and destructors). A programmer can extend base class functionality by adding or overriding members relevant to the derived class.
A base class may also be called parent class or superclass.
A base class may also be called parent class or superclass.
A class derived from a base class inherits both data and behavior. For example, "vehicle" can be a base class from which "car" and "bus" are derived. Cars and buses are both vehicles, but each represents its own specialization of the vehicle base class.
A base class has the following properties:
A base class has the following properties:
- Base classes are automatically instantiated before derived classes.
- The derived class can communicate to the base class during instantiation by calling the base class constructor with a matching parameter list.
- Base class members can be accessed from the derived class through an explicit cast.
- If abstract methods are defined in a base class, then this class is considered to be an abstract class and the non-abstract derived class should override these methods.
- Abstract base classes are created using the "abstract" keyword in its declaration and are used to prevent direct initiation using the "new" keyword.
Derived Class
A derived class is a class created or derived from another existing class. The existing class from which the derived class is created through the process of inheritance is known as a base class or superclass.
Derived classes are used for augmenting the functionality of base class by adding or modifying the properties and methods to suit the requirements of the specialization necessary for the derived class. This allows for defining virtual methods that form the means to implement polymorphism, which allows a group of objects to work in a uniform manner. Thus, the inherent advantages of inheritance and polymorphism like code reuse, faster development, easy maintenance, etc., are realized.
A derived class is also known as subclass or child class.
Derived classes are used for augmenting the functionality of base class by adding or modifying the properties and methods to suit the requirements of the specialization necessary for the derived class. This allows for defining virtual methods that form the means to implement polymorphism, which allows a group of objects to work in a uniform manner. Thus, the inherent advantages of inheritance and polymorphism like code reuse, faster development, easy maintenance, etc., are realized.
A derived class is also known as subclass or child class.
The hierarchical relationship between a derived class and base class is known as an “is a” relationship. For example, consider a base class, LivingBeing, which is used to create two derived classes, Plant and Animal. The plant is a LivingBeing and Animal is a LivingBeing. Both have a few common features but each type can have features that are unique to its specialization and are different from the features of the base class.
While inheriting from a base class, the derived class implicitly inherits all the members (except constructors and destructors), which it reuses as it extends and modifies the behavior of the base class. The derived class overrides the properties and methods of the base class so that it represents the specialized version of the base class.
While inheriting from a base class, the derived class implicitly inherits all the members (except constructors and destructors), which it reuses as it extends and modifies the behavior of the base class. The derived class overrides the properties and methods of the base class so that it represents the specialized version of the base class.
Comments
Post a Comment