Podcast
Questions and Answers
What is the purpose of the virtual method in a base class?
What is the purpose of the virtual method in a base class?
- To hide methods and data from the derived class
- To prevent overriding in a derived class
- To overload methods in a derived class
- To allow overriding in a derived class (correct)
What happens if a derived class does not override a virtual method?
What happens if a derived class does not override a virtual method?
- The method is deleted from the derived class
- The derived class can use the base class version (correct)
- The derived class must override the method
- The base class version is not accessible
What is an example of polymorphism in .NET?
What is an example of polymorphism in .NET?
- Method overriding
- Dynamic binding
- Method overloading
- The ToString() method (correct)
What is the difference between overriding and overloading?
What is the difference between overriding and overloading?
When is the selection of a method done in polymorphism?
When is the selection of a method done in polymorphism?
What is the purpose of the virtual modifier in the Object class?
What is the purpose of the virtual modifier in the Object class?
What is the term used to describe classes that depend on field names from parent classes?
What is the term used to describe classes that depend on field names from parent classes?
What can a derived class do to a base class member?
What can a derived class do to a base class member?
What is the purpose of the 'override' keyword in method overriding?
What is the purpose of the 'override' keyword in method overriding?
What is polymorphism?
What is polymorphism?
What is dynamic binding?
What is dynamic binding?
What is the purpose of the 'virtual' keyword in method overriding?
What is the purpose of the 'virtual' keyword in method overriding?
What is the term used to describe the process of replacing a method defined at a higher level with a new definition?
What is the term used to describe the process of replacing a method defined at a higher level with a new definition?
What is the purpose of the 'abstract' keyword in method overriding?
What is the purpose of the 'abstract' keyword in method overriding?
What is the term used to describe the process of using the same method to indicate different implementations?
What is the term used to describe the process of using the same method to indicate different implementations?
What is the purpose of inheritance in object-oriented programming?
What is the purpose of inheritance in object-oriented programming?
What is the purpose of marking a base instance method as virtual in C#?
What is the purpose of marking a base instance method as virtual in C#?
What happens if you mark a base instance method as virtual but not the derived instance method as override?
What happens if you mark a base instance method as virtual but not the derived instance method as override?
How do you prevent overriding of a method in C#?
How do you prevent overriding of a method in C#?
What is the purpose of the base keyword in C#?
What is the purpose of the base keyword in C#?
What is the result of the following code: ScholarshipStudent freeStudent = new ScholarshipStudent(); freeStudent.Credits = 15;
?
What is the result of the following code: ScholarshipStudent freeStudent = new ScholarshipStudent(); freeStudent.Credits = 15;
?
What is the difference between method overriding and method shadowing in C#?
What is the difference between method overriding and method shadowing in C#?
What is the purpose of the override keyword in C#?
What is the purpose of the override keyword in C#?
What is the result of the following code: Student payingStudent = new Student(); payingStudent.Credits = 15;
?
What is the result of the following code: Student payingStudent = new Student(); payingStudent.Credits = 15;
?
Study Notes
Overriding Base Class Members
- A virtual method can be overridden by a method with the same signature in a child class.
- A derived class can override and hide methods and data from the base class.
- A base class member that is not hidden by the derived class is visible in the derived class.
Virtual Methods
- The
ToString()
method uses the virtual modifier in theObject
class, implying that any class can override it. - Overriding differs from overloading a method, as overridden methods have exactly the same signature, while overloaded methods have different signatures.
Dynamic Binding and Polymorphism
- Polymorphism uses dynamic binding, where the selection of the method is done at runtime.
- Classes that depend on field names from parent classes are said to be fragile because they are prone to errors.
Using the protected Access Specifier
- The
protected
access specifier allows access to base class members from derived classes.
Polymorphism and Overriding Base Class Members
- A derived class contains data and methods defined in the original class.
- Polymorphism is using the same method to indicate different implementations.
- Examples of polymorphism include:
- Different musical instruments having a
Play()
method. - Different vehicles having an
Operate()
method. - Different schools having a
SatisfyGraduationRequirements()
method.
- Different musical instruments having a
Overriding Methods
- To override a method, use the
override
keyword in the derived class. - To prevent overriding of a method, don't use the
virtual
keyword. - It is possible to shadow a method in a derived class by marking it as
new
. - The CLR looks at the actual type of the object referred to.
The ScholarshipStudent Class
- The
ScholarshipStudent
class overrides theCredits
property. - In the
ScholarshipStudent
class, thetuition
is set to 0 when theCredits
property is set.
The DemoStudents Program
- The
DemoStudents
program demonstrates the use of inheritance and polymorphism. - The program creates instances of
Student
andScholarshipStudent
classes and calls their methods.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Learn about inheritance in C# including virtual methods, overriding, and polymorphism with this quiz.