Section A — 1 Mark Questions
1. Define inheritance. [1 Mark]
Answer: Inheritance is the mechanism in object-oriented programming that allows a new class to acquire the properties and behaviors of an existing class.
2. What is a derived class? [1 Mark]
Answer: A derived class is a new class created from an existing class, also known as a subclass or child class.
3. What is the name of the existing class from which a new class is derived? [1 Mark]
Answer: It is called the base class or parent class.
4. What is single inheritance? [1 Mark]
Answer: Single inheritance is a type where a single derived class inherits from a single base class.
5. Which access specifier prevents a member of a base class from being directly accessed by a derived class? [1 Mark]
Answer: The private access specifier.
6. How can a private member be made accessible to derived classes while remaining hidden from the outside world? [1 Mark]
Answer: By using the protected access specifier.
7. What is multilevel inheritance? [1 Mark]
Answer: Multilevel inheritance involves a derived class inheriting from a base class, which is itself a derived class of another base class.
8. Define hierarchical inheritance. [1 Mark]
Answer: Hierarchical inheritance occurs when multiple derived classes inherit from a single base class.
9. What is multiple inheritance? [1 Mark]
Answer: Multiple inheritance is a feature where a class can inherit features from more than one parent class.
10. What is hybrid inheritance? [1 Mark]
Answer: Hybrid inheritance is a combination of two or more types of inheritance within a single program.
11. Does a derived class automatically inherit private members of the base class? [1 Mark]
Answer: No, private members of a base class are not directly accessible or inherited by the derived class.
12. What is the main benefit of inheritance? [1 Mark]
Answer: The main benefit is code reusability.
13. Which keyword is commonly used to indicate a base class in a derivation? [1 Mark]
Answer: The colon operator (:) is typically used in the derived class declaration.
14. In which type of inheritance does a class hierarchy form a tree structure? [1 Mark]
Answer: Hierarchical inheritance.
15. True or False: Protected members are accessible within the derived class. [1 Mark]
Answer: True.
16. What does inheritance promote in software development? [1 Mark]
Answer: It promotes the principle of reusability and extensibility.
17. Can a derived class have its own member functions? [1 Mark]
Answer: Yes, a derived class can define its own unique data and functions.
18. What is the base class in a chain of multilevel inheritance? [1 Mark]
Answer: The topmost class from which all other classes are derived.
19. Does inheritance support the concept of 'is-a' relationship? [1 Mark]
Answer: Yes, inheritance establishes an 'is-a' relationship between classes.
20. What is the result of using protected instead of private members in a base class? [1 Mark]
Answer: It allows derived classes to access the members while keeping them inaccessible to the general public.
Section B — 2/3 Mark Questions
1. Differentiate between a base class and a derived class. [2 Marks]
Answer: A base class is the original class that provides properties and methods, while a derived class is the new class that inherits these features from the base class.
2. Explain the concept of multilevel inheritance with a short example. [3 Marks]
Answer: Multilevel inheritance represents a parent-child-grandchild relationship. For example, class C inherits from class B, and class B inherits from class A. Thus, C possesses properties of both B and A.
3. Why is the protected access specifier important in inheritance? [2 Marks]
Answer: It is important because it allows derived classes to access base class members directly while still preventing external classes from accessing those members.
4. What is hierarchical inheritance? Give a brief description. [2 Marks]
Answer: Hierarchical inheritance involves one base class serving as the foundation for multiple child classes. This is useful for grouping related classes under a common parent.
5. Explain why private members are not inherited by a derived class. [2 Marks]
Answer: Private members are designed for encapsulation within the base class only. Restricting their access ensures that the internal implementation details of the base class remain hidden from derived classes.
6. Describe multiple inheritance briefly. [3 Marks]
Answer: Multiple inheritance allows a class to inherit from two or more base classes simultaneously. This enables the derived class to combine features and behaviors from different parent sources.
7. How does hybrid inheritance differ from other types? [3 Marks]
Answer: Hybrid inheritance is not a single structure; it is a mixture of multiple types of inheritance (e.g., combining single and multiple inheritance). It is used to model complex relationships that cannot be represented by a single inheritance type.
8. What is the 'is-a' relationship in the context of inheritance? [2 Marks]
Answer: The 'is-a' relationship means that an object of the derived class is essentially a specific version of the base class (e.g., a 'Car' is a 'Vehicle').
9. List the primary advantages of using inheritance. [3 Marks]
Answer: The primary advantages are code reusability, hierarchical organization of data, and easier maintenance of large software systems by allowing shared functionality to be defined once.
10. What happens when a class is derived without specifying an access specifier (e.g., public/private)? [2 Marks]
Answer: By default, if the access specifier is omitted, the compiler usually treats the inheritance as private, meaning all public and protected members of the base class become private members of the derived class.
Section C — 5 Mark Questions
1. Explain the concept of Single Inheritance and its implementation process in detail. [5 Marks]
Introduction
Single inheritance is the simplest form of inheritance in object-oriented programming where a derived class inherits properties and behaviors from exactly one base class.
Explanation
In this model, the base class acts as the parent, providing a foundation of attributes and functions, while the derived class expands upon this base by adding its own specific functionality. This establishes a clean 'is-a' relationship between the classes.
Implementation Steps
- Define the base class containing common data and methods.
- Define the derived class, followed by a colon and the base class name.
- Specify the access mode (e.g., public) to determine how members of the base class are accessible within the derived class.
- The derived class can then access public and protected members of the base class as if they were its own.
2. Discuss the differences between Multilevel and Hierarchical inheritance with diagrams or logical descriptions. [5 Marks]
Multilevel Inheritance
Multilevel inheritance involves a linear chain of classes. For example, Class A (Grandparent) → Class B (Parent) → Class C (Child). Each level adds its own functionality to the features inherited from the previous level.
Hierarchical Inheritance
Hierarchical inheritance involves a single base class with multiple branches of derived classes. For example, Class A (Parent) is inherited by both Class B and Class C. Both B and C share the traits of A, but they are siblings to each other and independent of one another.
Key Differences
- Multilevel inheritance represents a hierarchy of depth, while hierarchical inheritance represents a hierarchy of breadth.
- In multilevel, the bottom-most class inherits traits from all its ancestors in the chain.
- In hierarchical, siblings do not share traits with each other; they only inherit from the common base class.
3. Elaborate on the role of access specifiers (public, private, protected) in controlling member accessibility within derived classes. [5 Marks]
Role of Access Specifiers
Access specifiers determine how members are exposed or hidden when inheritance occurs. They define the 'visibility' of base class members inside the derived class.
Detailed Breakdown
- Public Inheritance: Public members of the base class become public in the derived class, and protected members remain protected.
- Protected Inheritance: Public and protected members of the base class become protected in the derived class.
- Private Inheritance: Both public and protected members of the base class become private in the derived class.
- Private Members: Regardless of the inheritance mode, private members of the base class remain inaccessible directly to the derived class.
By using the protected access specifier, programmers can allow derived classes to utilize base class features without exposing those features to the outside world, maintaining strong encapsulation.
4. Describe the concept of Multiple Inheritance and the challenges associated with it. [5 Marks]
Definition
Multiple inheritance is a feature where a class can inherit from more than one base class simultaneously. This allows the derived class to combine the attributes and behaviors of multiple parent classes into one single unit.
Advantages
- Allows for the modular creation of classes.
- Enables combining functionality from entirely different domains.
Challenges
- Ambiguity: If both base classes have a function with the same name, the derived class may face ambiguity when trying to call that function.
- Complexity: The class hierarchy can become difficult to manage and trace when a class has many ancestors.
- The Diamond Problem: A classic issue where a class inherits from two classes that both inherit from a single common ancestor, potentially leading to duplication of the ancestor's data members.
5. Explain Hybrid Inheritance and how it integrates multiple types of inheritance. [5 Marks]
Concept
Hybrid inheritance is a design pattern that combines two or more different types of inheritance to meet specific software architectural needs. It is rarely a simple structure and is often used to model complex real-world requirements.
Integration
A typical example of hybrid inheritance would be combining hierarchical inheritance with multiple inheritance. For instance, if two separate base classes are inherited by a single derived class (Multiple Inheritance), and those base classes themselves are inherited from a common base (Hierarchical Inheritance), the resulting structure is hybrid.
Application
It is used when a project requires a sophisticated mix of modular components and specialized classes. By nesting different inheritance types, developers can build a flexible system where specific functionalities are passed down through various channels, provided that the potential for complexity and ambiguity is carefully managed through good design and clear documentation.