Chapter 9 Object Orientation: classes based inheritance

Cards (49)

  • What is class-based inheritance in object-oriented programming?
    It is a feature that allows new classes to reuse, extend, and modify behavior defined in other classes.
  • What is the difference between a base class and a derived class?
    The base class is the class whose members are inherited, while the derived class is the class that inherits those members.
  • What does it mean that inheritance is transitive?
    If ClassC is derived from ClassB, and ClassB is derived from ClassA, then ClassC inherits members from both ClassB and ClassA.
  • What are the alternative terms for base class and derived class?
    Base class is also called the parent class, and derived class is called the child class.
  • What happens if class B inherits from class A?
    Every instance of class B is also an instance of class A.
  • Why is it beneficial to have a Weapon class inherit from an Item class?
    It allows the Weapon class to reuse the logic defined in the Item class, promoting code efficiency.
  • What is the purpose of the base keyword in a derived class constructor?
    The base keyword is used to call the constructor of the base class from the derived class constructor.
  • What members are not inherited by a derived class?
    Constructors and finalizers are not inherited by a derived class.
  • Can a derived class remove inherited members?
    No, a derived class cannot remove inherited members or change their access modifier to a more restrictive one.
  • What happens if no base class constructor is explicitly called in a derived class?
    The base class constructor without arguments is called implicitly.
  • What does the protected keyword do in C#?
    The protected keyword makes a member accessible to the defining class and its derived classes.
  • What is the purpose of the override keyword in C#?
    The override keyword allows a derived class to replace the base class logic with new logic for an inherited method.
  • What is System.Object in C#?
    System.Object is the root of the type system in C#, and every class inherits directly or indirectly from it.
  • What happens when a class does not explicitly specify a base class?
    The class implicitly inherits from System.Object.
  • Why is the ToString method important in System.Object?
    It provides a text representation of an object, which is useful in many situations.
  • What conditions must be met to override a method in a child class?
    The parent class must define the method as virtual, and the child class must use the keyword override.
  • What are the key concepts of class-based inheritance?
    • Inheritance allows for code reuse and specialization.
    • Base class members can be inherited by derived classes.
    • Constructors are not inherited, but can be called using the base keyword.
    • The protected keyword allows access to derived classes.
    • The override keyword enables method replacement in derived classes.
  • What are the implications of using inheritance in object-oriented programming?
    • Promotes code reuse and reduces redundancy.
    • Allows for polymorphism and dynamic method resolution.
    • Can lead to complex hierarchies if not managed properly.
    • May introduce tight coupling between classes.
  • What are the differences between hiding and overriding methods?
    • Hiding: Uses the keyword new; does not require the base method to be virtual.
    • Overriding: Uses the keyword override; requires the base method to be virtual.
    • Hiding creates a new method, while overriding replaces the base method logic.
  • What is the role of the base keyword in derived classes?
    • Calls the constructor of the base class.
    • Allows access to base class methods and properties.
    • Facilitates method overriding by referencing the base class implementation.
  • What are the consequences of not having a parameterless constructor in the base class?
    • Derived classes cannot be instantiated without explicitly calling a base class constructor.
    • If no accessible parameterless constructor exists, it generates a compiler error.
    • The default constructor is added by the compiler only if no constructors are defined in the class.
  • What class does Student inherit from?
    Person
  • What is the purpose of the private set in the FirstName property of the Person class?
    It restricts the modification of FirstName to within the class only
  • What does the constructor of the Person class initialize?
    FirstName and LastName
  • What does the ToString method in the Person class return?
    LastName, FirstName ( Person )
  • What is the return type of the ToText1 method in the Person class?
    string
  • How does the Student class's constructor differ from the Person class's constructor?
    The Student constructor also initializes MajorName
  • What does the new keyword indicate in the ToString method of the Student class?
    It hides the inherited ToString method from the Person class
  • What happens when a Student object is accessed through a Person reference?
    It uses the ToString implementation from the Person class
  • What is the consequence of not using the override keyword in the Student class's ToString method?
    It hides the method instead of overriding it
  • What does the sealed keyword do when applied to a class?
    It prevents the class from being inherited
  • What does the abstract keyword indicate when applied to a method?
    It indicates that the method has no body and must be implemented in derived classes
  • Can an abstract class be instantiated directly?
    No, it cannot be instantiated directly
  • What is polymorphism in the context of object-oriented programming?
    It allows an object to be assigned to references of different types
  • What is the purpose of the is operator in type checking?
    It checks if a value can be assigned to a reference of a type
  • What happens if a child class does not implement all abstract methods?
    The child class must also be defined as abstract
  • How does the as operator differ from the is operator?
    The as operator attempts to cast and returns null if it fails
  • What is the purpose of the OfType method in LINQ?
    It filters a collection to include only elements of a specified type
  • What are the consequences of using composition vs inheritance in code reuse?
    • Both can be done right or wrong
    • Inheritance can lead to issues like the Circle-ellipse problem
    • Composition is often preferred for flexibility
    • Unity scripts inherit from MonoBehaviour, showing a mixed approach
  • What is the strategy pattern in programming?
    • A design pattern that allows selecting an algorithm at runtime
    • Involves defining a common interface for different algorithms
    • Enables different implementations to be used interchangeably
    • Reduces the need for conditional logic in code