Core concepts in object-oriented programming: What does cookie dough have to do with C#?

Classes are the cookie cutters of OOP. Objects are the cookies.

Classes are the cookie cutters of OOP. Objects are the cookies.

Last time, I highlighted a specific feature of C#. Thanks to everyone who checked it out and kept the conversation going!

This time, I’m discussing general – but core – concepts in Object Oriented Programming (“OOP”). Candidly, our professor, Cecil Phillip assigned the topic. But I’m also responding to feedback from readers who aren’t working in C# yet. So for all of these reasons, let’s step back and take more of a birds-eye view of C#.

Doing things in an object-oriented manner is a way of thinking. And C# is an object-oriented language.

So, while I will illustrate with code examples, my focus will be on the following core concepts:
1) classes; 2) objects; 3) inheritance; and 4) polymorphism.

 

The terms class and object are sometimes used interchangeably. But classes describe the type of objects. And objects are usable instances of classes.

Think of classes like cookie cutters. Objects are like the cookies we cut out with those cookie cutters.

Or think of classes like blueprints. Objects are like the buildings we build from those blueprints.

Takeaway: classes are the templates from which we create objects.

 

Each class can have different class members that include properties that describe class data, methods that define class behavior, and events that provide communication between different classes and objects.

Fields and properties represent information that an object contains. Fields are like variables because they can be read or set directly. A method is an action that an object can perform. A class can have several implementations, or overloads, of the same method that differ in the number of parameters or parameter types. Note: in most cases you declare a method within a class definition. But C# also allows you to add methods to an existing class outside the actual definition of the class.

To create an object, you need to instantiate a class – or create a class instance. After instantiating a class, you can assign values to the instance’s properties and fields and you can invoke class methods.

Inheritance describes the ability to create new classes based on an existing class. Inheritance enables you to create a new class that reuses, extends, and modifies the behavior that is defined in another class. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. Notably, all classes in C# implicitly inherit from the Object class, which supports .NET class hierarchy and provides a lot of built-in functionality for all classes.

    // instantiates a class called DrawingObject
    public class DrawingObject
    {
        // implements DrawingObject, with a single method
        // note: the modifier "virtual" indicates to derived 
            // classes that they can override this method        
        public virtual void Draw()
        {

            Console.WriteLine("I'm a generic drawing object.");
        }
    }

    // instantiates a derived class, Line, which inherits 
        // from the base class, DrawingObject
    public class Line : DrawingObject
    {
        // note: the modifier "override" allows a method to 
            // override the virtual method of its base class
        public override void Draw()
        {
            Console.WriteLine("I'm a Line.");
        }
    }

    // instantiates a derived class, Circle, which inherits
        // from the base class, DrawingObject
    public class Circle : DrawingObject
    {
        public override void Draw()
        {
            Console.WriteLine("I'm a Circle.");
        }
    }

    // instantiates a derived class, Square, which inherits 
        // from the base class, DrawingObject
    public class Square : DrawingObject
    {
        public override void Draw()
        {
            Console.WriteLine("I'm a Square.");
        }
    }

Polymorphism – from Greek πολύς, polys, “many, much” and μορφή, morphē, “form, shape” – means many shapes. More specifically, it means that you can have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways.

Continuing with our example, we see polymorphism illustrated by each derived class – Line, Circle, and Square – responding differently to the same “Draw()” method even though each inherits from the same base class, DrawObject.

    public class DrawDemo
    {
        static void Main(string[] args)
        {
            //creates an array to hold four objects of type 
                // DrawingObject.
            DrawingObject[] dObj = new DrawingObject[4];

            // initializes objects in array
            dObj[0] = new Line();
            dObj[1] = new Circle();
            dObj[2] = new Square();
            dObj[3] = new DrawingObject();

            // enumerate through each element in the array, 
                // invoking the Draw() method
            foreach (DrawingObject drawObj in dObj)
            {
                drawObj.Draw();
            }

            // Output:

            // I'm a Line.
            // I'm a Circle.
            // I'm a Square.
            // I'm a generic drawing object.

            Console.ReadLine();
        }
    }

Thanks for reading. I’m grateful for your feedback. And if we haven’t yet, let’s connect!

Now to make some real and metaphorical cookies…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s