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?
What happens if a derived class does not override a virtual method?
What happens if a derived class does not override a virtual method?
What is an example of polymorphism in .NET?
What is an example of polymorphism in .NET?
What is the difference between overriding and overloading?
What is the difference between overriding and overloading?
Signup and view all the answers
When is the selection of a method done in polymorphism?
When is the selection of a method done in polymorphism?
Signup and view all the answers
What is the purpose of the virtual modifier in the Object class?
What is the purpose of the virtual modifier in the Object class?
Signup and view all the answers
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?
Signup and view all the answers
What can a derived class do to a base class member?
What can a derived class do to a base class member?
Signup and view all the answers
What is the purpose of the 'override' keyword in method overriding?
What is the purpose of the 'override' keyword in method overriding?
Signup and view all the answers
What is polymorphism?
What is polymorphism?
Signup and view all the answers
What is dynamic binding?
What is dynamic binding?
Signup and view all the answers
What is the purpose of the 'virtual' keyword in method overriding?
What is the purpose of the 'virtual' keyword in method overriding?
Signup and view all the answers
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?
Signup and view all the answers
What is the purpose of the 'abstract' keyword in method overriding?
What is the purpose of the 'abstract' keyword in method overriding?
Signup and view all the answers
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?
Signup and view all the answers
What is the purpose of inheritance in object-oriented programming?
What is the purpose of inheritance in object-oriented programming?
Signup and view all the answers
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#?
Signup and view all the answers
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?
Signup and view all the answers
How do you prevent overriding of a method in C#?
How do you prevent overriding of a method in C#?
Signup and view all the answers
What is the purpose of the base keyword in C#?
What is the purpose of the base keyword in C#?
Signup and view all the answers
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;
?
Signup and view all the answers
What is the difference between method overriding and method shadowing in C#?
What is the difference between method overriding and method shadowing in C#?
Signup and view all the answers
What is the purpose of the override keyword in C#?
What is the purpose of the override keyword in C#?
Signup and view all the answers
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;
?
Signup and view all the answers
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.