Site icon C1CTech

Kotlin Object Declarations and Expressions

&NewLine;<p class&equals;"editor-contents&lowbar;&lowbar;short-description">This tutorial is about object declarations &lpar;singletons&rpar; and object expressions with the help of examples&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Object Declarations<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>Object declarations defines singletons&period; Singleton is an object-oriented pattern where a class can have only one instance &lpar;object&rpar; at a time&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>Kotlin creates singletons with the <span style&equals;"color&colon; &num;008000&semi;"><strong>object<&sol;strong><&sol;span> keyword&period;<&sol;li><li>The object declaration can have <strong><span style&equals;"color&colon; &num;008000&semi;">functions<&sol;span><&sol;strong>&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">properties<&sol;span><&sol;strong>&comma; and the <span style&equals;"color&colon; &num;008000&semi;"><strong>init<&sol;strong><&sol;span> block&period;<&sol;li><li>We can use the <strong><span style&equals;"color&colon; &num;008000&semi;">init<&sol;span><&sol;strong> block if some initialisation is required&period; The init block will be called when your object gets constructed&period;<&sol;li><li>Object declaration&&num;8217&semi;s initialisation is thread-safe and done at first access&period;<&sol;li><li>The constructor method is not allowed in an object&period;<&sol;li><li>To access methods and properties of object&comma; use the object declaration&&num;8217&semi;s name and dot &lpar;&period;&rpar; notation&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example&colon;<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;creating object Test<&sol;span><&sol;strong>&NewLine;object Test &lbrace;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;init block<&sol;strong><&sol;span>&NewLine; init &lbrace;&NewLine; println&lpar;"Singleton class invoked&period;"&rpar;&NewLine; &rcub;&NewLine;&NewLine; var a&colon; Int &equals; 10&NewLine; var b&colon; Int &equals; 20&NewLine;&NewLine; fun add&lpar;&rpar;&colon; Int &equals; a &plus; b&NewLine;&rcub;&NewLine;&NewLine;fun main&lpar;&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;accessing members using object name<&sol;span><&sol;strong>&NewLine; println&lpar;"Addition of &dollar;&lbrace;Test&period;a&rcub; and &dollar;&lbrace;Test&period;b&rcub;&colon; &dollar;&lbrace;Test&period;add&lpar;&rpar;&rcub;"&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">Singleton class invoked&period;&NewLine;Addition of 10 and 20&colon; 30<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>Objects can also implement <strong><span style&equals;"color&colon; &num;0000ff&semi;">interfaces<&sol;span><&sol;strong> and extend other <strong><span style&equals;"color&colon; &num;0000ff&semi;">classes<&sol;span><&sol;strong> in a similar way like normal classes&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example&colon;<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;object RunnableSingleton implementing interface Runnable<&sol;strong><&sol;span>&NewLine;object RunnableSingleton &colon; Runnable &lbrace;&NewLine; override fun run&lpar;&rpar; &lbrace;&NewLine; println&lpar;"I'm a runnable singleton"&rpar;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>Kotlin also allows objects to be declared inside classes&period; Nested objects cannot access members from the outer class&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;declare an object Singleton inside a class Outer<&sol;strong><&sol;span>&NewLine;class Outer &lbrace;&NewLine; object Singleton &lbrace;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Object Expressions<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">The <span style&equals;"color&colon; &num;0000ff&semi;"><strong>object<&sol;strong><&sol;span> keyword can also be used to create objects of an anonymous class known as <span style&equals;"color&colon; &num;008000&semi;"><strong>anonymous objects<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">They are used if you need to create an object of a slight modification of some class or interface without declaring a subclass for it&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Anonymous object implementing an Interface<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<p>Let&&num;8217&semi;s create an anonymous object of a <strong>Runnable<&sol;strong> interface&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example&colon;<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">Thread&lpar;object &colon; Runnable &lbrace;&NewLine; override fun run&lpar;&rpar; &lbrace;&NewLine; println&lpar;"I am anonymous object"&rpar;&NewLine; &rcub;&NewLine;&rcub;&rpar;&period;run&lpar;&rpar;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>If you need a name for your anonymous object&comma; or need it to store for later use&comma; you can initialise a variable with it&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example&colon;<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">val runnable &equals; object &colon; Runnable &lbrace;&NewLine; override fun run&lpar;&rpar; &lbrace;&NewLine; println&lpar;"I'm created with anonymous object"&rpar;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Anonymous object implementing a class<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example&colon;<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">open class Vehicle &lbrace;&NewLine;&NewLine; fun start&lpar;&rpar; &lbrace;&NewLine; println&lpar;"Vehicle is in start mode&period;"&rpar;&NewLine; &rcub;&NewLine;&NewLine; fun stop&lpar;&rpar; &lbrace;&NewLine; println&lpar;"Vehicle is in stop mode&period;"&rpar;&NewLine; &rcub;&NewLine;&NewLine; open fun park&lpar;&rpar; &lbrace;&NewLine; println&lpar;"Vehicle is in park mode&period;"&rpar;&NewLine; &rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;fun main&lpar;&rpar; &lbrace;&NewLine; val car &equals; object &colon; Vehicle&lpar;&rpar; &lbrace;&NewLine; override fun park&lpar;&rpar; &equals; println&lpar;"Car is in park mode&period;"&rpar;&NewLine; &rcub;&NewLine;&NewLine; car&period;start&lpar;&rpar;&NewLine; car&period;stop&lpar;&rpar;&NewLine; car&period;park&lpar;&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">Vehicle is in start mode&period;&NewLine;Vehicle is in stop mode&period;&NewLine;Car is in park mode&period;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">Here&comma; anonymous object is stored in variable <span style&equals;"color&colon; &num;008000&semi;"><strong>car<&sol;strong><&sol;span> which implements <strong><span style&equals;"color&colon; &num;008000&semi;">Vehicle<&sol;span><&sol;strong> class with <strong><span style&equals;"color&colon; &num;008000&semi;">park&lpar;&rpar;<&sol;span><&sol;strong> method is overridden&period;&NewLine;

Exit mobile version