I’m a Java/C#/PHP developer, through my OOP programming experience I find myself asking the same question: Why static members can’t be abstract and don’t implement polymorphism especially in situations involving factory methods, for example:
abstract class Resource {
public void doSomething();
}
abstract class User<resource extends Resource> {
//if i want to instantiate resource,
//my only option here is to create an abstract factory in user class
protected abstract resource create();
public void use(){
create().doSomething();
}
}
class FileUser extends User<File> {
//It's a bit clumsy, isn't it?
@Override
protected Internet create() {
return new File();
}
}
when abstract static methods would get me rid of these abstract factories:
(Below is not a valid code)
abstract class Resource {
public void doSomething();
//Im not sure how the child class would get passed though
static abstract Resource create();
}
abstract class User<resource extends Resource> {
public void use(){
resource.create().doSomething();
}
}
So, does anyone know why this kind of behavior wouldn’t be approved in OOP? Sorry if it sounds like a silly question.
5
Static methods are not a part of Object Oriented Programming, they are practically namespaced functions. Think about it this way, what does it mean to have an abstract method, or to have polymorphic behavior?
It means that you can do this:
function connectToDB(/*string */ $dsn, /* string */ $user, /*string*/ $password, /*Logger*/ $logger) {
$pdo = new PDO($dsn, $user, $password);
//This is polymorphic, you can pass any object that implements the Logger
//interface, without exposing to this function what logging mechanism is used.
$logger->log('Connected to database!');
}
How would you do this with static methods? Static methods include the class name in the call, you can’t (reasonably) pass any logger you want, and use the log
static method on it.
The same goes for abstract. There’s just no point of an abstract static method.
1
I’m no expert in any of the aforementioned languages, but a quick perusal of the Java docs and the implicit assertion by you that they all act the same leads me to the understanding that they all use static in the same way that C++ does:
static methods/variables/whatevers are a class-level thing, NOT an object level thing. To call a static method or access a static variable, you MUST specify (implicitly or explicitly) the class whose static member you want to access. And you can do it without EVER creating an object of that class’s type.
If there’s no object, there CAN’T be polymorphism as it exists in these languages. It doesn’t make sense because these languages all do polymorphism based on the actual type of an object. No object, no polymorphism.
3
There is a current programming model called Object Oriented, and at it’s center is objects.
Various kinds of polymorphism are supported, even though some people like to argue about it. But the main kind and the most powerful, is dynamic binding of your method call to the method implemented by the actual type of the target object.
Different languages provide different levels of support for this. Some, like smalltalk, don’t guarantee that the method is implemented in the target class, and so at runtime you can get a “message not found” error (smalltalk calls methods messages, loosely speaking). Other, strongly typed languages like C++ and Java, have syntax rules whereby the compiler can guarantee that after your code has compiled correctly that the method call will go somewhere appropriate.
So you could create a language where the kind of thing you want, can happen. It just wouldn’t be Java or C++. You would need to think through all the corner cases to ensure that code always worked the way you define it to work in your language definition. Try reading the Java specification to get an idea how carefully this must be done.
A solution many people arrive at, instead of a new language, is Design Patterns. See OO Design and Wikipedia. Design Patterns provide common solutions to common problems, and give a common vocabulary for the solution.
You will always come across cases where “it would be nice if …” – and so many people produce solutions such as the multitude of open-source frameworks that exist. In comparison to a new complied language a framework is relatively easy to produce and disseminate.
So why can’t static members be abstract and don’t implement polymorphism? Because it would not be object oriented, and designing a language to do that and get people to accept and use it, is hard.