Govt. Exams
Entrance Exams
IComparable<T> interface requires implementation of CompareTo method to define comparison logic between objects of the same type.
Properties with private backing fields allow validation in setters, ensuring data integrity by preventing invalid values like negative prices.
public class Animal { public virtual void Sound() { Console.WriteLine("Some sound"); } }
public class Dog : Animal { public override void Sound() { Console.WriteLine("Bark"); } }
Animal animal = new Dog();
animal.Sound();
Due to polymorphism, the overridden method in Dog class is called at runtime, printing 'Bark' even though the reference type is Animal.
Static variables are shared across all instances and can be used to generate unique IDs automatically without modifying external data.
Virtual methods are designed to be overridden by derived classes, enabling polymorphic behavior and runtime method resolution.
Polymorphism through inheritance allows each vehicle type to override the toll calculation method with its own implementation, enabling different behavior for each type.
sealed prevents a class from being inherited, while abstract requires derived classes to implement its abstract members. They serve opposite purposes.
Inheritance allows creating a base class (LibraryItem) with common properties, and derived classes can inherit these properties, reducing code duplication.
C# supports multiple interface implementation. A class can implement multiple interfaces separated by commas, and must implement all abstract members of each interface.
Abstract classes can have both concrete (implemented) and abstract (unimplemented) methods. They cannot be instantiated directly but can be inherited. This perfectly fits the requirement.