Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / Visual Basic

The Many Faces of an Object

Rate me:
Please Sign up or sign in to vote.
4.46/5 (4 votes)
19 Oct 2015CPOL4 min read 10.9K   3   3
A very quick explanation of what an object is and how it is expresssed in three facets

Introduction

An object is a fundamental building block of a great number of programming languages - broadly categorised as "object oriented programming" (OOP).

Background

This is an article for absolute beginners to explain what an object is under the hood. It is not expressed in any language but is applicable to C# and VB.NET among others.

One Object, Three Facets

An object is a programming unit that attempts to encapsulate three related concepts:

  1. What am I?
  2. What can I do?
  3. What can be done with me?

What Am I?

The question "What am I?" is answered at two different levels in object oriented programming. The first is "what type of a thing am I?" which is answered by each object having one class. Behind the scenes, there is a look-up system which means that for any object, you can find its class. In .NET, this is done with the GetType method.

The family tree of "What am I?" is introduced by the concept of class inheritance. This allows a class to be specified as "I am an X which is a child of a Y... and so on".

The second part of "What am I?" is which individual instance of that class am I? Just as in the physical world, no two physical objects can occupy the same coordinates in space, so in the computing world, no two objects can occupy the same space in memory. Therefore, the second part of "What am I?" can basically be expressed as "Where am I in memory".

However in some circumstances, we want to treat two differently located objects as if they were the same based on some intrinsic properties they have. For example, if we were playing a card game like snap, we might want to say that two cards are the same if they have the same face value. In .NET, we would achieve this by overriding the Equals method and putting your own rules of equality in there.

What Can I Do?

What an object can do is a function of the data it contains and any methods (or functions) that it contains. The original idea behind object oriented programming was the realisation that keeping the data and the methods that operate on that data together made it easier for the programmer to understand and build large systems.

What Can Be Done With Me?

The aspect of object oriented programming that is often overlooked is "What can be done with me?" For example, some objects can be saved in a file, or sorted in a grid according to their properties, or indeed many many other operations.

The two main ways in which we indicate what can be done to an object in .NET are through Attributes or through Interfaces.

An attribute is a way of tagging more information about any part of your class onto it in a manner that other code can later read. For example, if you wanted to put some annotation on a property, you could use the DescriptionAttribute and any code that knew about that attribute could read it off your class and use it.

An interface is a way of making a promise of commitment to other code saying "I can or will do this". For example, you can have an interface that makes the promise, "I will provide code to turn this object to an XML data set and turn a valid XML data set into an object" (by implementing the interface ISerializable).

Mapping the Facets to the Implementation

So the facets of an object are mapped to the actual methods used to implemenmt them as follows:

The facet that answers the question "What am I?" is provided by the class identity, inheritance chain and by the instance data.

The facet "What can I do?" is provided by the class instance data and by the class methods.

The facet "What can be done with me?" is provided by the class attributes and interfaces, the methods it provides and the instance data it contains.

Points of Interest

This is only the very start of the journey in object oriented programming but using objects you can start to assemble something that is highly functioning and yet maintainable.

History

  • 19th October, 2015 - First cut
  • 29th July, 2016 - Added inheritance

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions

 
Questionis the second figure represents the object? Pin
Thava Rajan19-Oct-15 19:10
professionalThava Rajan19-Oct-15 19:10 
AnswerRe: is the second figure represents the object? Pin
Duncan Edwards Jones19-Oct-15 21:52
professionalDuncan Edwards Jones19-Oct-15 21:52 
QuestionRe: is the second figure represents the object? Pin
Thava Rajan21-Oct-15 18:45
professionalThava Rajan21-Oct-15 18:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.