FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Commit 4222baf6 authored by Andrew Rice's avatar Andrew Rice
Browse files

lecture 7

parent aa4bebd4
No related branches found
No related tags found
No related merge requests found
Showing
with 197 additions and 0 deletions
package uk.ac.cam.acr31.oop.democode1920.lecture7;
class Point {
private final int x, y;
private final String name;
Point(int x, int y) {
this.x = x;
this.y = y;
name = makeName();
}
protected String makeName() {
return "[" + x + "," + y + "]";
}
public final String toString() {
return name;
}
}
public class ColorPoint extends Point {
private final String color;
ColorPoint(int x, int y, String color) {
super(x, y);
this.color = color;
}
protected String makeName() {
return super.makeName() + ":" + color;
}
public static void main(String[] args) {
System.out.println(new ColorPoint(4, 2, "purple"));
}
}
package uk.ac.cam.acr31.oop.democode1920.lecture7;
class ConstructedObject extends SuperObject {
private static final String CONSTANT = print("CONSTANT");
static {
print("STATIC INITIALIZER");
}
private final String instance = print("instance");
ConstructedObject() {
super(print("hello"));
print("CONSTRUCTOR");
}
private static final String CONSTANT2 = print("CONSTANT2");
private final String instance2 = print("instance2");
static String print(String message) {
System.out.println("ConstructedObject." + message);
return null;
}
}
package uk.ac.cam.acr31.oop.democode1920.lecture7;
public class DynamicPolymorphism {
static class A {
String get = "A";
String get() {
return "A";
}
}
static class B extends A {
String get = "B";
String get() {
return "B";
}
}
static void print(A value) {
System.out.println(value.get);
System.out.println(value.get());
}
public static void main(String[] args) {
print(new B());
}
}
package uk.ac.cam.acr31.oop.democode1920.lecture7;
public class Finalizer {
@Override
protected void finalize() throws Throwable {
super.finalize();
System.out.println("I'm free!");
}
public static void main(String[] args) {
for (int i = 0; i < 1000000; i++) {
Finalizer f = new Finalizer();
}
}
}
package uk.ac.cam.acr31.oop.democode1920.lecture7;
public class InitializationOrder {
public static void main(String[] args) {
ConstructedObject constructedObject = new ConstructedObject();
}
}
package uk.ac.cam.acr31.oop.democode1920.lecture7;
import java.util.ArrayList;
import java.util.List;
public class Leak {
private static List<Leak> l;
public static void main(String[] args) {
l = new ArrayList<>();
for (int i = 0; i < 1000000000; i++) {
l.add(new Leak());
}
}
}
package uk.ac.cam.acr31.oop.democode1920.lecture7;
class SuperObject {
private static final String CONSTANT = print("CONSTANT");
static {
print("STATIC INITIALIZER");
}
private final String instance = print("instance");
SuperObject(String arg) {
print("CONSTRUCTOR");
}
private static final String CONSTANT2 = print("CONSTANT2");
private final String instance2 = print("instance2");
static String print(String message) {
System.out.println("SuperObject." + message);
return null;
}
}
package uk.ac.cam.acr31.oop.democode1920.lecture7;
class This {
private final int i;
This() {
this(4);
}
This(int i) {
this.i = i;
}
}
package uk.ac.cam.acr31.oop.democode1920.lecture7;
import java.io.Closeable;
public class TryWithResources implements Closeable {
TryWithResources() {
System.out.println("Acquire");
}
public static void main(String[] args) {
System.out.println("start");
try (TryWithResources t = new TryWithResources()) {
System.out.println("execute");
// object exists
}
System.out.println("done");
// object still exists - but close will have been called
}
@Override
public void close() {
System.out.println("release");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment