In the first chapter of this book, we will look at the below topics which get covered :
Initially we will talk about the conventions which we would use in the book. So, overall, we would be making use of UML diagrams whenever we want to define the relationships that different objects have with respect to each other which is the core of OOPS language such as C#.
So, overall, there could be 3 types of relationships between objects in the OOPS world and they are:
Association: Here, there could be m# of X object ß-à n# of Y objects and the X and Y Classes can be mutually independent in terms of their scope of existence (or lifecycle). An example would be that X is a Person Class’s object and Y is a Car Class’s object. A person can have n# of cars and a car can be driven by m# of persons. And, cars & persons can exist without affecting the existence of the other.
Aggregation: Here, there is a one-way relationship defined between 2 objects (X class’s object) and (Y class’s object). And also, the lifetime of these objects are again independent (similar to association relationship. Infact, aggregation is considered a derived special type of Association). An example would be X is a Car class and Y is Passenger class. So, a Car object can have 0 or more Passengers in it. But passenger can belong to a car or exist outside the car as well. And a car can exist without any passengers at all (so they are independent of one another). However, Car always assumes the upper hierarchy in terms of the car containing 0…3 # of passengers inside of it. So, there is always a parent-child relationship when it comes to Aggregations in general.
Composition: Here, the X class’s object would always contain Y class’s object. And Y as a child cant independently exist without X class’s object. Lifetime of Y class’s object is bound by the lifetime of X class’s object. And here as well, X class is a parent class for Y class. Example would be a Car containing 4 wheels.
A better representation of UML diagrams is provided in: https://youtu.be/UI6lqHOVHic?t=25
C#, currently supports only single level inheritance when it comes to inheriting from base classes. However, we can inherit from multiple interfaces if need be. A problem called the Diamond problem is the root cause of this issue in .NET world but its for the reader to explore this topic.
Overall, sub categories of inheritance are : Hierarchy based inheritance, multi-level inheritance and hybrid inheritance (C# does not support hybrid inheritance which is hybrid of multi-level and hierarchy based inheritances when it becomes like multiple inheritance)
We have 2 kids of Polymorphism:
a. Static / Early binding polymorphism : Also, called method overloading. Same named methods but with different signature.
b. Dynamic polymorphism / Runtime binding / Late binding : We have 3 sub categories here: a. Interface polymorphism b. Inheritance polymorphism c. Generics (which is about defining / enforcing behavior to a class)
More details of each sub category of dynamic polymorphism is provided below:
Interface polymorphism is about defining interfaces and hierarchy of child classes would implement the properties / behaviour of the interface.
Inheritance Polymorphism is about defining base classes (abstract / non-abstract) which is marked as new / virtual and the hierarchical child class can either redefine the same method (along with same signature) by method overriding / method hiding or can choose to reuse the method defined by parent class without overriding that particular member.
Generics is about defining the behavior for the class by use of <> syntax wherein as an example:
Public static class PetFeeder
{
Public static Feed<PA, PF>(PA pa, PF petFood) where PA: PetAnimal where PF: IPetFood
{
…
}
}
Usually, generics are defined for collections which have similar behaviors . As in, List<string> and List<int> are enumerated/ counted in the same way and therefore, we have a <> syntax for List<> as well.
Comments