Questions and Answers
A class inherited from two or more levels up in the hierarchy is known as a?
indirect base class
Inheritance is represented by a(n) ________ relationship.
is-a
A _________ derived class cannot access the members of its base class.
private
Which of the following statements is false?
Signup and view all the answers
A derived class can effect state changes in base class private members only through public, protected, internal methods provided in the base class and inherited into the derived class.
Signup and view all the answers
Inheritance is the process of building a class with object references of other classes.
Signup and view all the answers
A class that inherits from another class is referred to as the derived class.
Signup and view all the answers
Every object of a base class is an object of that class's derived classes.
Signup and view all the answers
A derived class is often larger and more general than its base class.
Signup and view all the answers
Multiple inheritance, widely used in C#, is the process of inheriting from more than one class.
Signup and view all the answers
The process of abstraction allows you to focus on the commonalities among objects in the system.
Signup and view all the answers
Which of the following pairs demonstrates the is-a relationship between the first and the second terms?
Signup and view all the answers
Which of the following pairs demonstrates the has-a relationship between the first and the second terms?
Signup and view all the answers
Identify which of the following examples could be considered a base class for the Computer class?
Signup and view all the answers
Which of the following is not a base/derived class relationship?
Signup and view all the answers
An advantage of inheritance is that:
Signup and view all the answers
A base class may have only one derived class.
Signup and view all the answers
Members of a base class that are private are not inherited by derived classes.
Signup and view all the answers
Constructors are not inherited.
Signup and view all the answers
All classes in C# have object as either a direct or indirect base class.
Signup and view all the answers
An object of one class cannot be an object of another class.
Signup and view all the answers
Derived classes provide the functionality and features inherited by base classes.
Signup and view all the answers
A base class's constructors are inherited into its derived classes.
Signup and view all the answers
When a derived-class member overrides a base-class member, the base-class member can be accessed from the derived class by using the keyword ____________.
Signup and view all the answers
Base class methods with this level of access cannot be called from derived classes.
Signup and view all the answers
The protected members of a class may be accessed in their base class or any classes derived from that base class.
Signup and view all the answers
A method must be declared __________ for it to be overridden by derived classes.
Signup and view all the answers
Which of the following classes is the root of the class hierarchy?
Signup and view all the answers
Every class in C#, except __________, extends an existing class.
Signup and view all the answers
Overriding a method differs from overloading a constructor because:
Signup and view all the answers
To avoid duplicating code (and possibly errors), use _______, rather than _______.
Signup and view all the answers
Consider the classes below, declared in the same file: class A { int a; public A() { a = 7; }} class B : A { int b; public B() { b = 8; }} A reference to class A can be treated as a reference to class B.
Signup and view all the answers
Which base class members are inherited by all derived classes of that base class?
Signup and view all the answers
Which statement is true when a base class has protected instance variables?
Signup and view all the answers
Private fields of a base class can be accessed in a derived class
Signup and view all the answers
The first task of any derived-class constructor is to call its base-class constructor.
Signup and view all the answers
A 'copy-and-paste' approach is a simple and efficient way from a software-engineering perspective of providing functionality that exists in other classes.
Signup and view all the answers
Using protected instance variables can cause derived-class methods to be written to depend on base-class implementation.
Signup and view all the answers
When a derived class constructor calls its base class constructor, what happens if the base class's constructor does not assign a value to an instance variable?
Signup and view all the answers
How can a derived class call a base class constructor?
Signup and view all the answers
When a program creates a derived-class object, the object constructor is the last constructor called and the first whose body finishes executing.
Signup and view all the answers
If a base class constructor is overridden, the original constructor can no longer be called explicitly by the derived class.
Signup and view all the answers
When a base class method is overridden in a derived class, it is common to have the derived class version call the base class version and do some additional work.
Signup and view all the answers
Which of the following statements is (are) true?
Signup and view all the answers
Inheritance preserves the integrity of a base class.
Signup and view all the answers
A key to improving the software development process is encouraging software reuse.
Signup and view all the answers
To enhance performance and reduce errors, it's a good idea to make derived classes larger than they need to be.
Signup and view all the answers
A base class is often designed by factoring out commonalities among a set of classes.
Signup and view all the answers
When creating derived classes, you must use discretion in choosing the proper base class. Ideally, the base class will not contain excessive capabilities or information.
Signup and view all the answers
The default implementation of method ToString of object returns a string representing ________.
Signup and view all the answers
The default Equals implementation determines:
Signup and view all the answers
Method Equals will accurately compare any two objects.
Signup and view all the answers
Every object in C# has at least seven methods: Equals, Finalize, GetHashCode, GetType, MemberwiseClone, ReferenceEquals, and ToString.
Signup and view all the answers
Study Notes
Inheritance Concept
- An indirect base class is a class inherited from two or more levels up in the hierarchy.
- Inheritance is represented as an "is-a" relationship, indicating a subclass's relationship to a superclass.
- A private derived class cannot access the members of its base class directly.
True/False Statements on Inheritance
- A base class object is not a derived class object; they represent different levels of hierarchy.
- A derived class can only change the state of base class private members via inherited public, protected, or internal methods.
- Inheritance involves extending class capabilities, whereas composition involves building a class with other object references.
- A derived class is typically not larger and more general than its base class; it is more specialized.
- Multiple inheritance, where a class inherits from more than one class, is not supported in C#.
- Constructors are specific to their class and are not inherited into derived classes.
Class Relationships
- An object of a base class cannot be an object of its derived classes due to the specificity of derived classes.
- Protected members of a base class can be accessed by derived classes, maintaining an internal hierarchy of access.
Method Overriding
- When overriding a base class method, use the "base" keyword to access the original member.
- A method must be declared as virtual to allow derived classes to override it.
- Overridden methods must maintain the same signature as the methods in the base class.
Access and Modifications
- Private fields in a base class can be accessed by derived classes only through public or protected methods.
- The first task of any derived-class constructor is to invoke its base class constructor to ensure proper initialization.
Software Design Principles
- Inheritance allows for the treatment of derived class objects as base class objects, promoting code reuse and reducing redundancy.
- A "copy-and-paste" approach is discouraged; inheritance is preferred to avoid code duplication.
- Classes should be minimal, containing only necessary capabilities, enhancing performance and reducing errors.
Object Class Hierarchy
- All classes in C# derive from the base class 'object', establishing a universal structure.
- The default implementation of the ToString method provides a string representing the namespace and the class name of the object.
Methods and Equality
- Equals determines if two references point to the same object in memory; it must be overridden to compare object contents accurately.
- Every C# object has at least seven methods, including Equals, GetType, and ToString, ensuring basic functionality.
Final Observations
- The integrity and functionality of a base class are preserved through inheritance, allowing for safe modifications in derived classes.
- Careful selection of the base class is necessary to avoid excessive capabilities, leading to cleaner and more maintainable code structures.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your knowledge on Chapter 11 of the C# course with these flashcards. This quiz covers key concepts related to inheritance, base classes, and class hierarchy. Perfect for final exam preparation!