C# Inheritance and Polymorphism

MeritoriousBinomial avatar
MeritoriousBinomial
·
·
Download

Start Quiz

Study Flashcards

24 Questions

What is the purpose of the virtual method in a base class?

To allow overriding in a derived class

What happens if a derived class does not override a virtual method?

The derived class can use the base class version

What is an example of polymorphism in .NET?

The ToString() method

What is the difference between overriding and overloading?

Overriding has the same signature, while overloading has a different signature

When is the selection of a method done in polymorphism?

At runtime

What is the purpose of the virtual modifier in the Object class?

To imply that any class can override the ToString() method

What is the term used to describe classes that depend on field names from parent classes?

Fragile

What can a derived class do to a base class member?

Either override or hide it

What is the purpose of the 'override' keyword in method overriding?

To provide a new definition for a method

What is polymorphism?

Using the same method to indicate different implementations

What is dynamic binding?

The selection of a method at runtime

What is the purpose of the 'virtual' keyword in method overriding?

To make the method overridable

What is the term used to describe the process of replacing a method defined at a higher level with a new definition?

Overriding

What is the purpose of the 'abstract' keyword in method overriding?

To make the method abstract

What is the term used to describe the process of using the same method to indicate different implementations?

Polymorphism

What is the purpose of inheritance in object-oriented programming?

To create a new class from an existing class

What is the purpose of marking a base instance method as virtual in C#?

To enable overriding of the method

What happens if you mark a base instance method as virtual but not the derived instance method as override?

The compiler will generate an error

How do you prevent overriding of a method in C#?

Don't use virtual on the base instance method

What is the purpose of the base keyword in C#?

To access the parent class method or property

What is the result of the following code: ScholarshipStudent freeStudent = new ScholarshipStudent(); freeStudent.Credits = 15;?

freeStudent's tuition will be set to 0

What is the difference between method overriding and method shadowing in C#?

Method overriding is used to override the base method, while method shadowing is used to hide the base method

What is the purpose of the override keyword in C#?

To override a base method

What is the result of the following code: Student payingStudent = new Student(); payingStudent.Credits = 15;?

payingStudent's tuition will be set to a value based on the Credits property

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 the Object 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.

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 the Credits property.
  • In the ScholarshipStudent class, the tuition is set to 0 when the Credits property is set.

The DemoStudents Program

  • The DemoStudents program demonstrates the use of inheritance and polymorphism.
  • The program creates instances of Student and ScholarshipStudent classes and calls their methods.

Learn about inheritance in C# including virtual methods, overriding, and polymorphism with this quiz.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free
Use Quizgecko on...
Browser
Browser