#757 – Books on Object-Oriented Programming

Since C# is an object-oriented language, it can be helpful to study object-oriented design and programming techniques.  Here are some good books that focus on object-oriented design or programming.

  • Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development (3rd edition). Craig Larman. 2004.
  • Beginning C# Object-Oriented Programming. Dan Clark. 2011.
  • Design Patterns: Elements of Reusable Object-Oriented Software. Gamma, Helm, Johnson, Vlissides. 1994.
  • Designing Object-Oriented Software. Wirfs-Brock, Wilkerson, Wiener. 1990
  • Growing Object-Oriented Software, Guided by Tests. Freeman, Pryce. 2009.
  • Head First Object-Oriented Analysis and Design. McLaughlin, Pollice, West. 2006.
  • An Introduction to Object-Oriented Programming (3rd edition). Timothy Budd. 2001.
  • Object-Oriented Analysis and Design with Applications (3rd edition). Booch, Maksimchuk, et al. 2007.
  • Object-Oriented Design and Patterns. Cay S. Horstmann. 2005.
  • Object-Oriented Software Construction (2nd edition). Bertrand Meyer. 2000.
  • The Object-Oriented Thought Process (3rd edition). Matt Weisfeld. 2008.
  • OOP Demystified. Keogh, Giannini. 2004.
  • Principles of Object-Oriented Analysis and Design. Martin, Odell. 1993.

#688 – Aggregation, Composition and Containment

The idea of containment in object-oriented programming is the idea that an outer class contains an instance of another class and allows access to the contained object through its own methods.

Aggregation is one form of containment where the contained object can exist independently from the outer object.  For example, a Family object might contain an instance of a Dog object, representing a dog that the family owns.

    public class Family
    {
        List<Dog> OurDog { get; set; }

        void AcquireDog(Dog d)
        {
            OurDog.Add(d);
        }
    }

Composition is a type of containment where the contained object is created and exists entirely within the object that contains it.  It ceases to exist when the containing object is destroyed.  For example a Dog is composed of an instance of a DogName object (among other things).  You can’t have an instance of a DogName without a Dog.

#346 – Polymorphism

Recall that polymorphism is one of the three core principles of object-oriented programming.

Polymorphism is the idea that the same code can act differently, depending on the underlying type of the object being acted upon.  The type of the object is determined at run-time, rather than at compile-time.

In C#, you can use a variable declared as a base type to refer to instances of one or more derived types.  Polymorphism allows you to call a method that exists in the base type but whose implementation exists in the derived types.  The appropriate method in the derived type will be called, based on the type of the object.

            Dog d;

            d = new Terrier("Jack", 15);
            d.Bark();      // Terrier.Bark is called

            d = new Shepherd("Kirby", 12);
            d.Bark();      // Shepherd.Bark is called

#251 – Class Properties Support the Principle of Encapsulation

Encapsulation is one of the core principles of object-oriented programming.  Encapsulation is the idea of hiding implementation details of a class from the users of that class and only exposing a public interface.

Class properties in C# support the idea of encapsulation.  Client code that wants to read or write a property has access only to its public interface–the data type of the property and the knowledge that there is a get or a set accessor (or both) for that property.

Client code that reads or writes a property has no information about:

  • How the class actually stores the property data
  • The implementation of the get accessor–how data is retrieved when a client reads the property value
  • The implementation of the set accessor–how data is stored when a client writes the property value

#229 – The Core Principles of Object-Oriented Programming

As an object-oriented language, C# supports the three core principles of object-oriented programming:

  • Encapsulation – Hide implementation details in a class from users of the class, exposing only a public interface
  • Inheritance – Derive a subclass from a parent class, inheriting data and behavior from the parent, in an “is-a” relationship.  Inheritance defines a hierarchy of classes.  All classes ultimately inherit from System.Object.
  • Polymorphism – Any subtype may be used where a parent type (or type higher up in the class hierarchy) is expected.  Conversely, a variable of a particular class will be treated as the appropriate subclass.

#228 – Object-Oriented Programming in C# Using Classes

Classes are the construct in C# that enable the object-oriented paradigm and make C# an object-oriented language.

Classes are the user-defined types that define a set of related data and the methods that act upon that data.  Each instance of the class (an object) has a set of values for the class’ data, know as the object’s state.

Object-orientation is a powerful paradigm to use when writing software because using classes and objects helps you think about things, rather than algorithms.

For example, we might have a Dog class with data fields like: Name, Age, Breed, LikesBalls, and PersonalityType.  We might have methods in the Dog class like Bark, Sit, and Fetch.  Each instance of a Dog would have different values for these fields and we can call any of the Dog methods on an instance of a dog and it would respond based on the value of these fields.