Course Features
- Lectures 12
- Quizzes 0
- Duration 10 weeks
- Skill level All levels
- Language English
- Students 36
- Certificate No
- Assessments Yes
This is an individual activity. You may consult your lecturer for any assistance.
Steps:
using System; namespace LearnInheritance { } |
has one double parameter for setting the Speed property
calls base() with that parameter
in its body, sets Wheels to 2
Add 5 to Speed
If Speed is greater than 15, set it to 15
An error might occur here, which is okay.
warning CS0108: ‘Bicycle.SpeedUp()’ hides inherited member ‘Vehicle.SpeedUp()’. Use the new keyword if hiding was intended.
The computer suggests using the new approach, but we prefer override for its clarity.
In Bicycle.cs, label SpeedUp() with override.
error CS0506: ‘Bicycle.SpeedUp()’: cannot override inherited member ‘Vehicle.SpeedUp()’ because it is not marked virtual, abstract, or override
In Vehicle.cs, label SpeedUp() with virtual.
Subtracts 5 from Speed
Sets it to 0 if Speed is less than 0
Is labeled override
Hints:
public bool IsFake
{ get; protected set; }
class Dog : Animal
{}
class Dog : Animal
{
public Dog(int age) : base(age)
{
}
}
public override void FakeIt()
Some compilers will show a similar warning that mentions both override and new:
warning CS0114: ‘Bicycle.SpeedUp()’ hides inherited member ‘Vehicle.SpeedUp()’. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
public virtual void FakeIt()
Solution:
Solution:
using System;
namespace LearnInheritance
{
class Bicycle : Vehicle
{
public Bicycle(double speed) : base(speed)
{
Wheels = 2;
}
public override void SpeedUp()
{
Speed += 5;
if (Speed > 15)
{
Speed = 15;
}
}
public override void SlowDown()
{
Speed -= 5;
if (Speed < 0)
{
Speed = 0;
}
}
}
}
Emmanuel holds a Masters degree in Information Technology and has more than 10 years of experience in both academics and the software industry; with extensive experience in software development. Prior to joining redAcademy, Emmanuel held the position of Software Development Team Lead and Lecturer, working closely with students on Software Development projects.
During his career, Emmanuel has worked with students from different backgrounds, in both private and public institutions. This broad experience has further strengthened Emmanuel’s ability to engage a diverse group of students through his adaptive lecturing style. Emmanuel has a strong background in implementing and improving the full systems development cycle (SDLC), with the use of a wide range of technologies and frameworks.