Why static members can’t be abstract and don’t implement polymorphism? [duplicate]

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật