Could someone objectively tell me what's the difference between object-oriented and class-oriented/based programming? I know a little OOP basics, but I'm clueless about the class-oriented paradigm. An enlightening example would be very welcome (it doesn't have to be some piece of code -- a brief explanation suffices). Thanks.
-
1From what I gather skimming https://en.wikipedia.org/wiki/Class-based_programming, what most people think of as OOP *is* class-oriented programming. You would contrast it with prototype-based programming. – chepner Apr 02 '20 at 23:56
-
1@Jessy No, not quite. I was not willing to learn that a class is a blueprint with which objects are created, etc. I know this already. What I understand is that there is a programming paradigm called OOP and another one called class-oriented or class-based programming, and that they're different somehow in style, approach, goals, whatever. Alas, there's always Wikipedia with its definition of class-based programming, but it's too abstract for me to understand. I need something simpler. [My 1st question and I got down-voted already. Guess that's what they call "beginner's luck". :-) ] – Carlos Gouveia Apr 03 '20 at 00:06
-
@chepner Wikipedia says, quote: "Class-based programming, or more commonly class-orientation, is a style of object-oriented programming (OOP)...". This hints that there _is_ a difference, albeit subtle, between OOP and COP/CBP. So let me give my question a different spin: how can I tell OOP from CBP programming? Given two pieces of code how could I then categorically say: "this is OOP whereas this is CBP"? – Carlos Gouveia Apr 03 '20 at 00:14
-
1https://en.wikipedia.org/wiki/Prototype-based_programming provides an example in JavaScript of defining an object's behavior via a prototype, rather than a class. The term "class-oriented programming" simply sounds unfamiliar because it's rarely used; what it describes is the style of OOP that you are already accustomed to. – chepner Apr 03 '20 at 00:20
-
Put another way: you are contrasting the wrong things. CBP is a style of OOP, just as PBP is. You already know what CBP is: it's the use of classes to define common behavior of different objects. – chepner Apr 03 '20 at 00:22
-
There are a number of good threads comparing class-based OOP vs. prototype-based OOP under the [prototype-programming](https://stackoverflow.com/questions/tagged/prototype-programming?tab=Votes) tag, for example: [one](https://stackoverflow.com/q/816071/1371329), [two](https://stackoverflow.com/q/879061/1371329), [three](https://stackoverflow.com/q/28783077/1371329). – jaco0646 Apr 03 '20 at 02:01
1 Answers
Class-based programming describes precisely the common form of object-oriented programming supported by most common languages: object behavior is defined in terms of classes of which objects are instances.
Here's an example of class-oriented programming (in Python):
class Foo:
def __init__(self, x, y):
self.one = x
self.two = y
class Bar(Foo):
def __init__(self, z, **kwargs):
super().__init__(**kwargs)
self.three = z
bar = Bar(x=1, y=2, z=3)
assert bar.one == 1
assert bar.two == 2
assert bar.three == 3
bar
"inherits" its support for attributes one
and two
from the class Foo
, which Bar
subclasses.
(Yes, it's exactly what you think of when you hear "object-oriented programming".)
In contrast, prototype-based programming would dispense with classes, and simply define one object in terms of another. Here's a short example taken from https://en.wikipedia.org/wiki/Prototype-based_programming, written in JavaScript. (It's also what the preceding class-based example is based on.)
var foo = {one: 1, two: 2};
var bar = Object.create(foo);
bar.three = 3;
bar.one; // 1
bar.two; // 2
bar.three; // 3
Instead of defining two classes, one of which inherits from the other and is used to define the attributes used by bar
, we define a "generic" object foo
that bar
will inherit from. We create bar
by using foo
as a "prototype"; in addition to whatever we define explicitly for bar
, it will also have what it inherits from foo
.

- 497,756
- 71
- 530
- 681