Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on Why object-oriented instead of class-oriented?
Parent
Why object-oriented instead of class-oriented?
I understand that in object-oriented programming, an object is an instance of a class.
If it's an instance, I misunderstand why does it need the term object at all (we could just say "instance").
Wouldn't it be more practical to say that this programming paradigm is class-oriented and that classes can be instantiated or not?
Post
Why object-oriented instead of class-oriented?
tl;dr
Because you can "do OOP" without classes.
Long answer
A class is one possible way to implement object oriented programming. But it's not the only one.
OOP is a programming paradigm: a particular "style"/way of designing programs. First of all, let's remind that there's no single, unique, upon-agreed, free-of-controversies, canonical definition for OOP. But most people "usually (tend to) kinda agree" on three basic pillars:
- encapsulation: don't expose an object's internal details to other objects.
- inheritance: one object can acquire properties and methods of another object.
- polymorphism: the ability to occurr in several different forms.
As I said, there's not a consensus about the correct definition. Some add a fourth pillar ("abstraction"), and we could debate the definition for hours, but that's beside the point. In case you're interested, there's already a post here with a more detailed discussion.
Anyway, you can achieve all those things (encapsulation, polymorphism, etc) without using classes.
For example, JavaScript uses prototypes to implement inheritance.
JavaScript has a way to define classes using the class
keyword, but that's just a syntax sugar, because in the end they're all "special" functions:
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
console.log(typeof Rectangle); // function
You can also achieve an object-oriented design with languages that have no classes, such as C - see an example here.
an object is an instance of a class.
In a general way, an object is something (a structure, a container, etc) that contains data (fields/attributes/properties) and "behaviour" (usually in the form of methods, that manipulate the data).
In a more general way, an object can be a variable, data structure, function/method, etc. Basically, any region of memory that has a value and can be accessed by some identifier.
For languages that have classes, though, an object is usually defined as an instance of a class.
But different languages can have different definitions. For example, MDN defines a JavaScript object as "a collection of properties". And you can create an object without using classes (considering that the language doesn't actually have classes, I mean "you can create an object without using the class
keyword"):
var obj = {
somefield: 1,
somemethod: function () {
console.log(this.somefield);
}
};
obj.somemethod(); // 1
Python, on the other hand, defines an object as "abstraction for data", which means that all data is represented by objects and relations between those (even a single number as 1
is also an object).
C11, section 3.15, defines object as "region of data storage in the execution environment, the contents of which can represent values".
Anyway, some languages define an object as an instance of a class, but the actual definition is way broader and it can vary according to the language. It's not something restricted to classes only.
A class is just a mechanism that some languages provide to achieve OOP goals (encapsulation, polymorphism, inheritance, etc). In the languages that have classes, I recognize that it makes things easier, IMO.
But it doesn't mean they're the only way to do it, you can do OOP without classes. That's why the paradigm isn't called "class-oriented programming".
0 comment threads