Source: Ch4_OOP.pdf
Composition
Composition vs aggregation, nested classes, and partial classes.
Composition
Composition is a 'has-a' relationship where the contained object cannot exist independently of the container. When the container is destroyed, its parts are too.
class Engine
{
public int Horsepower;
public Engine(int hp) { Horsepower = hp; }
}
class Car
{
private Engine engine;
public Car(int hp)
{
engine = new Engine(hp);
}
public void ShowPower() => Console.WriteLine(engine.Horsepower + " HP");
}
Car car = new Car(200);
car.ShowPower();Output
200 HP
Composition — Exercise 1
In composition the contained object is created inside the container's constructor, not passed in from outside.
Fix the composition — create Engine inside Car
Hint
Create the Engine instance inside Car and expose it via a property rather than making it public.
Which statement best describes composition?
Aggregation
Aggregation is a looser 'has-a' where the contained object can exist independently. The outer class receives a reference to an already-existing object.
class Teacher
{
public string Name;
public Teacher(string name) { Name = name; }
}
class Classroom
{
private Teacher teacher;
public Classroom(Teacher t)
{
teacher = t; // Teacher already exists outside
}
public void Info() => Console.WriteLine("Teacher: " + teacher.Name);
}
Teacher alice = new Teacher("Alice");
Classroom room = new Classroom(alice);
room.Info();
Console.WriteLine(alice.Name + " still exists");Output
Teacher: Alice Alice still exists
Aggregation — Exercise 1
Unlike composition, in aggregation the contained object is passed in (injected), not created inside the constructor.
Convert composition to aggregation
Hint
Accept the Pilot as a constructor parameter instead of creating it inside Plane.
What is the key difference between composition and aggregation?
Nested Classes
A class declared inside another class is a nested class. It is scoped to its outer class and is useful for implementation details that should not be visible outside.
class OuterClass
{
private int secret = 42;
public class InnerClass
{
public void Greet() => Console.WriteLine("Hello from Inner!");
}
public void ShowSecret(InnerClass ic)
{
Console.WriteLine("Secret: " + secret);
}
}
OuterClass.InnerClass inner = new OuterClass.InnerClass();
inner.Greet();Output
Hello from Inner!
Nested Classes — Exercise 1
A public nested class is accessed as OuterClass.InnerClass from outside the outer class.
Fix the nested class instantiation
Hint
A nested class must be qualified with its outer class name: Library.Book.
Where is a nested class visible when declared as private?
Partial Classes
The partial keyword splits a class definition across multiple files. All parts must use partial and the same class name. The compiler merges them into one class.
// File 1
public partial class Employee
{
public void DoWork() => Console.WriteLine("Working...");
}
// File 2
public partial class Employee
{
public void GoToLunch() => Console.WriteLine("Lunch time!");
}
Employee e = new Employee();
e.DoWork();
e.GoToLunch();Output
Working... Lunch time!
Partial Classes — Exercise 1
Every part of a partial class must include the partial keyword, otherwise the compiler sees two duplicate class definitions and reports an error.
Mark both parts as partial
Hint
Add the partial keyword to both class declarations.
What keyword splits a class across multiple files?