According to Why define a Java object using interface (e.g. Map) rather than implementation (HashMap), I should declare the most abstract type as possible. However, in some cases I remember, especially for UI related codes, I can't declare the most abstract type because those UI object usually require to run some child class specific functions, consider Label is a subclass of View in some UI framework, eg:
//Label specific methods
Label* label=Label::create("myText");
label->setTextColor(255,0,0);
label->setTextSize(32);
//View methods
label->setViewPosition(400,300);
this->view->addChildView(label);
My question is, should I modify to:
View* label=[](){
//Label specific methods
Label* label=Label::create("myText");
label->setTextColor(255,0,0);
label->setTextSize(32);
return label;
}();
//View methods
label->setViewPosition(400,300);
this->view->addChildView(label);
which wrap all child class specific function first, so that the declaration can be the most abstract type? Is it necessary or over-engineering?