Implementing an interface already implemented by a parent class?

If a class extends a parent which defines an interface, should that class also explicitly implement the interface?

As an example, is it better to include or remove the portion in square brackets below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>interface IVerbable {}
abstract class BaseVerber implements IVerbable {}
class Verber extends BaseVerber [implements IVerbable] {}
</code>
<code>interface IVerbable {} abstract class BaseVerber implements IVerbable {} class Verber extends BaseVerber [implements IVerbable] {} </code>
interface IVerbable {}

abstract class BaseVerber implements IVerbable {}

class Verber extends BaseVerber [implements IVerbable] {}

Functionally, there is no difference in forced implementation, since Verber “is-an” IVerbable by virtue of it’s parentage, even if it doesn’t explicitly implement the interface itself:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var_dump(new Verber() instanceof IVerbable); // bool(true)
</code>
<code>var_dump(new Verber() instanceof IVerbable); // bool(true) </code>
var_dump(new Verber() instanceof IVerbable); // bool(true)

Non-functionally, there seem to be two benefits to implementing in both places:

  • A person reading the code immediately knows that it implements the interface, without having to view the parent’s definition.
  • Second, PhpDocumentor doesn’t follow the trail if the abstract class doesn’t give a default implementation of the interface. The concrete implementation would not inherit the documentation unless either it explicitly implements the interface, or the abstract gives an implementation, even an empty one.

I suppose it might be bad practice to omit implemented methods from the abstract class though. It’s probably better to at least show the empty signature, but then you’re also just duplicating the empty signature in both the interface and the abstract class.

I my opinion you shouldn’t add the second implements IVerbable. Here is why:

  • It does not change the meaning of the code.
  • It is perfectly natural to navigate between classes when you try to understand someone else’s code. You will have to do that anyway.
  • It is the feature of the base class that it implements the interface, what the derived class inherits like everithing else. I does not make sense to repeat, as it does not make sense to repeat any other feature from the base class.
  • I am not familiar with PhpDocumentor, but it does not seem like a good idea to change your code just so a tool can understand it better. (If a tool can’t understand your code because you did not followed some generally accepted and advisable convention, that is a different case.)

3

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