How to teach object oriented programming to procedural programmers? [closed]

The best thing you can do is: Have a ton of Q&A. Wikipedia’s procedural programming (PP) article really hits where you should start: Whereas procedural programming uses procedures to operate on data structures, object-oriented programming bundles the two together so an “object” operates on its “own” data structure. Once this is understood, I think a … Read more

Initializing a class with Class.forName() and which have a constructor which takes arguments

Use Class.getConstructor() and call Constructor.newInstance() on that. For example if this is your constructor on class Foo: public Foo(String bar, int baz) { } You’d have to do something like this: Constructor c = Class.forName(“Foo”).getConstructor(String.class, Integer.TYPE); Foo foo = (Foo) c.newInstance(“example”, 34); You’ll have to know what arguments need to be passed to the constructor. … Read more

How exactly do “Objects communicate with each other by passing messages”?

If we are talking about OOP than the term “message passing” comes from Smalltalk. In a few words the Smalltalk basic principles are: Object is the basic unit of object-oriented system. Objects have their own state. Objects communicate by sending and receiving messages. If you are interested in Smalltalk take a look at Pharo or … Read more

What is the difference between subtyping and inheritance in OO programming?

In addition to the answers already given, here’s a link to an article I think is relevant. Excerpts: In the object-oriented framework, inheritance is usually presented as a feature that goes hand in hand with subtyping when one organizes abstract datatypes in a hierarchy of classes. However, the two are orthogonal ideas. Subtyping refers to … Read more

How much work should the constructor for an HTML parsing class do?

I normally follow one easy principle: Everything that is mandatory for the correct existence and behavior of the class instance should be passed and done into the constructor. Every other activity is done by other methods. The constructor should never: use other methods of the class with the purpose of using overriding behavior act on … Read more

When to use interfaces or abstract classes? When to use both?

As a first rule of thumb, I prefer abstract classes over interfaces, based on the .NET Design Guidelines. The reasoning applies much wider than .NET, but is better explained in the book Framework Design Guidelines. The main reasoning behind the preference for abstract base classes is versioning, because you can always add a new virtual … Read more

How adding event handler inside a class with a class-method as the callback?

The this inside the event listener callback will be the element that fired the event. If you want the this to be the instance of your class, then either: Bind the function to the class instance: Using Function.prototype.bind, will create a new function that its this value will always be what you specify it to … Read more