Govt. Exams
Entrance Exams
Abstract classes in C# cannot be instantiated directly. The compiler enforces this rule at compile-time. Abstract classes serve as blueprints and must be inherited by concrete classes that provide implementations for all abstract members. This ensures proper design and forces derived classes to implement required functionality.
Encapsulation is the principle of bundling data and methods together while hiding internal details. Using private fields with public properties (getters/setters) allows controlled access to the stock level, preventing unauthorized direct modifications. This is the standard practice in inventory systems.
The 'base' keyword in the constructor initialization list ensures the parent class constructor is called before the derived class constructor body executes, maintaining proper initialization order.
C# allows one implementation to satisfy multiple interfaces when they have the same method signature. Explicit interface implementation can be used if methods need different behaviors.
Abstract methods have no implementation in the abstract class and force derived classes to provide their own implementation. They cannot be private as they must be overridable.
Interfaces establish a contract that forces implementing classes to provide their own logic for calculating delivery costs, promoting code consistency without duplicating implementation.
An abstract class is ideal when you want to provide common functionality and properties to derived classes while preventing direct instantiation. It can have both abstract and concrete members.
Composition is represented by having an object of one class as a member variable of another class, allowing code reuse through object containment rather than inheritance.
class Animal { }
class Dog : Animal { }
Dog dog = new Animal(); // Line 1
You cannot assign a base class instance to a derived class reference without explicit casting. This violates type safety rules in C#.
The 'sealed' keyword prevents a class from being used as a base class for inheritance, ensuring that derived classes cannot override the sealed class.