Abstract type: Difference between revisions
→{{header|Java}}
Line 1,422:
=={{header|Java}}==
Java has an ''interface'' and an ''abstract class''.<br />
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() {
public abstract int method1(double value);▼
protected abstract int method2(String name);▼
int add(int a, int b) {▼
▲ return a + b;
}
▲}</syntaxhighlight>
default int methodB(int value) {
return value + 100;
<syntaxhighlight lang="java">public interface Inter {▼
}
▲ int method1(double value);
int
}
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}}==
|