3. public abstract class MyAbstractClass {
private int a = 0;
public int getSum(){
return a + getB();
}
protected int getA(){
return a;
}
abstract int getB();
}
public interface MyInterface {
int getDif();
}
4. public class MyClass extends MyAbstractClass implements MyInterface {
private int b = 1;
public int getB(){
return b;
}
public int getDif(){
return getA() - b;
}
}
public class MyClassChild extends MyClass implements Interface1, Interface2{
…
}
5. public static void main(String[] args) {
//MyAbstractClass aObject = new MyAbstractClass(); - ошибка
MyClass object1 = new MyClass();
int result1 = object1.getSum();
int result2 = object1.getDif();
doSomething(object1);
MyInterface object2 = new MyClass();
//int result3 = object2.getSum(); - ошибка
int result4 = object2.getDif();
doSomething(object2);
}
public static void doSomething(MyInterface object){
//MyClass object3 = object; - ошибка
//int result5 = object.getSum(); - ошибка
int result6 = object.getDif();
}
13. public class GenericExample<T> {
private T value;
public GenericExample(T value){
this.value = value;
}
public T getT(){
return value;
}
public static void main(String[] args) {
GenericExample<Integer> intObject = new GenericExample<Integer>(1);
Integer valueInteger = intObject.getT();
GenericExample<String> stringObject = new GenericExample<String>("word");
String valueString = stringObject.getT();
}
}
14. public class LongId<T> {
private long id;
public LongId(long id){
this.id = id;
}
public long getLong(){
return id;
}
}
public void manyIdsInParams(long userId, long serverId, long adress){…}
public void manyIdsInParams(LongId<User> userId,
LongId<Server> serverId,
LongId<Adress> adress){…}
24. public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new Thread(new HelloRunnable())).start();
}
}
25. public class HelloThread extends Thread {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new HelloThread()).start();
}
}
27. try {
Thread.sleep(5000);
} catch (InterruptedException e) { // We've been interrupted.
return;
}
for (int i = 0; i < inputs.length; i++) {
heavyTask(inputs[i]);
if (Thread.interrupted()) { // We've been interrupted.
return;
}
}
28. public class HelloThread extends Thread {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
Thread thread = new HelloThread();
thread.start();
thread.join();
System.out.println("Hello from the main!");
}
}
29. public class HelloThread extends Thread {
private StringBuilder builder;
public HelloThread(StringBuilder builder){
this.builder = builder;
}
public void run() {
builder.append("Hello from a thread!");
}
public static void main(String args[]) {
StringBuilder builder = new StringBuilder();
Thread thread = new HelloThread();
thread.start();
builder.append("Hello from the main!");
}
}