In his Dog class hes created a few methods Here are the headers written in C

In his dog class hes created a few methods here are

This preview shows page 30 - 33 out of 65 pages.

In his Dogclass, he’s created a few methods. Here are the headers, written in C#:public void Bark() { … }public void WagTail() { … }public void Walk() { … }1.To be able to override these methods in the derived classes, what modifier should Viktor add to all three headers?a. newb. staticc. virtual2.How can Viktor invoke the Bark method in the Dog class from within a derived class?a. Bark()b. Dog.Bark()c. base.Bark()3.What should Viktor do if he wants the Bulldogclass to just use the Walkmethod fromDog?a. Implement Walkin Bulldog, copying and pasting the code from Dogb. Implement Walkin Bulldogand simply call Dog’s Walkmethodc. Do not implement Walkin BulldogOverriding a method allows a derived class to have its own implementation, different from other derived classes.
Background image
20CHAPTER 2: Understanding Object Oriented ProgrammingAnswers1.What modifier should Viktor add to the three methods?c. virtual2.How can Viktor invoke the Dog’s Bark()method?c. base.Bark(). The basekeyword is used to access members of the base class.3.How should Viktor retain the base class’s Walkmethod in a derived class?c. Do not implement Walkin Bulldog. No need to call base.Walk()—if a derived class does not override an inherited method, the base class’s method will automatically be used.Essential details• Important keywords:• base: Used to access members of the base class from within a derived class.• virtual: Allows a method’s implementation to be overridden in a derived class.• sealed: When applied to a class, prevents other class from inheriting from it; when applied to a member, prevents that member from being overridden by other classes.• new: When used as a modifier, this hides a base class member; the new member replaces the implementation in the base class. Note: This is different than the newoperator used to instantiate an object!• override: Required to replace an inherited member.The following C# code shows how Viktor can replace Dog’s methods in his derived classes (assuming he correctsDogas indicated by question 1):public override void Bark() { … }public override void WagTail() { … }public override void Walk() { … }FAST TRACK HELPd _______ /3
Background image
Understand encapsulation21OBJECTIVE UNDERSTANDING OBJECT ORIENTED PROGRAMMING 2.4Understand encapsulationSCENARIO:Viktor has done a great job designing software to customize Tailspin Toys’ robotic dogs. So great, in fact, that his boss wants to give him a promotion. Unfortunately, that means Viktor won’t be able to finish writing the software himself; instead, a new developer will take over the project. On the new developer’s first day, however, she accidentally sets the robot’s ageto -237 and crashes the program.
Background image
Image of page 33

You've reached the end of your free preview.

Want to read all 65 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture