2081 1

BIT351 · 2081 · 10 marks

Define indexers. What is the use of base keyword? How can you apply polymorphism in code extensibility?

Define indexers. What is the use of base keyword? How can you apply polymorphism in code extensibility?[10]

NET Centric ComputingChapter 1: Core .NET Framework FundamentalsIndexers

Quick answer

An indexer is a special class member in C# that allows an object to be indexed like an array. It enables instances of a class to be accessed using the subscript notation ([]), making the class behave like a virtual array.

Indexers, Base Keyword, and Polymorphism in Code Extensibility

Note: No specific curriculum notes were provided for this question. The answer below is based on standard C# / Object-Oriented Programming concepts as taught in TU BSc CSIT curriculum.


1. Indexers (3 marks)

Definition

Syntax

<access_modifier> <return_type> this[<parameter_type> index]
{
    get { // return value }
    set { // set value }
}

Example

class StudentMarks
{
    private int[] marks = new int[5];

    // Indexer definition
    public int this[int index]
    {
        get
        {
            return marks[index];
        }
        set
        {
            marks[index] = value;
        }
    }
}

class Program
{
    static void Main()
    {
        StudentMarks s = new StudentMarks();
        s[0] = 85;   // uses set accessor
        s[1] = 90;   // uses set accessor
        Console.WriteLine(s[0]);  // uses get accessor -> Output: 85
    }
}

Key Features of Indexers

FeatureDescription
Keyword usedthis
Can be overloadedYes
Can have get/setYes
Parameter typeCan be int, string, or any type
Access modifierCan be public, private, etc.

2. The base Keyword (3 marks)

Definition

The base keyword in C# is used to access members of the base (parent) class from within a derived (child) class. It is used when a derived class wants to call or refer to the parent class's constructor, methods, or properties that have been overridden.

Uses of base Keyword

a) Calling Base Class Constructor

class Animal
{
    string name;

    public Animal(string name)
    {
        this.name = name;
        Console.WriteLine("Animal constructor called: " + name);
    }
}

class Dog : Animal
{
    public Dog(string name) : base(name)  // calls Animal's constructor
    {
        Console.WriteLine("Dog constructor called");
    }
}

b) Calling Overridden Base Class Method

class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Drawing a Shape");
    }
}

class Circle : Shape
{
    public override void Draw()
    {
        base.Draw();  // calls Shape's Draw() method
        Console.WriteLine("Drawing a Circle");
    }
}

Output:

Drawing a Shape
Drawing a Circle

Summary of base Keyword Uses

  • base.MethodName() -- calls the parent class method
  • base(arguments) -- calls the parent class constructor
  • Prevents ambiguity when method names are the same in parent and child classes

3. Polymorphism in Code Extensibility (4 marks)

Definition

Polymorphism means "many forms." It is the ability of a method, object, or variable to take multiple forms. In OOP, polymorphism allows a single interface to represent different underlying data types or behaviors.

Types of Polymorphism

TypeAlso Known AsAchieved By
Compile-timeStatic / Early BindingMethod Overloading, Operator Overloading
Run-timeDynamic / Late BindingMethod Overriding (virtual/override)

How Polymorphism Enables Code Extensibility

Code extensibility means the ability to add new functionality without modifying existing code. Polymorphism supports this through:

  1. Open/Closed Principle -- classes are open for extension but closed for modification.
  2. Runtime polymorphism allows new derived classes to be added without changing the base class or calling code.

Example: Polymorphism for Code Extensibility

// Base class
class Shape
{
    public virtual double Area()
    {
        return 0;
    }

    public void Display()
    {
        Console.WriteLine("Area = " + Area());
    }
}

// Derived class 1
class Circle : Shape
{
    double radius;
    public Circle(double r) { radius = r; }

    public override double Area()
    {
        return 3.14 * radius * radius;
    }
}

// Derived class 2
class Rectangle : Shape
{
    double length, width;
    public Rectangle(double l, double w) { length = l; width = w; }

    public override double Area()
    {
        return length * width;
    }
}

// NEW class added WITHOUT modifying existing code
class Triangle : Shape
{
    double base_t, height;
    public Triangle(double b, double h) { base_t = b; height = h; }

    public override double Area()
    {
        return 0.5 * base_t * height;
    }
}

class Program
{
    static void Main()
    {
        // Polymorphic array
        Shape[] shapes = new Shape[3];
        shapes[0] = new Circle(5);
        shapes[1] = new Rectangle(4, 6);
        shapes[2] = new Triangle(3, 8);

        foreach (Shape s in shapes)
        {
            s.Display();  // calls the correct overridden Area()
        }
    }
}

Output:

Area = 78.5
Area = 24
Area = 12

Explanation of Extensibility

  • The Display() method in Shape calls Area() which is virtual.
  • When a new shape like Triangle is added

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.