1

I'm struggling with Object Oriented Programming.
From a conceptual point view, are instance methods contained in objects (like instance variables are)?

I am picturing objects like this:

object

4

Conceptually, methods are as much part of an object as the instance variables are. In that way, your view on objects is correct.

However, you should realize that if you have multiple objects of the same type, then physically there will be multiple copies of the instance variables in memory, but only a single copy of the methods. You can view this as an optimization over the conceptual view where the compiler writers found a way to let a single copy of an instance method work with multiple different instances.

0

It is important to grasp the differences inherent in objects, instances & inheritance.

  • An object or class defines a collection of what to store and how to process it complete with interface methods, some of each of these may be private others public.
  • An instance of an object initialises and stores the data and references the methods.
  • Inheritance allows you to specialise objects by adding, or even replacing, any of the above by creating a new object type or class at the time of defining the object.
  • Templates allow you to define objects/classes where some. or all, of the data types & methods are only created at the time of the first instanciation of that object or class with a given set of types for the templated items - so if you have a class that has one function parameter & storage variable that is a template type you will actually create one copy of that class for each concrete type of that specific item that you instanciate. Note that some languages, such as python, use "Duck Typing" where, provided all of the operations are valid, a new copy of the methods is not required because of type changes to members within instances.
  • Some languages, such as python, allow "Monkey Patching" both classes and instances. If an instance of an object has data members &/or methods added to that instance only, if you Monkey Patch the base class then, in python at least, existing instances of it will gain the new member/method.
0

This depends very much on the language.

  • In C♯, Visual Basic.NET, Eiffel (methods are called "routines" here, but they are the same thing), C++ ("member functions"), Smalltalk, Objective-C, Python, Newspeak, D, … methods are contained in classes.
  • In Java, methods are contained in classes or interfaces (in the form of "default methods").
  • In Ruby, methods are contained in modules (Class is a subclass of Module). Modules are used for mixin inheritance.
  • In Scala, methods are contained in classes, traits, or singleton objects. Traits are used for mixin inheritance.
  • In Self, NewtonScript, Act-1, Io, Ioke, Seph, ECMAScript, …, methods are contained in objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.