


Preview text:
lOMoARcPSD| 61814229
1. What is the primary benefit of inheritance in C#?
The primary benefit of inheritance in C# is code reuse. Inheritance allows a class (called
a derived or child class) to inherit fields, properties, methods, and behaviors from
another class (called a base or parent class). This promotes reusability and reduces
redundancy, as common functionality can be written once in the base class and shared
across multiple derived classes. Key benefits include:
Code Reusability: Shared functionality is written once and reused across multiple classes.
Maintainability: Updates or bug fixes made to the base class automatically propagate to derived classes.
Extensibility: Derived classes can extend or customize the behavior of the base class by overriding its methods.
Polymorphism: Enables dynamic method binding, allowing different derived classes to
have different implementations of the same method, while being used interchangeably.
2. When should you consider using inheritance?
In C#, inheritance should be used when you have a clear "is-a" relationship between
classes, meaning the derived class is a specialized version of the base class. It is ideal
when multiple classes share common properties and behaviors, allowing you to avoid
code duplication. Use inheritance to promote code reusability, maintainability, and
polymorphism when you want to extend or override base class functionality. However,
avoid inheritance if the relationship between classes is not hierarchical or if it leads to
tightly coupled code. In such cases, composition might be a better design choice.
3. How do you declare a derived class in C#?
In C#, a derived class is declared using a colon (:) to indicate that it is inheriting from a base class. lOMoARcPSD| 61814229
4. What is the difference between public, protected, and private inheritance public,
protected, and private inheritance are not defined as in C++ (where the inheritance
type affects the visibility of inherited members). Instead, C# controls member access
through access modifiers: public, protected, private, and others. However, these
modifiers play a crucial role in controlling access to members of a base class when they
are inherited. Public Inheritance (Default in C#) •
When a derived class inherits from a base class with public members, those members
remain public in the derived class and are accessible by any external code.
Protected Inheritance (via protected members) •
Protected members of a base class are accessible within the derived class but not from
outside the class hierarchy. This helps to limit access while still allowing the derived
class to use and extend base class functionality. lOMoARcPSD| 61814229
Private Inheritance (via private members) •
Private members are not accessible in the derived class. Only public and protected
members are inherited, while private members remain completely encapsulated in the
base class. 5. What is the purpose of the base keyword in C#?
The base keyword in C# is used to access members of a base class from within a derived
class. It allows a derived class to:
Call base class constructors: The base keyword is commonly used to invoke a specific
constructor of the base class when creating an instance of the derived class. This
ensures proper initialization of the base class before the derived class adds its own behavior.
Access base class methods and properties: The base keyword can call methods or access
properties of the base class when they have been overridden in the derived class,
allowing the derived class to extend or modify the base functionality.