C# Final Chapter 11 Flashcards
53 Questions
100 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

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?

<p>A base class object is a derived class object.</p> 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.

<p>True</p> Signup and view all the answers

Inheritance is the process of building a class with object references of other classes.

<p>False</p> Signup and view all the answers

A class that inherits from another class is referred to as the derived class.

<p>True</p> Signup and view all the answers

Every object of a base class is an object of that class's derived classes.

<p>False</p> Signup and view all the answers

A derived class is often larger and more general than its base class.

<p>False</p> Signup and view all the answers

Multiple inheritance, widely used in C#, is the process of inheriting from more than one class.

<p>False</p> Signup and view all the answers

The process of abstraction allows you to focus on the commonalities among objects in the system.

<p>True</p> Signup and view all the answers

Which of the following pairs demonstrates the is-a relationship between the first and the second terms?

<p>Baseball, Sport</p> Signup and view all the answers

Which of the following pairs demonstrates the has-a relationship between the first and the second terms?

<p>Employee, Company</p> Signup and view all the answers

Identify which of the following examples could be considered a base class for the Computer class?

<p>Machine</p> Signup and view all the answers

Which of the following is not a base/derived class relationship?

<p>Sailboat/Tugboat</p> Signup and view all the answers

An advantage of inheritance is that:

<p>Objects of a derived class can be treated like objects of their base class.</p> Signup and view all the answers

A base class may have only one derived class.

<p>False</p> Signup and view all the answers

Members of a base class that are private are not inherited by derived classes.

<p>False</p> Signup and view all the answers

Constructors are not inherited.

<p>True</p> Signup and view all the answers

All classes in C# have object as either a direct or indirect base class.

<p>True</p> Signup and view all the answers

An object of one class cannot be an object of another class.

<p>False</p> Signup and view all the answers

Derived classes provide the functionality and features inherited by base classes.

<p>False</p> Signup and view all the answers

A base class's constructors are inherited into its derived classes.

<p>False</p> 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 ____________.

<p>base</p> Signup and view all the answers

Base class methods with this level of access cannot be called from derived classes.

<p>private</p> 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.

<p>True</p> Signup and view all the answers

A method must be declared __________ for it to be overridden by derived classes.

<p>virtual</p> Signup and view all the answers

Which of the following classes is the root of the class hierarchy?

<p>object</p> Signup and view all the answers

Every class in C#, except __________, extends an existing class.

<p>object</p> Signup and view all the answers

Overriding a method differs from overloading a constructor because:

<p>Overridden methods have the same signature.</p> Signup and view all the answers

To avoid duplicating code (and possibly errors), use _______, rather than _______.

<p>inheritance, the 'copy-and-paste' approach</p> 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.

<p>false</p> Signup and view all the answers

Which base class members are inherited by all derived classes of that base class?

<p>protected instance variables and methods</p> Signup and view all the answers

Which statement is true when a base class has protected instance variables?

<p>All of the above.</p> Signup and view all the answers

Private fields of a base class can be accessed in a derived class

<p>by calling public or protected methods declared in the base class</p> Signup and view all the answers

The first task of any derived-class constructor is to call its base-class constructor.

<p>True</p> 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.

<p>False</p> Signup and view all the answers

Using protected instance variables can cause derived-class methods to be written to depend on base-class implementation.

<p>True</p> 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?

<p>the program compiles and runs correctly because the instance variables are initialized to their default values.</p> Signup and view all the answers

How can a derived class call a base class constructor?

<p>a and b</p> 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.

<p>True</p> 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.

<p>False</p> 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.

<p>True</p> Signup and view all the answers

Which of the following statements is (are) true?

<p>All of the above.</p> Signup and view all the answers

Inheritance preserves the integrity of a base class.

<p>True</p> Signup and view all the answers

A key to improving the software development process is encouraging software reuse.

<p>True</p> 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.

<p>False</p> Signup and view all the answers

A base class is often designed by factoring out commonalities among a set of classes.

<p>True</p> 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.

<p>True</p> Signup and view all the answers

The default implementation of method ToString of object returns a string representing ________.

<p>namespace_name.object_class_name</p> Signup and view all the answers

The default Equals implementation determines:

<p>whether two references refer to the same object in memory.</p> Signup and view all the answers

Method Equals will accurately compare any two objects.

<p>False</p> Signup and view all the answers

Every object in C# has at least seven methods: Equals, Finalize, GetHashCode, GetType, MemberwiseClone, ReferenceEquals, and ToString.

<p>True</p> 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.

Quiz Team

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!

More Like This

Use Quizgecko on...
Browser
Browser