Abstract Class Question

  • Can abstract class will have non abstract method

Yes Below Example

  • Method hiding principal ( use of New KeyWord)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public abstract class Animal
{
    public abstract void MakeSound();
    public void Eat()
    {
        Console.WriteLine("Eating...");
    }

    public void IsAlive()
    {
        Console.WriteLine("Yes");
    }
    public virtual void Sleep()
    {
        Console.WriteLine("Sleeping...");
    }
}

public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The dog barks.");
    }
    public void Bark()
    {
        Console.WriteLine("Barking...");
    }
    public new void IsAlive()
    {
        Console.WriteLine("NO");
    }
    public override void Sleep()
    {
        Console.WriteLine("The dog barks Sleeping.");
    }
}
class Program
{
    static void Main(string[] args)
    {
        // Create an instance of the Dog class
        Dog myDog = new Dog();

        // Call methods from the abstract class (Animal)
        myDog.Eat();    // Output: Eating...
        myDog.Sleep();      // Output: The dog barks Sleeping.
    }
}