Hot answers tagged

110

No, an object does not have to represent an entity. In fact, I would argue that when you stop thinking about objects as physical entities is when you finally get the benefits that OOP promises. This isn't the best example, but the Coffee Maker design is probably where the light started to come on for me. Objects are about messages. They're about ...


66

Congratulations! You rediscovered the well known fact that object orientation can be done without specific programming language support. It is basically the same way objects are introduced in Scheme in this classic text book. Note that Scheme does not have a class keyword or some kind of equivalent, and objects can be created without having even classes. ...


46

An example is when you want to have the same object in two separate lists: Dog myDog = new Dog(); List dogsWithRabies = new ArrayList(); List dogsThatCanPlayPiano = new ArrayList(); dogsWithRabies.add(myDog); dogsThatCanPlayPiano.add(myDog); // Now each List has a reference to the same dog Another use is when you have the same object playing several roles:...


39

They both are member variables, meaning that both are associated with a class. Now of course, there are differences between the two: Instance variables: These variables belong to the instance of a class, thus an object. And every instance of that class (object) has it's own copy of that variable. Changes made to the variable don't reflect in other instances ...


35

In your design, coins need to know about vending machines. This unnecessary coupling seems to be a wrong start: The vending machine’s responsibility is to provide a product if the price is paid. The latter is achieved by inserting coins. So the vending machine needs to know about coins and inserting coins to achieve its purpose. Moreover it probably needs ...


32

I am trying to understand behind the curtain scenes of Javascript and kind of stuck in understanding the creation of built in objects, specially Object and Function and the relation between them. It is complicated, it is easy to misunderstand, and a great many beginner Javascript books get it wrong, so do not trust everything you read. I was one of the ...


21

Can classes represent entity-less objects? If not, why they are bad/incomplete/non-OOP-centric? Are there ways they need to be changed/improved? In short, you can do anything, but this specific scenario would be against OOP principles :) What you are describing is sometimes called a "utility" class - usually a sign of code smell. You want to avoid creating ...


15

Can classes represent entity-less objects? Can? Yes. Should? Probably not - or at least, not how you're phrasing things. Objects actually are best when not representing a physical object directly since reality so infrequently maps nicely to code. But they do need to represent a cohesive concept or code-object. They need to represent a single cohesive ...


14

I'm not going to criticize your design because I see that it's not the point. Indeed coins shouldn't know about vending machines, and the related arguments, are valid. Let's assume that these classes are just dummies like Foo and Bar. The crux of your question seems to be in these code comments: coin->Insert(vending_machine); // which ...


13

I would agree that the first definition satisfies the three points your teacher made. I do not think we need the class keyword for anything. Under the covers, what else is an object but a data structure with with different types of data and functions to work with the data? Of course, the functions are data as well.. I would go even further and say that ...


12

The code is being too literal in attempting to model the real world. Object oriented programming does not need to model the real world. Instead, it should model the business process of purchasing something from a vending machine. Instead of a Coin class, consider using an integral type representing the number of cents (if using USD) or another small unit of ...


11

The question asks "which process determines which method should execute?" This is a bad question. But, we can immediately eliminate three of the choices: Is-A, Has-A, and Parent Class, since those are object-oriented, but not certainly not processes. Even if Is-A and Has-A were processes, they would be processes regarding class and composition, as you ...


11

A class should model something - otherwise it's pointless. However, what's being modeled may not be a physical "thing"; instead, it may be a representation of something which is not 'real', but which is needed to control the system being modeled. For example, in a traffic light control system you could very well have some sort of ControlSignal class, ...


9

On each iteration, a new String is created by the + operator and assigned to s. After return, all of them but the last are garbage-collected. String constants like "" and "a" are not created every time, these are interned strings. Since strings are immutable, they can freely be shared; this happens to string constants. To efficiently concatenate strings, ...


9

Of course you can! The Self programming language is a dynamic prototype-based object oriented language in which everything is an object and there is no sense of classes or whatsoever. It's focused in the idea of prototypical objects and the idea of cloning them instead of having classes as templates of how to create objects. You should check http://www....


9

As per this link Seeing as rule #1 is horrible, I would caution against taking the rest of the blog post as law. Though really, no blog post should be taken as law. And the quote itself is rather contradictory: A good object should never change his encapsulated state. Be aware that immutability doesn't mean that all methods always return the same ...


9

If this is the case then would it be right to say that it is not stored in memory? Not really. The Scene object is being instantiated, which by definition means memory is allocated. Why would we want to do this? As user232967 pointed out, it's a convenient way to declutter your source code. Why write (and force others to read) 5 lines of code when 1 ...


8

Deciding whether or not null is a allowed as an object value is a decision that you must make consciously for your project. You don't have to accept a language construct just because it exists; in fact, it is often better to enforce a strict rule against any nullvalues in the entire project. If you do this, you don't need checks; if a NullPointerException ...


7

Doing this is the best (and verbose) option right now. return {x: x, y: y} Or, less verbose but cleaner return {x, y} The caller would have to do this to use the values var c = get_coords(); alert(c.x + ' ' + c.y); // c.x and c.y holds the coordinates If you care to use only single axis you could do - alert(get_coords().x); With the ECMAScript 6 you ...


7

Oh here I go, necroing another old question but I'm going to guess you didn't win this argument and here's why. It Sounds Like a Culture Problem You're their manager but you can't replace them and you have to go to your managers in order to get them to do what you believe they should be doing which in this case I assume is to at least knock it off with the ...


7

We have to decouple two concepts here. As of Java 7, the language does have RAII. It's called the try-with-resources statement. static String readFirstLineFromFile(String path) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.readLine(); } } This guarantees that the reader will be closed no matter ...


7

No. (Title edited to ask the opposite question!) eg: Public class MyRepository { public MyObject GetObject(string id) { //get data from the DB and build an object } } or Public class MyService { public MyObject3 ProcessData(MyObject1 obj1, MyObject2 obj2) { //perform a process which is not a sole responsiblity of obj1 ...


7

Just did some googling and it seems the terms were invented by Ole-Johan Dahl and Kristen Nygaard for their Simula programming language We chose the terms “class” and “objects” of classes for our new Simula. The notion of subclass was especially appealing to us, since we had seen many cases of objects belonging to different classes having common ...


6

None of the above. There is no universal term, it depends on the language and the community, but the correct answer is one of message dispatch (virtual) method dispatch (virtual) method resolution (virtual) method lookup vtable lookup or a similar term.


6

Consider two distinct concepts: objects and references. References 'point' to objects in memory, but they are not the objects themselves. In Java, we always refer to and control objects through references to them. In Java, you never store the object itself in the variable, only it's address in memory (not exactly - implementation dependent - but you get the ...


6

Create alias references for the Location and Address objects to shorten the assignments. $loc = $api_object->Organisation->Location; $main = $db_object->MainAddress; $loc[0]->AddressLine1 = $main->address_line_1; $loc[0]->AddressLine2 = $main->address_line_2;


6

You are absolutely correct. new operator reserves space in memory for a new object of Type Fruit. Assignment = assigns the reference to your variable x. If you were to write Fruit y = x;, you would only create a new reference, so both y and x would still be pointing to single location on memory. For more in-depth knowledge, check out this article.


Only top voted, non community-wiki answers of a minimum length are eligible