2

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.

6
  • 1
    From what I gather skimming 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. Commented Apr 2, 2020 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". :-) ] Commented Apr 3, 2020 at 0: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"? Commented Apr 3, 2020 at 0:14
  • 1
    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. Commented Apr 3, 2020 at 0: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. Commented Apr 3, 2020 at 0:22

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

'bar = Bar(1,2,3)" should be 'bar = Bar(x=1, y=2, z=3)' .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.