Abstract type: Difference between revisions
Content added Content deleted
Line 1,422: | Line 1,422: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
Java has an |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 { |