What is the output of this C# code?
interface IAnimal { void Speak(); } abstract class Animal : IAnimal { public abstract void Speak(); public void Eat() { Console.WriteLine("Eating food"); } } class Dog : Animal { public override void Speak() { Console.WriteLine("Woof"); } } class Program { static void Main() { IAnimal animal = new Dog(); animal.Speak(); // animal.Eat(); // Uncommenting this line causes error } }
Remember that interface references only expose interface members.
The variable animal is of type IAnimal, which only declares Speak(). The Eat() method is defined in the abstract class Animal but is not part of the interface. So calling animal.Eat() causes a compile error. The Speak() method prints "Woof".
You want to design a system where multiple unrelated classes share a method signature but have different implementations. Which is the best choice?
Think about multiple inheritance and unrelated classes.
Interfaces allow unrelated classes to implement the same method signature without sharing implementation. Abstract classes are better when you want to share code or state. Sealed and static classes do not support inheritance or polymorphism.
What is the output of this C# program?
interface IVehicle { void Start(); } abstract class Vehicle : IVehicle { protected string model; public Vehicle(string model) { this.model = model; } public abstract void Start(); public void ShowModel() { Console.WriteLine($"Model: {model}"); } } class Car : Vehicle { public Car(string model) : base(model) {} public override void Start() { Console.WriteLine("Car started"); } } class Program { static void Main() { IVehicle v = new Car("Sedan"); v.Start(); // v.ShowModel(); // Uncommenting causes error } }
Interface reference only exposes interface methods.
The variable v is of type IVehicle, which only declares Start(). The method ShowModel() is defined in the abstract class Vehicle but is not part of the interface. So calling v.ShowModel() causes a compile error. The Start() method prints "Car started".
What error does this code produce?
interface IShape { double Area(); } abstract class Shape : IShape { public virtual double Area() { return 0; } } class Circle : Shape { private double radius; public Circle(double r) { radius = r; } public override double Area() { return Math.PI * radius * radius; } } class Program { static void Main() { IShape s = new Circle(3); Console.WriteLine(s.Area()); } }
Check method modifiers in abstract class and override usage.
The method Area() in Shape is now marked virtual, so Circle can override it. The output is the area of the circle with radius 3, approximately 28.2743338823081.
Which scenario best justifies using an abstract class instead of an interface in C#?
Think about code reuse and shared data.
Abstract classes allow you to provide default implementations and hold state (fields) that derived classes can use or override. Interfaces cannot hold state or provide implementations (except default interface methods in newer C#, but abstract classes remain the choice for shared state). Multiple inheritance of behavior is not supported by abstract classes but can be simulated with interfaces. Declaring only method signatures or contracts is the role of interfaces.