Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Derived Classes | OOP Essentials
C# Beyond Basics

bookChallenge: Derived Classes

A class called Vehicle is given. Apart from that there are two other classes called Car and Plane.

There is currently an error in the code. To fix the error you need to make the Car and Plane inherit from Vehicle.

Edit the code such that Car and Plane become the child classes of Vehicle.

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637
using System; public class Vehicle { public int modelYear; public float fuel; } public class Car { public void Drive() { Console.WriteLine("The car is now driving."); } } public class Plane { public void Fly() { Console.WriteLine("The plane is now flying."); } } public class Program { public static void Main(string[] args) { Car c1 = new Car(); Plane p1 = new Plane(); c1.modelYear = 2024; p1.fuel = 6000; Console.WriteLine("Executed Successfully"); } }

Use the : symbol in the syntax of a derived class to specify the parent class.

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637
using System; public class Vehicle { public int modelYear; public float fuel; } public class Car : Vehicle { public void Drive() { Console.WriteLine("The car is now driving."); } } public class Plane : Vehicle { public void Fly() { Console.WriteLine("The plane is now flying."); } } public class Program { public static void Main(string[] args) { Car c1 = new Car(); Plane p1 = new Plane(); c1.modelYear = 2024; p1.fuel = 6000; Console.WriteLine("Executed Successfully"); } }
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show me the code for the Vehicle, Car, and Plane classes?

What does the error message say?

Can you explain how inheritance works in this context?

Awesome!

Completion rate improved to 2.04

bookChallenge: Derived Classes

Swipe to show menu

A class called Vehicle is given. Apart from that there are two other classes called Car and Plane.

There is currently an error in the code. To fix the error you need to make the Car and Plane inherit from Vehicle.

Edit the code such that Car and Plane become the child classes of Vehicle.

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637
using System; public class Vehicle { public int modelYear; public float fuel; } public class Car { public void Drive() { Console.WriteLine("The car is now driving."); } } public class Plane { public void Fly() { Console.WriteLine("The plane is now flying."); } } public class Program { public static void Main(string[] args) { Car c1 = new Car(); Plane p1 = new Plane(); c1.modelYear = 2024; p1.fuel = 6000; Console.WriteLine("Executed Successfully"); } }

Use the : symbol in the syntax of a derived class to specify the parent class.

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637
using System; public class Vehicle { public int modelYear; public float fuel; } public class Car : Vehicle { public void Drive() { Console.WriteLine("The car is now driving."); } } public class Plane : Vehicle { public void Fly() { Console.WriteLine("The plane is now flying."); } } public class Program { public static void Main(string[] args) { Car c1 = new Car(); Plane p1 = new Plane(); c1.modelYear = 2024; p1.fuel = 6000; Console.WriteLine("Executed Successfully"); } }
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 2
some-alt