Abstract type: Difference between revisions

Content added Content deleted
Line 1,422: Line 1,422:


=={{header|Java}}==
=={{header|Java}}==
Java has an ''interface'' and an ''abstract class''. Neither of which can be instantiated, and require some sort of implementation or abstraction.<br />
Java has an <code>interface</code> and an <code>abstract class</code>. Neither of which can be instantiated, and require some sort of implementation or abstraction.<br />
For an ''interface'', only the ''private'' and ''default'' access modifiers are allowed, which also implies they require code.<br />
For an <code>interface</code>, only the <code>private</code> and <code>default</code> access modifiers are allowed, which also implies they require code.<br />
A ''private'' method cannot be overridden by a sub-class, and a ''default'' method, optionally, can.<br />
A <code>private</code> method cannot be overridden by a sub-class, and a <code>default</code> method, optionally, can.<br />
A method with no access modifier is inherently ''public'', must not contain code, and requires implementation by its sub-class.<br />
A method with no access modifier is inherently <code>public</code>, must not contain code, and requires implementation by its sub-class.<br />
Member fields are allowed, although are effectively ''final'' and ''static'', thus requiring a value.<br />
Member fields are allowed, although are effectively <code>public</code>, <code>final</code>, and <code>static</code>, thus requiring a value.<br />
Here is an example of an ''interface''.
Here is an example of an <code>interface</code>.
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
interface Example {
interface Example {
Line 1,456: Line 1,456:
}
}
</syntaxhighlight>
</syntaxhighlight>
The ''abstract class'' is very generalized, and for the most part is just a ''class'' that allows for un-implemented methods.<br />
The <code>abstract class</code> is very generalized, and for the most part is just a <code>class</code> that allows for un-implemented methods.<br />
The ''default'' access modifier is not used here, as it applies only to an ''interface''.<br />
The <code>default</code> access modifier is not used here, as it applies only to an <code>interface</code>.<br />
Additionally, if a method is marked ''abstract'', then the ''private'' access modifier is not allowed, as the concept does not apply.<br />
Additionally, if a method is marked <code>abstract</code>, then the <code>private</code> access modifier is not allowed, as the concept does not apply.<br />
Here is an example of an ''abstract class''. If the class contains ''abstract'' methods then the class definition must also have the ''abstract'' keyword.
Here is an example of an <code>abstract class</code>.<br />
If the class contains <code>abstract</code> methods then the class definition must also have the <code>abstract</code> keyword.
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
abstract class Example {
abstract class Example {
Line 1,476: Line 1,477:
}
}
</syntaxhighlight>
</syntaxhighlight>
Here is an example of a class which ''extends'' an ''abstract class''.
Here is an example of a class which <code>extends</code> an <code>abstract class</code>.
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
class ExampleImpl extends Example {
class ExampleImpl extends Example {