0% found this document useful (0 votes)
3 views5 pages

Michael Tibebu RCD/0729/2014 RCD/0708/2014

The document analyzes key concepts in object-oriented programming, focusing on abstract vs virtual methods, interfaces vs abstract classes, method overriding, 'is-a' vs 'has-a' relationships, safe casting, and extending vs implementing. It provides code examples and explanations for each concept, demonstrating their practical applications in a programming context. The analysis emphasizes the importance of these principles in structuring and designing software effectively.

Uploaded by

9w68mqqvn9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Michael Tibebu RCD/0729/2014 RCD/0708/2014

The document analyzes key concepts in object-oriented programming, focusing on abstract vs virtual methods, interfaces vs abstract classes, method overriding, 'is-a' vs 'has-a' relationships, safe casting, and extending vs implementing. It provides code examples and explanations for each concept, demonstrating their practical applications in a programming context. The analysis emphasizes the importance of these principles in structuring and designing software effectively.

Uploaded by

9w68mqqvn9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

RAD: Code Analysis

Department of Computer Science

GROUP MEMBERS ID NO

1. Michael Tibebu RCD/0729/2014


2. Bemnet Getasetegn RCD/0708/2014

SECTION: B
SUBMITTED TO: Haileyesus Tilahun
DUE DATE: DEC 13, 2024

1. Abstract vs Virtual Methods


Code Context:
• Abstract Method: The Drive method in the Vehicle class is defined as abstract,
meaning it must be overridden in any derived class.
public abstract void Drive();

• Virtual Method: The Refuel method in the Vehicle class is virtual, meaning it
provides a default implementation but can be optionally overridden in derived classes.
public virtual void Refuel()
{
Console.WriteLine($"{Name} is refueled.");
}

Analysis:
• Abstract methods enforce that derived classes must implement the method (Drive
is implemented in both Car and Bike classes).
• Virtual methods provide flexibility. The Refuel method has a default
implementation in Vehicle but is overridden in both Car and Bike classes for specific
behavior.

2. Interface vs Abstract Class

Code Context:
• Interface: IFuelEfficiency defines a contract with a single method:
public interface IFuelEfficiency
{
double GetFuelEfficiency();
}

• Abstract Class: Vehicle provides both fields (Name), constructors, and methods
(Drive and Refuel).

Analysis:
• Purpose: Interfaces define a contract that can be implemented by any class (e.g.,
Car implements IFuelEfficiency), while abstract classes provide a base structure with
shared logic and fields (Name in Vehicle).
• Use Case: The Car class inherits from Vehicle (abstract class) and implements
IFuelEfficiency (interface), showing how they can complement each other.

3. Override

Code Context:
The Car and Bike classes both override the Drive and Refuel methods from the Vehicle
class:
public override void Drive()
{
Console.WriteLine($"{Name} is being driven.");
}
public override void Refuel()
{
Console.WriteLine($"{Name} does not need refueling.");
}

Analysis:
• override keyword ensures that the method in the derived class (Car or Bike)
replaces the base class implementation (Vehicle) for that method.
• Importance: Overrides allow class-specific behavior while adhering to the base
class interface.

4. “Is-a” vs “Has-a”

Code Context:
• “Is-a”: Car and Bike inherit from Vehicle, meaning they are specialized types of
vehicles. This demonstrates an “is-a” relationship.
public class Car : Vehicle
public class Bike : Vehicle

• “Has-a”: The Garage class contains an array of Vehicle objects, illustrating a


“has-a” relationship.
private Vehicle[] vehicles;
Analysis:
• “Is-a” represents inheritance (e.g., a Car is a Vehicle).
• “Has-a” represents composition/aggregation (e.g., a Garage has a collection of
Vehicle objects).

5. Safe Castings

Code Context:
Safe casting is used with the is keyword in the Main method:
if (vehicle is Car castedCar)
{
Console.WriteLine($"Fuel Efficiency: {castedCar.GetFuelEfficiency()}
km/l");
}

Types of Casting:
• Upcasting: Assigning a derived class (Car) to a base class
reference (Vehicle):
Vehicle vehicle = car;

• Downcasting: Checking if a Vehicle is a Car and casting it:


if (vehicle is Car castedCar) { }

Why is it used?
• Upcasting allows treating objects uniformly (as Vehicle)
regardless of their specific type.
• Downcasting restores specific functionality (GetFuelEfficiency
from Car).

6. Extending vs Implementing

Code Context:
• Extending: Car and Bike extend the Vehicle class:
public class Car : Vehicle

• Implementing: Car implements the IFuelEfficiency interface:


public class Car : Vehicle, IFuelEfficiency

Analysis:
• Extending allows inheriting shared behavior and properties from
a base class.
• Implementing provides a way to ensure a class adheres to a
contract defined by the interface (e.g., GetFuelEfficiency).

This analysis connects key concepts in object-oriented programming


with the provided code, highlighting the principles in practice.

You might also like