Jump to content

Abstract type: Difference between revisions

Line 1,422:
 
=={{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''.
}</syntaxhighlight lang="java">
interface Example {
String stringA = "rosetta";
String stringB = "code";
 
private String methodA() {
<syntaxhighlight lang="java">public abstract class Abs {
return astringA + b" " + stringB;
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) {
Before Java 8, interfaces could not implement any methods and all methods were implicitly public and abstract.
return value + 100;
<syntaxhighlight lang="java">public interface Inter {
}
int method1(double value);
 
int method2(String name);
int addmethodC(int avalueA, int bvalueB);
}
}</syntaxhighlight>
And here is an example of its implementing class.
<syntaxhighlight lang="java">public interface Inter {
class ExampleImpl implements Example {
public abstract int method1methodB(doubleint value); {
return value + 200;
}
 
public int addmethodC(int avalueA, int bvalueB) {
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.
<syntaxhighlight lang="java">public abstract class Abs {
abstract class Example {
String stringA = "rosetta";
String stringB = "code";
 
private String methodA() {
return stringA + " " + stringB;
}
 
protected abstract int method2methodB(Stringint namevalue); {
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}}==
118

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.