{"id":570,"date":"2024-12-26T12:57:19","date_gmt":"2024-12-26T12:57:19","guid":{"rendered":"https:\/\/docs.sisus.co\/init-args\/?p=570"},"modified":"2025-12-30T06:44:07","modified_gmt":"2025-12-30T06:44:07","slug":"inheritance-and-dependencies","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/problems-and-solutions\/inheritance-and-dependencies\/","title":{"rendered":"Inheritance and Dependencies"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>You have a base class that depends on some services.<\/p>\n<p>You want to create a class that derives from that base class, which depends on some additional services on top of the ones that the base class does.<\/p>\n<pre><span class=\"line_wrapper\">class Base : MonoBehaviour&lt;A, B&gt;\r\n{\r\n    \/\/ The base class depends on some services\r\n    A a;\r\n    B b;\r\n\r\n    protected override void Init(A a, B b)\r\n    {\r\n        this.a = a;\r\n        this.b = b;\r\n    }\r\n}\r\n\r\n<\/span><span class=\"line_wrapper\">class Derived : Base\r\n{\r\n    \/\/ A derived type depends on some additional services\r\n    C c;\r\n    D d;\r\n}<\/span><\/pre>\n<p>Since the Derived class must derive from Base, it can not simultanously derive from MonoBehaviour&lt;A, B, C, D&gt;.<\/p>\n<p>How can Derived be defined in such a way that:<\/p>\n<ol>\n<li>all four dependencies can be provided to it from the outside,<\/li>\n<li>and it will be able to acquire the dependencies automatically at the beginning of the Awake event?<\/li>\n<\/ol>\n<h1>Solution<\/h1>\n<p>To make it possible to inject all the objects that your derived class and its base class depend on, implement the <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/iinitializable\/\">IInitializable&lt;T&#8230;&gt;<\/a> interface, and list the types of all their combined dependencies as generic type arguments of the interface.<\/p>\n<p>You will also need to override the <em>bool Init(Context)<\/em> method, to make it acquire all the combined dependencies at the beginning of the Awake event, instead of just the ones that the base class depends on.<\/p>\n<pre class=\"code_syntax\"><span class=\"line_wrapper\">public class Derived : Base, IInitializable&lt;A, B, C, D&gt;\r\n<\/span><span class=\"line_wrapper\">{\r\n    C c;\r\n    D d;\r\n\r\n    \/\/ Implement Init(A, B, C, D) to be able to receive four Init arguments\r\n    public void Init(A a, B b, C c, D d)\r\n    {\r\n        \/\/ Init the base type\r\n        Init(a, b);\r\n\r\n        \/\/ Init the derived type\r\n        this.c = c;\r\n        this.d = d;\r\n    }\r\n\r\n     \/\/ Override Init(Context) to receive all four Init arguments at the beginning of the Awake event.\r\n     protected override bool Init(Context context) =&gt; InitArgs.TryGet&lt;Derived, A, B, C, D&gt;(this);\r\n}<\/span><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem You have a base class that depends on some services. You want to create a class that derives from that base class, which depends on some additional services on top of the ones that the base class does. class Base : MonoBehaviour&lt;A, B&gt; { \/\/ The base class depends on some services A a; ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/problems-and-solutions\/inheritance-and-dependencies\/\" title=\"read more\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/570"}],"collection":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/comments?post=570"}],"version-history":[{"count":15,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/570\/revisions"}],"predecessor-version":[{"id":572,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/570\/revisions\/572"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}