Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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?

+6
−2

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?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+6
−0

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:

  1. encapsulation: don't expose an object's internal details to other objects.
  2. inheritance: one object can acquire properties and methods of another object.
  3. 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".

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Regarding the three pillars (1 comment)
Regarding the three pillars
Lundin‭ wrote almost 2 years ago

I'd say that the "three pillars" are private encapsulation, autonomous objects and inheritance. Autonomous objects meaning that each object just does it's own designated task and doesn't meddle with other unrelated things - it has loose coupling, as few dependencies on other things in the program as possible. Including as few dependencies as possible to other objects of the same class. Whereas polymorphism is just one form of inheritance, so it would sort under inheritance. Also inheritance and polymorphism are generally overrated, they have their uses but they are the least important OO "pillar".