0
0
C Sharp (C#)programming~10 mins

Base class and derived class in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a base class named Animal.

C Sharp (C#)
public class [1] 
{
    public virtual void Speak() 
    {
        Console.WriteLine("Animal sound");
    }
}
Drag options to blanks, or click blank then click option'
ADog
BAnimal
CCat
DBird
Attempts:
3 left
💡 Hint
Common Mistakes
Using a derived class name like Dog instead of the base class Animal.
2fill in blank
medium

Complete the code to declare a derived class Dog that inherits from Animal.

C Sharp (C#)
public class Dog : [1] 
{
    public void Bark() 
    {
        Console.WriteLine("Woof!");
    }
}
Drag options to blanks, or click blank then click option'
AAnimal
BFish
CBird
DCat
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name other than Animal after the colon.
3fill in blank
hard

Fix the error in the code to call the Speak method from the Dog class instance.

C Sharp (C#)
Dog myDog = new Dog();
myDog.[1]();
Drag options to blanks, or click blank then click option'
AFly
BBark
CRun
DSpeak
Attempts:
3 left
💡 Hint
Common Mistakes
Calling Bark instead of Speak when the goal is to call the base class method.
4fill in blank
hard

Fill both blanks to override the Speak method in the Dog class.

C Sharp (C#)
public class Dog : Animal 
{
    public override void [1]() 
    {
        Console.WriteLine([2]);
    }
}
Drag options to blanks, or click blank then click option'
ASpeak
B"Woof!"
C"Meow!"
DBark
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name or wrong string inside Console.WriteLine.
5fill in blank
hard

Fill all three blanks to create a derived class Cat that overrides Speak and adds a new method Purr.

C Sharp (C#)
public class Cat : [1] 
{
    public override void [2]() 
    {
        Console.WriteLine("Meow!");
    }

    public void [3]() 
    {
        Console.WriteLine("Purr...");
    }
}
Drag options to blanks, or click blank then click option'
AAnimal
BSpeak
CPurr
DDog
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong base class or method names.