DotNetSlackers: ASP.NET News for lazy Developers

Wednesday, October 14, 2015

Classes & Objects in OOPS



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
4) polymorphism.

CLasses and objectS GenerallY

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.

CLasseS

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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.

No comments:

Post a Comment