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''.<br /> |
|||
Methods that don't have an implementation are called abstract methods in Java. A class that contains an abstract method or inherits one but did not override it must be an abstract class; but an abstract class does not need to contain any abstract methods. An abstract class cannot be instantiated. If a method is abstract, it cannot be private. |
|||
For an ''interface'', only the ''private'' and ''default'' 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 method with no access modifier is inherently ''public'', 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 /> |
|||
Here is an example of an ''interface''. |
|||
⚫ | |||
interface Example { |
|||
String stringA = "rosetta"; |
|||
String stringB = "code"; |
|||
private String methodA() { |
|||
<syntaxhighlight lang="java">public abstract class Abs { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
⚫ | |||
default int methodB(int value) { |
|||
Before Java 8, interfaces could not implement any methods and all methods were implicitly public and abstract. |
|||
return value + 100; |
|||
⚫ | |||
} |
|||
⚫ | |||
int method2(String name); |
|||
int |
int methodC(int valueA, int valueB); |
||
} |
|||
</syntaxhighlight> |
|||
And here is an example of its implementing class. |
|||
⚫ | |||
class ExampleImpl implements Example { |
|||
⚫ | |||
return value + 200; |
|||
} |
|||
⚫ | |||
return valueA + valueB; |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
The ''abstract class'' is very generalized, and for the most part is just a ''class'' that allows for un-implemented methods.<br /> |
|||
The ''default'' access modifier is not used here, as it applies only to an ''interface''.<br /> |
|||
Additionally, if a method is marked ''abstract'', then the ''private'' 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. |
|||
⚫ | |||
abstract class Example { |
|||
String stringA = "rosetta"; |
|||
String stringB = "code"; |
|||
private String methodA() { |
|||
return stringA + " " + stringB; |
|||
} |
|||
⚫ | |||
return value + 100; |
|||
} |
|||
public abstract int methodC(int valueA, int valueB); |
|||
} |
|||
</syntaxhighlight> |
|||
Here is an example of a class which ''extends'' an ''abstract class''. |
|||
<syntaxhighlight lang="java"> |
|||
public class ExampleImpl extends Example { |
|||
public int methodC(int valueA, int valueB) { |
|||
return valueA + valueB; |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|jq}}== |
=={{header|jq}}== |