Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Abstraction Practice | OOP Principles
course content

Зміст курсу

C# Beyond Basics

Abstraction PracticeAbstraction Practice

In this task you are given a code you might already be familiar with since we looked at similar code in multiple previous chapters. It contains an overridden method called MakeSound.

Your task is to convert the Animal class into an abstract class and the MakeSound method into an abstract method.

There should be no change in the output of the program after this modification.

cs

index.cs

1. Add the abstract keyword before the Animal class definition to make it abstract.
2. Add the abstract keyword before the makeSound method's return type in the Animal class to make the method abstract. Make sure to remove the method's body from the Animal class leaving only it's blueprint (return type, name and arguments) : returnType methodName(arg1, arg2, ..);

    using System;

    abstract class Animal
    {
        protected string species;
    
        public Animal(string species)
        {
            this.species = species;
        }
    
        public abstract void MakeSound();
    
        public void DisplaySpecies()
        {
            Console.WriteLine($"Species: {species}");
        }
    }
    
    class Cat : Animal
    {
        string furPattern;
    
        public Cat(string species, string furPattern) : base(species)
        {
            this.furPattern = furPattern;
        }
    
        public override void MakeSound()
        {
            Console.WriteLine("Meow! Meow!");
        }
    }
    
    class Dog : Animal
    {
        float weight;
    
        public Dog(string species, float weight) : base(species)
        {
            this.weight = weight;
        }
    
        public override void MakeSound()
        {
            Console.WriteLine("Woof! Woof!");
        }
    }
    
    class ConsoleApp
    {
        static void Main()
        {
            Animal myCat = new Cat("Feline", "Ginger & White");
            Animal myDog = new Dog("Canine", 42.5f);
    
            myCat.DisplaySpecies();
            myCat.MakeSound();
    
            Console.WriteLine("\n");
    
            myDog.DisplaySpecies();
            myDog.MakeSound();
        }
    }    
    

Все було зрозуміло?

Секція 5. Розділ 9
course content

Зміст курсу

C# Beyond Basics

Abstraction PracticeAbstraction Practice

In this task you are given a code you might already be familiar with since we looked at similar code in multiple previous chapters. It contains an overridden method called MakeSound.

Your task is to convert the Animal class into an abstract class and the MakeSound method into an abstract method.

There should be no change in the output of the program after this modification.

cs

index.cs

1. Add the abstract keyword before the Animal class definition to make it abstract.
2. Add the abstract keyword before the makeSound method's return type in the Animal class to make the method abstract. Make sure to remove the method's body from the Animal class leaving only it's blueprint (return type, name and arguments) : returnType methodName(arg1, arg2, ..);

    using System;

    abstract class Animal
    {
        protected string species;
    
        public Animal(string species)
        {
            this.species = species;
        }
    
        public abstract void MakeSound();
    
        public void DisplaySpecies()
        {
            Console.WriteLine($"Species: {species}");
        }
    }
    
    class Cat : Animal
    {
        string furPattern;
    
        public Cat(string species, string furPattern) : base(species)
        {
            this.furPattern = furPattern;
        }
    
        public override void MakeSound()
        {
            Console.WriteLine("Meow! Meow!");
        }
    }
    
    class Dog : Animal
    {
        float weight;
    
        public Dog(string species, float weight) : base(species)
        {
            this.weight = weight;
        }
    
        public override void MakeSound()
        {
            Console.WriteLine("Woof! Woof!");
        }
    }
    
    class ConsoleApp
    {
        static void Main()
        {
            Animal myCat = new Cat("Feline", "Ginger & White");
            Animal myDog = new Dog("Canine", 42.5f);
    
            myCat.DisplaySpecies();
            myCat.MakeSound();
    
            Console.WriteLine("\n");
    
            myDog.DisplaySpecies();
            myDog.MakeSound();
        }
    }    
    

Все було зрозуміло?

Секція 5. Розділ 9
some-alt