The Trick Of The Mind - Why Objects
Written by Mike James   
Wednesday, 26 July 2023
Article Index
The Trick Of The Mind - Why Objects
Instances & Class

Instances & Class

Now we come to an aspect of objects that confuses the beginner and expert alike. So far we have discussed objects, but we haven’t considered the mechanism by which we can obtain multiple objects of the same type. For example, we might well define a Person object with properties and methods, but we most likely want multiple copies of the object so we can represent multiple people.

What we have been calling objects are more properly called “instances” of a particular type of object. The question is how do we formalize the way we can create multiple instances of the same type of object? There are a number of approaches to this problem, but the most commonly encountered is the class mechanism. A “class” is a specification for a particular type of object. Once you have an object defined by a class you can use the class to create as many instances of the object you require. The class is like a template that allows you to “stamp out” as many instances of the object as you need. Typically you might create a Person class:

class Person
	name: string
	address: string
	age: number 
     	Retire: Return 70-age

Notice that following the name of each field we state what sort of data is going to be stored, but this isn’t essential to the idea. Once we have a class defined we can use it to create as many instances as we like:

me = new Person(Mike, 1 FORTRAN Drive, 18)
mickey = new Person(Mickey, Disney Road, 65)

In this case me and mickey have a name field, an address field, an age field and a Retire method. This means you could write things like:

me.name
mickey.Retire()

and so on. Obviously the output of the first is Mike and the second is 5. Notice that the properties and methods all work on their respective instance data, i.e. it may seem obvious that mickey.Retire() computes the retirement based on mickey.age, but in practice this is tricky to implement.

The usual solution is to simply pass the instance as the first parameter of the method. That is:

mickey.Retire()

is actually implemented behind the scenes as:

Retire(mickey)

This also means that methods all have a default first parameter which is traditionally, but not universally, called this. The use of the term this is widespread, and leads to much confusion, self is also used and is also the cause of much confusion. Sometimes finding good names for things is very important.

The introduction of classes also causes a problem with naming, especially for beginners. The problem is particularly acute when you only need one instance of a class, known as a “singleton”. Suppose you need a single object called MasterRecord. You first need to create a class. What do you call it? Probably MasterRecord. Now you have the class you can create an instance. What do you call it? You probably want it to be MasterRecord – but of course you can’t. In a language that takes notice of upper and lower case the best you can do is something like:

masterRecord = new MasterRecord()

which introduces a convention that classes are named with a starting capital letter. Some find this confusing because it seems we have lots of things called masterrecord with only changes to where the capital letters are.

The fact that you need a name for the class and names for each of the instances can sometimes be a problem.

In Chapter but not in this extract

  • Polymorphism
  • Encapsulation
  • Inheritance
  • Overriding
  • Real World Objects
  • Inheritance Hierarchies
  •  The Liskov Problem
  •  Restrictions
  •  Why are Objects So Unreasonably Effective?

 Summary

  • Object-Programming OPP is currently the most used programming methodology. It works by simply associating subroutines with the data they work with. Put simply, an object is a struct with fields that are subroutines or methods.

  • We also need a way to create multiple copies of objects and this leads to the idea of a class. A class defines an object and allows the programmer to create as many instances of the object as needed.

  • The first advantage of using classes and objects is polymorphism which ensures that the correct method will be called depending on the object in use.

  • The second advantage of using classes and objects is encapsulation, which means that the workings of the object can be hidden from the outside world.

  • The third advantage of using classes and objects, inheritance, isn’t always seen as an advantage. This is a way of reusing existing objects to create new extended objects.

  • Inheritance is dynamic and any changes made to a base class are passed on to derived classes. Inherited methods can be redefined or overridden and this is the cause of many problems.

  • The philosophy of object-oriented programming uses the idea that objects are simulations of the real world and inheritance mirrors the relationships between objects in the real world.

  • The Liskov principle states that a derived class can be used in place of a base class. This is only true if you work at making it true. Overriding can cause derived classes not to function as substitute base classes.

  • The usual real world relationship modeled by inheritance is IS A but many other relationships are possible. In particular, the HAS A relationship leads to the idea of containment.

  • Despite all of the subtle problems with object-oriented programming, it is hugely successful. The reason is that consuming objects is much easier than creating or modifying them and this is what most programmers do.

  • My advice to the beginning programmer is to focus first on the flow of control and top-down structured programming before object, an approach that is fostered by Python.

The Trick Of The Mind - Programming & ComputationalThought

Buy Now From Amazon

Trick360

Chapter List

  1. The Trick Of The Mind

  2. Little Languages
       Extract: Little Languages Arithmetic

  3. Big Languages Are Turing Complete

  4. The Strange Incident of The Goto Considered Harmful
       Extract: The Goto Considered Harmful

  5. On Being Variable  

  6. Representation

  7. The Loop Zoo
       Extract The Loop Zoo
      
    Extract Advanced Loops

  8. Modules, Subroutines, Procedures and Functions
       Extract Modular Programming

  9. Top-Down Programming 

  10. Algorithms
       Extract: Binary Search 
       Extract: Recursion ***NEW!!

  11. The Scientific Method As Debugging 

  12. The Object Of It All
       Extract Why Objects 

 <ASIN:1871962722>

<ASIN:B09MDL5J1S>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Kotlin Ktor Improves Client-Server Support
04/11/2024

Kotlin Ktor 3 is now available with better performance and improvements including support for server-sent events and CSRF (Cross-Site Request Forgery) protection.



Prompt Engineering Techniques To Make You An Expert
18/11/2024

Introducing a GitHub repository full of hot tips and instructions on how to build the perfect prompt presented in a collection of Jupiter Notebooks.


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Wednesday, 26 July 2023 )