What is the difference between class and instance methods?

Like most of the other answers have said, instance methods use an instance of a class, whereas a class method can be used with just the class name. In Objective-C they are defined thusly: @interface MyClass : NSObject + (void)aClassMethod; – (void)anInstanceMethod; @end They could then be used like so: [MyClass aClassMethod]; MyClass *object = … Read more

Should we @Override an interface’s method implementation?

You should use @Override whenever possible. It prevents simple mistakes from being made. Example: class C { @Override public boolean equals(SomeClass obj){ // code … } } This doesn’t compile because it doesn’t properly override public boolean equals(Object obj). The same will go for methods that implement an interface (1.6 and above only) or override … Read more

Use of .apply() with ‘new’ operator. Is this possible?

With ECMAScript5’s Function.prototype.bind things get pretty clean: function newCall(Cls) { return new (Function.prototype.bind.apply(Cls, arguments)); // or even // return new (Cls.bind.apply(Cls, arguments)); // if you know that Cls.bind has not been overwritten } It can be used as follows: var s = newCall(Something, a, b, c); or even directly: var s = new (Function.prototype.bind.call(Something, null, … Read more

What are the differences between struct and class in C++?

You forget the tricky 2nd difference between classes and structs. Quoth the standard (ยง11.2.2 in C++98 through C++11): In absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class. And just for completeness’ sake, the more widely … Read more

When to use an interface instead of an abstract class and vice versa?

I wrote an article about that: Abstract classes and interfaces Summarizing: When we talk about abstract classes we are defining characteristics of an object type; specifying what an object is. When we talk about an interface and define capabilities that we promise to provide, we are talking about establishing a contract about what the object … Read more

Explaining Python’s ‘__enter__’ and ‘__exit__’

Using these magic methods (__enter__, __exit__) allows you to implement objects which can be used easily with the with statement. The idea is that it makes it easy to build code which needs some ‘cleandown’ code executed (think of it as a try-finally block). Some more explanation here. A useful example could be a database … Read more

JavaScript: Class.method vs. Class.prototype.method

Yes, the first function has no relationship with an object instance of that constructor function, you can consider it like a ‘static method’. In JavaScript functions are first-class objects, that means you can treat them just like any object, in this case, you are only adding a property to the function object. The second function, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)