Note 1: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
A{O/tsn?N%
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Upcasting is {{c1:: automatic}}. |
Note 2: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
C)hi{ye`0-
Previous
Note did not exist
New Note
Front
interface defines a set of methods that any class that implements it has to have. We use class Test implements Tester {}; to define such a relationship.Back
interface defines a set of methods that any class that implements it has to have. We use class Test implements Tester {}; to define such a relationship.Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <div>An <code>interface</code> defines {{c1::a set of methods that any class that implements it has to have}}. We use <code>class Test implements Tester {};</code> to define such a relationship.</div> |
Note 3: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
CGEw,Tn>.Y
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | The {{c1::static}} type is the type that the compiler sees as assigned to the variable. The {{c2::dynamic}} type is the runtime type. |
Note 4: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
D,hN!b[~0a
Previous
Note did not exist
New Note
Front
Dog[] dogs = new Dog[5];
Animal[] animals = dogs; // Allowed! (upcasting)
animals[0] = new Cat(); // Compiles but ArrayStoreException at runtime!
Back
Dog[] dogs = new Dog[5];
Animal[] animals = dogs; // Allowed! (upcasting)
animals[0] = new Cat(); // Compiles but ArrayStoreException at runtime!
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <pre><code>Dog[] dogs = new Dog[5]; Animal[] animals = dogs; // {{c1::Allowed! (upcasting)}} animals[0] = new Cat(); // {{c2::Compiles but ArrayStoreException at runtime!}} </code></pre> |
Note 5: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
E(5s[CJXSQ
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Casting to an interface only leads to a compile error for {{c1::final classes}}. | |
| Extra | Then no subclass could implement the interface. |
Note 6: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
EYTdZt3/*5
Previous
Note did not exist
New Note
Front
Back
public void throwsStuff() throws CheckedException {
throws CheckedException(); // Allowed
}
public void throwsStuff() {
try {
throwsStuff();
} catch (Exception e) { }; // Allowed
}
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Checked exceptions have to be {{c1:: announced or handled directly}}. | |
| Extra | <pre><code>public void throwsStuff() throws CheckedException { throws CheckedException(); // Allowed } public void throwsStuff() { try { throwsStuff(); } catch (Exception e) { }; // Allowed } </code></pre> |
Note 7: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
El$[?h8w;k
Previous
Note did not exist
New Note
Front
- default: package scoped, only available in same package.
- public: by everyone
- private: only accessible from within the object itself (not shared with other instances, unlike static)
- static:: no initialisation needed, can be accessed through Math.PI
- final: prevents overwriting, like const
- protected: only by this class and subclasses
Back
- default: package scoped, only available in same package.
- public: by everyone
- private: only accessible from within the object itself (not shared with other instances, unlike static)
- static:: no initialisation needed, can be accessed through Math.PI
- final: prevents overwriting, like const
- protected: only by this class and subclasses
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Attribute access modifiers:<br><ul><li>default: {{c1:: package scoped, only available in same package.}}</li><li>public: {{c2:: by everyone}}</li><li>private: {{c3:: only accessible from within the object itself (not shared with other instances, unlike static)}}</li><li>static:: {{c4:: no initialisation needed, can be accessed through <b>Math.PI</b>}}<br></li><li>final: {{c5:: prevents overwriting, like const}}</li><li>protected: {{c6:: only by this class and subclasses}}</li></ul> |
Note 8: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
E|{Z<-t*y@
Previous
Note did not exist
New Note
Front
Back
public class Rational {
private int x;
// Copy constructor
public Rational(Rational other) {
this.x = other.x; // We CAN access another objects private attributes
}
}
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Private attributes are accessible by {{c1:: all objects of this class}}. | |
| Extra | <pre><code>public class Rational { private int x; // Copy constructor public Rational(Rational other) { this.x = other.x; // We CAN access another objects private attributes } } </code></pre> |
Note 9: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
F$o5lV^z=H
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <div>Note that a class can<b> </b>{{c1::<b>implement multiple interfaces</b>}}.</div><div>If a method is defined in {{c1::multiple of those interfaces}}, it {{c1::of course has to implemented only once}}.</div> |
Note 10: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
FXUR__;!h6
Previous
Note did not exist
New Note
Front
Back
class T {
int data = 50;
public void s2() { System.out.println("T " + data); }
}
class S extends T {
int data = 100;
public void s2() { System.out.println(this.data); }
}
class R extends S {
int data = 200;
}
T r = new R();
r.s2(); // Prints "100"
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | When a function is called in dynamic dispatch, the attributes of {{c1::the type in which the actual function is executed}} will be used. | |
| Extra | <pre><code>class T { int data = 50; public void s2() { System.out.println("T " + data); } } class S extends T { int data = 100; public void s2() { System.out.println(this.data); } } class R extends S { int data = 200; } T r = new R(); r.s2(); // Prints "100" </code></pre> |
Note 11: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
FwBjd_dmb+
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Constructors are {{c1:: not inherited}} by subclasses. |
Note 12: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
G&=W~#L|H<
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | There are {{c1:: checked}} and {{c1:: unchecked}} exceptions. |
Note 13: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
G]t&7SvtKp
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | In a dynamic dispatch, the this still refers to the dynamic type, thus {{c1:: even if the method is in a superclass, it will always try to use the "most overriden" method}}. |
Note 14: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
H%0Tkaz:@S
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Interfaces don't define behaviour, only {{c1:: method signatures}}. |
Note 15: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
HI3~1]W{fH
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | if we have <b>final Cat c = new Cat()</b> the {{c1::reference}} is immutable but {{c2:: the attributes of the class are not}}. |
Note 16: ETH::1. Semester::EProg
Note Type: Horvath Classic
GUID:
HNxps|u:&w
Previous
Note did not exist
New Note
Front
Back
SUBMITTED,
PAID,
...
}
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Front | Enums are special classes, they are initialised like this: | |
| Back | public enum Status {<br> SUBMITTED,<br> PAID,<br> ...<br>} |
Note 17: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
HY!(CO](X8
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Private methods are {{c1::not inherited}} from the superclass and {{c1::cannot be called (same as for private attributes)}}. |
Note 18: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
Hz_:Sp*X(y
Previous
Note did not exist
New Note
Front
class A {
public int x = 5;
}
class B extends A {
public int y = 6;
public int test() { return 0; }
}
A a1 = new B(1);
B.y; // leads to a compile error, as A doesn't have x
B.test() // leads to a compile error, as A doesn't have test()Back
class A {
public int x = 5;
}
class B extends A {
public int y = 6;
public int test() { return 0; }
}
A a1 = new B(1);
B.y; // leads to a compile error, as A doesn't have x
B.test() // leads to a compile error, as A doesn't have test()Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <pre><code>class A { public int x = 5; } class B extends A { public int y = 6; public int test() { return 0; } </code></pre><pre><code>} A a1 = new B(1); B.y; // {{c1:: leads to a compile error, as A doesn't have x}} B.test() // </code>{{c2:: leads to a compile error, as A doesn't have test()}}</pre> |
Note 19: ETH::1. Semester::EProg
Note Type: Image Occlusion-73a2c
GUID:
IdA(dVgMPN
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Occlusion | {{c1::image-occlusion:rect:left=.8594:top=.1808:width=.1308:height=.1517:oi=1}}<br>{{c2::image-occlusion:rect:left=.8584:top=.3622:width=.1308:height=.1369:oi=1}}<br>{{c3::image-occlusion:rect:left=.8603:top=.5139:width=.127:height=.1295:oi=1}}<br>{{c4::image-occlusion:rect:left=.8603:top=.6582:width=.1232:height=.1332:oi=1}}<br> | |
| Image | <img src="paste-717272a5d9a6b814701d81c48dd95ec99a540e2e.jpg"> |
Note 20: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
J`K7cc2By_
Previous
Note did not exist
New Note
Front
List<Dog> dogs = new ArrayList<>();
List<Animal> animals = (List<Animal>) dogs; // Unchecked cast warningBack
List<Dog> dogs = new ArrayList<>();
List<Animal> animals = (List<Animal>) dogs; // Unchecked cast warningField-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <pre><code>List<Dog> dogs = new ArrayList<>(); List<Animal> animals = (List<Animal>) dogs; // {{c1::Unchecked cast warning}}</code></pre> | |
| Extra | No Error because of type erasure here. |
Note 21: ETH::1. Semester::EProg
Note Type: Horvath Classic
GUID:
KZF4,Q[f&D
Previous
Note did not exist
New Note
Front
Back
try { } catch (ExceptionName e) { };Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Front | We can catch exceptions using: | |
| Back | <div><code>try { } catch (ExceptionName e) { };</code></div> |
Note 22: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
K]ZyP_vxG;
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Inside a class (except static functions) we have access to special variable {{c1:: <b>this</b> and <b>super</b>}}. |
Note 23: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
KnKuMTM<[8
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | A protected attribute can also be accessed by {{c1:: subclasses}}. |
Note 24: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
LdyT3WH.QQ
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Casting to an interface (if not implemented) leads to a {{c1:: runtime error}}. | |
| Extra | The compiler always assumes a subtype could implement the class. |
Note 25: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
MQ0U{K
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | We can access the parent's attribute of a subclass by {{c1:: casting to the static type of the parent}}. |
Note 26: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
MqC&7Qly55
Previous
Note did not exist
New Note
Front
class A {
public int x = 5;
public void fct1() {
System.out.println(this.x);
}
}
class B extends A {
public B() {
super.x = 10;
}
public B(int a) {
super.x = a;
}
}
A a1 = new B(1);
A a2 = new B(12); // As these are different instances, their attributes are separate
B b1 = new B(1);
B b2 = new B(12);
a1.fct1(); // 1 -> Even though static type is A
a2.fct1(); // 12 -> Different output as a2's instance of A has 12
b1.fct1(); // 1 -> same here, even though it dynamic dispatches
b2.fct1(); // 12
Back
class A {
public int x = 5;
public void fct1() {
System.out.println(this.x);
}
}
class B extends A {
public B() {
super.x = 10;
}
public B(int a) {
super.x = a;
}
}
A a1 = new B(1);
A a2 = new B(12); // As these are different instances, their attributes are separate
B b1 = new B(1);
B b2 = new B(12);
a1.fct1(); // 1 -> Even though static type is A
a2.fct1(); // 12 -> Different output as a2's instance of A has 12
b1.fct1(); // 1 -> same here, even though it dynamic dispatches
b2.fct1(); // 12
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <pre><code>class A { public int x = 5; public void fct1() { System.out.println(this.x); } } class B extends A { public B() { super.x = 10; } public B(int a) { super.x = a; } } A a1 = new B(1); A a2 = new B(12); // As these are different instances, their attributes are separate B b1 = new B(1); B b2 = new B(12); a1.fct1(); // {{c1::1 -> Even though static type is A}} a2.fct1(); // {{c1:: 12 -> Different output as a2's instance of A has 12}} b1.fct1(); // {{c1::1 -> same here, even though it dynamic dispatches}} b2.fct1(); // {{c1::12}} </code></pre> |
Note 27: ETH::1. Semester::EProg
Note Type: Horvath Classic
GUID:
N,~U9>@HR@
Previous
Note did not exist
New Note
Front
Back
public class Lorenz extends Students {
@Override
public void someMethod() {
...
super.someMethod(); // Can call super's implementation of this
}
}
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Front | How do we implement a subtype in Java? | |
| Back | <pre><code>public class Lorenz extends Students { @Override public void someMethod() { ... super.someMethod(); // Can call super's implementation of this } } </code></pre> |
Note 28: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
Nz^YQRPVQ2
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Properties of classes initialised as <b>public int x;</b> are set to {{c1:: 0, null, false, etc... their default values}}. |
Note 29: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
O:PCKgyN)+
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Java uses {{c1:: dynamic dispatch}} for function calls, we therefore get the method of the {{c1:: dynamic type}}. |
Note 30: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
O@-k%ZU440
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Accessing the parent's parent's variables in Java is {{c1:: not allowed}}. |
Note 31: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
OGFJDr03D}
Previous
Note did not exist
New Note
Front
super.super.xxx, or x.super.xxx are not allowed an give a compile error.Back
super.super.xxx, or x.super.xxx are not allowed an give a compile error.Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <div><code>super.super.xxx</code>, or <code>x.super.xxx</code> are not allowed an give a {{c1:: compile error}}.</div> |
Note 32: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
OGFye&*Y@Y
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | The compiler uses the {{c1::static}} type to get attributes. |
Note 33: ETH::1. Semester::EProg
Note Type: Horvath Classic
GUID:
P6>K30cX4E
Previous
Note did not exist
New Note
Front
Back
public class AliasingProblem {
private Point center; // Problematic - exposes internal reference
public Point getCenter() {
return center; // Returns alias to internal object
}
}
main() {
Point center = new AliasingProblem().getCenter();
center.setX(2); // Changes private attribute!!
}
If we get a reference to a private object, we can indeed change it's values!Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Front | How could someone modify a private variable (aliasing)? | |
| Back | <pre><code>public class AliasingProblem { private Point center; // Problematic - exposes internal reference public Point getCenter() { return center; // Returns alias to internal object } } main() { Point center = new AliasingProblem().getCenter(); center.setX(2); // Changes private attribute!! }</code> </pre> If we get a reference to a private object, we can indeed change it's values! |
Note 34: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
Pi]A?I0a.)
Previous
Note did not exist
New Note
Front
Back
super.super.xxx, nor x.super.xxx to access super methods or attributes. This gives a compile error.Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | In a subclass, we can access the parent's methods and attributes using {{c1:: super}}. | |
| Extra | <div>You cannot use <code>super.super.xxx</code>, nor <code>x.super.xxx</code> to access super methods or attributes. This gives a <strong>compile</strong> error.</div> |
Note 35: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
QEMy5L&
Previous
Note did not exist
New Note
Front
class A { int x; }
class B extends A { boolean x; }
A a = new A();
a.x = 6; // OK
a.x = true; // Compile error, type mismatch
A a = new B();
a.x = 6; // OK
a.x = true; // Compile error, type mismatch because static type is used
B b = new B();
b.x = 6; // Type Error
b.x = true; // OK!
Back
class A { int x; }
class B extends A { boolean x; }
A a = new A();
a.x = 6; // OK
a.x = true; // Compile error, type mismatch
A a = new B();
a.x = 6; // OK
a.x = true; // Compile error, type mismatch because static type is used
B b = new B();
b.x = 6; // Type Error
b.x = true; // OK!
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <pre><code>class A { int x; } class B extends A { boolean x; } A a = new A(); a.x = 6; // {{c1:: OK}} a.x = true; // {{c1:: Compile error, type mismatch}} A a = new B(); a.x = 6; // {{c2:: OK}} a.x = true; // {{c2:: Compile error, type mismatch because static type is used}} B b = new B(); b.x = 6; // {{c3::Type Error}} b.x = true; // {{c3::OK!}} </code></pre> |
Note 36: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
c>xF]nJjL8
Previous
Note did not exist
New Note
Front
Animal cat = null; (Cat) cat; leads to a no error, this is always allowed.Back
Animal cat = null; (Cat) cat; leads to a no error, this is always allowed.Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <div><code>Animal cat = null; (Cat) cat;</code> leads to a {{c1:: no error, this is always allowed}}.</div> |
Note 37: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
dBkAd*&?D6
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <div>The dynamic type is {{c1::<strong>always a subtype of the static type}}</strong>.</div> |
Note 38: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
dW?TP?RI{A
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | The dynamic change of a variable can be changed by {{c1:: reassigning the reference to another instance}}. |
Note 39: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
f3eqMn1(f/
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Trying to access a method or attribute on a class with a different dynamic than static type leads to a compile error if {{c1:: the static type doesn't define that attribute or method}}. |
Note 40: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
fx//I5}Q?7
Previous
Note did not exist
New Note
Front
(Husky) dog;: Casting further down than dynamic type(Cat) dog;: Casting into sibling type
Back
(Husky) dog;: Casting further down than dynamic type(Cat) dog;: Casting into sibling type
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Runtime Errors for Casting:<br><ol> <li><code>{{c1:: (Husky) dog;</code>: Casting further down than dynamic type}}</li> <li><code>{{c2:: (Cat) dog;</code> : Casting into sibling type}}</li></ol> |
Note 41: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
g%YZ@`35DU
Previous
Note did not exist
New Note
Front
Back
- protected -> public is okay
- private -> public okay
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | In a subclass, we can only make methods {{c1:: more visible :: access modifiers}}. | |
| Extra | <ul><li>protected -> public is okay</li><li>private -> public okay</li></ul><div>public to default or default to private are not.</div> |
Note 42: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
g_DbKK.&&1
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Downcasting like <b>(B) A</b> will lead to {{c1:: runtime errors}} if {{c2::A is not of type or subtype B}}. |
Note 43: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
gc|oK]2yr^
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | We cannot override {{c1::attributes inside a subclass}} they are {{c1::shadowed}}. |
Note 44: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
iyK[p{ZDF+
Previous
Note did not exist
New Note
Front
A a = new B() when calling a.test we get A’s test attribute.Back
A a = new B() when calling a.test we get A’s test attribute.Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <div><code>A a = new B()</code> when calling <code>a.test</code> we get {{c1::<code>A}}</code>’s test attribute.</div> | |
| Extra | The compiler uses the static type to get attributes. |
Note 45: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
iz]M]${2
Previous
Note did not exist
New Note
Front
- can compare using
== - use
.name()to get string representation - go from string to enum using
Status.valueOf("PAID") - Ordering using order in declaration
Status.SUBMITTED < Status.PAID
Back
- can compare using
== - use
.name()to get string representation - go from string to enum using
Status.valueOf("PAID") - Ordering using order in declaration
Status.SUBMITTED < Status.PAID
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Enums have some convenience features:<br><ul><li>{{c1::can compare using <code>==}}</code></li> <li>{{c2::use <code>.name()</code> to get string representation}}</li> <li>{{c3::go from string to enum using <code>Status.valueOf("PAID")}}</code></li> <li>{{c4:: Ordering using order in declaration <code>Status.SUBMITTED < Status.PAID</code>}}</li></ul> |
Note 46: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
j>`+}@t/`y
Previous
Note did not exist
New Note
Front
class T {
int data = 50;
public void s2() { System.out.println("T " + data); }
}
class S extends T {
int data = 100;
public void s2() { System.out.println(this.data); }
}
class R extends S {
int data = 200;
}
T r = new R();
r.s2(); // Prints "100"
Back
class T {
int data = 50;
public void s2() { System.out.println("T " + data); }
}
class S extends T {
int data = 100;
public void s2() { System.out.println(this.data); }
}
class R extends S {
int data = 200;
}
T r = new R();
r.s2(); // Prints "100"
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <pre><code>class T { int data = 50; public void s2() { System.out.println("T " + data); } } class S extends T { int data = 100; public void s2() { System.out.println(this.data); } } class R extends S { int data = 200; } T r = new R(); r.s2(); // Prints {{c1::"100"}} </code></pre> |
Note 47: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
m5.*#}3,2{
Previous
Note did not exist
New Note
Front
- Cat c = new Cat(); Dog d = (Dog) c; Casting Static type Cat to Dog as they’re siblings.
- Cat c = (Dog) d; Assigning static type of sibling or super-type to sub-type
Back
- Cat c = new Cat(); Dog d = (Dog) c; Casting Static type Cat to Dog as they’re siblings.
- Cat c = (Dog) d; Assigning static type of sibling or super-type to sub-type
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Casting Compile Errors:<br><ul><li>{{c1::<b>Cat c = new Cat(); Dog d = (Dog) c;</b> Casting Static type <b>Cat</b> to <b>Dog</b> as they’re siblings.}}</li><li>{{c2::<b>Cat c = (Dog) d;</b> Assigning static type of sibling or super-type to sub-type}}</li></ul> |
Note 48: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
mxWnHn=6?K
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Unchecked Exceptions {{c1:: can but don't have to be}} caught. |
Note 49: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
o!E/h>2m7B
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Private attributes cannot be accessed in subclasses, but we can still access them as a user through {{c1:: non-overriden methods}}. |
Note 50: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
oE~TL)}ZNN
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | In java a function cannot inherit from {{c1:: multiple super-classes}} | |
| Extra | This works only for interfaces. |
Note 51: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
p,3.jh@oZC
Previous
Note did not exist
New Note
Front
Back
private String name; // instance variable
public void setName(String name) { // parameter shadows instance variable
this.name = name; // use 'this' to access the instance variable
}
}
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Shadowing is when an {{c1:: inner scope variable makes an outer scope one inaccessible}}. | |
| Extra | public class Person {<br> private String name; // instance variable<br><br> public void setName(String name) { // parameter shadows instance variable<br> this.name = name; // use 'this' to access the instance variable<br> }<br>} |
Note 52: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
q+A$0CHp#W
Previous
Note did not exist
New Note
Front
Back
class A {
public int x = 5;
public void fct1() {
System.out.println(this.x);
}
}
class B extends A {
public B() {
super.x = 10;
}
public B(int a) {
super.x = a;
}
}
A a1 = new B(1);
A a2 = new B(12); // As these are different instances, their attributes are separate
B b1 = new B(1);
B b2 = new B(12);
a1.fct1(); // 1 -> Even though static type is A
a2.fct1(); // 12 -> Different output as a2's instance of A has 12
b1.fct1(); // 1 -> same here, even though it dynamic dispatches
b2.fct1(); // 12
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | If we override an attribute inherited from the subclass, it will {{c1::override}} the parents attribute. | |
| Extra | <pre><code>class A { public int x = 5; public void fct1() { System.out.println(this.x); } } class B extends A { public B() { super.x = 10; } public B(int a) { super.x = a; } } A a1 = new B(1); A a2 = new B(12); // As these are different instances, their attributes are separate B b1 = new B(1); B b2 = new B(12); a1.fct1(); // 1 -> Even though static type is A a2.fct1(); // 12 -> Different output as a2's instance of A has 12 b1.fct1(); // 1 -> same here, even though it dynamic dispatches b2.fct1(); // 12 </code></pre> |
Note 53: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
qj}+Vy$^iX
Previous
Note did not exist
New Note
Front
- All methods are public (doesn’t have to be explicit).
- Any attributes must be public and final.
Back
- All methods are public (doesn’t have to be explicit).
- Any attributes must be public and final.
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <div>In an <b>Interface:</b> </div><div><ul><li>All methods are {{c1::public (doesn’t have to be explicit).}}</li><li>Any attributes {{c2::<b>must be public and final</b>.}}</li></ul></div> |
Note 54: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
rcu~6HwhWg
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Static methods can only access {{c1:: static}} attributes. |
Note 55: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
rz>>y0Yl@}
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | An unchecked Exception extends {{c1:: RuntimeException}} and {{c2:: does not need to be announced nor caught}}. |
Note 56: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
sV73UB+t=%
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | An interface can {{c1:: extend}} another interface. | |
| Extra | Note that it extends not implements it. |
Note 57: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
s[]2mb-kIp
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | We can change the static type of a variable by {{c1:: casting}}. |
Note 58: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
v:4|0s-7ML
Previous
Note did not exist
New Note
Front
implements must instantiate all methods declared by the interface , otherwise we get a compiler error.Back
implements must instantiate all methods declared by the interface , otherwise we get a compiler error.Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <div>All implementations of an interface using <code>implements</code> must instantiate {{c1::<strong>all</strong> methods}} declared by the interface , otherwise we get {{c1::a <strong>compiler error}}</strong>.</div> | |
| Extra | (except if the class is abstract, then we don't have to implement them) |
Note 59: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
w3Z$4;rQou
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | We cannot instantiate {{c1:: abstract classes}} and {{c2:: interfaces}}. |
Note 60: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
xnz4`TH(j}
Previous
Note did not exist
New Note
Front
staticfinalprivateare not inherited eitherconstructors
Back
staticfinalprivateare not inherited eitherconstructors
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | What methods cannot be overriden in a subclass:<br><ul><li><code>{{c1::static}}</code></li> <li><code>{{c2::final}}</code></li> <li><code>{{c3::private</code> are not inherited either}}</li> <li><code>{{c4::constructors}}</code></li></ul> |
Note 61: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
yj/5Ksg%P1
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | A static attribute is {{c1:: unique amongst all imports :: property}}. If we don't make it {{c2:: final}} it can {{c2::be changed globally}}. | |
| Extra | This means someone could change Math.PI for example. |
Note 62: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
yrm.]7QjhX
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | A static method cannot {{c1:: access this or other non-static class methods}}. |
Note 63: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
y}<[=^7@`S
Previous
Note did not exist
New Note
Front
interface B {
Object getValue();
void process() throws IOException;
}
interface A extends B {
// 1. Covariant return type - more specific than Object
String getValue(); // Valid override
// 2. Can reduce or eliminate exceptions
void process(); // Valid - removes IOException
// 3. Can add default implementation
default String getValue() {
return "default";
}
}
Back
interface B {
Object getValue();
void process() throws IOException;
}
interface A extends B {
// 1. Covariant return type - more specific than Object
String getValue(); // Valid override
// 2. Can reduce or eliminate exceptions
void process(); // Valid - removes IOException
// 3. Can add default implementation
default String getValue() {
return "default";
}
}
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <pre><code>interface B { Object getValue(); void process() throws IOException; } interface A extends B { // {{c1::1. Covariant return type - more specific than Object}} String getValue(); // {{c1::Valid override}} // {{c2::2. Can reduce or eliminate exceptions}} void process(); // {{c2::Valid - removes IOException}} // {{c3::3. Can add default implementation}} {{c3::default}} String getValue() { return "default"; } } </code></pre> |
Note 64: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
z*_4p
Previous
Note did not exist
New Note
Front
- .equals(Object o)
- .toString()
Back
- .equals(Object o)
- .toString()
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | The <b>Object</b> type defines:<br><ul><li>{{c1::<b>.equals(Object o)</b>}}</li><li>{{c2::<b>.toString()</b>}}</li></ul> |
Note 65: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
z=94+q]*#%
Previous
Note did not exist
New Note
Front
Back
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | <div>Any type is a subtype of the {{c1:: <b>Object</b>}} type.</div> |