Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP: class based and prototype based, are there other alternatives?

I know of class-based and protype based object oriented programming languages, are there any other alternatives? What are they?

like image 794
Gabriel Ščerbák Avatar asked Apr 30 '10 16:04

Gabriel Ščerbák


People also ask

What is the difference between class-based and prototype-based?

The most important difference between class- and prototype-based inheritance is that a class defines a type which can be instantiated at runtime, whereas a prototype is itself an object instance.

Is Python prototype-based or class-based?

Most object-oriented languages out there, including Python, are class-based. But JavaScript is instead prototype-based. Over the years, this has led to a lot of confusion, and even more attempts to resolve that confusion, either by faking classes, or by explaining why prototypes are good.

What is prototype-based OOP?

Prototype-based programming is a style of object-oriented programming in which classes are not explicitly defined, but rather derived by adding properties and methods to an instance of another class or, less frequently, adding them to an empty object.


3 Answers

These are indeed the two main approaches behind object-oriented languages, and I'm not aware of another completely different underlying principle.

But there exists a lot of variants of both approaches, as well as a lot of other programming language constructs that tackles reuse/extensibility in either class-based or prototype-based language. Examples: traits, mixin, extension methods, partial class, generics, first-class slots, split objects, etc. A lot of such constructions are first proposed in research papers (ECOOP, OOPSLA, POPL conferences), and a few of them become mainstream in popular languages. But I would qualify them as variations and not as groundbreaking new underlying principle.

Note though than you can mimic object-oriented programming in languages which are not object-oriented per-se. For instance, with Clojure multi-method. Object-oriented and functional programming are also slowly merging, for instance in Scala.

EDIT

It's actually hard to make a list of classic/seminal papers, and I don't pretend to have sufficient knowledge to do so. If there is one somewhere, I would be very interested to see it :) Still, here are a few ones that might interest you.

Inheritance, delegation, subtyping:

  • Genericity vs inheritance
  • Inheritance Is Not Subtyping
  • On the notion of inheritance
  • Split objects: a disciplined use of delegation within objects

Module, composition, adaptation

  • Traits: Composable Units of Behavior
  • Expanders: Statically Scoped Object Adaptation for Java
  • JavaGI: Generalized Interfaces for Java
like image 190
ewernli Avatar answered Oct 19 '22 22:10

ewernli


mixins allow you to extend a class with code that is defined elsewhere, such as in a module.

See Ruby Mixin Tutorial for an introduction.

like image 35
Justin Ethier Avatar answered Oct 19 '22 22:10

Justin Ethier


Go has a concept that is similar to classes, but without inheritance and with very flexible interfaces. You can read more about it in Effective Go.

like image 33
Lukáš Lalinský Avatar answered Oct 20 '22 00:10

Lukáš Lalinský