Anki Deck Changes

Commit: c784f93b - add eprog cards

Author: obrhubr <obrhubr@gmail.com>

Date: 2026-01-25T22:59:10+01:00

Changes: 65 note(s) changed (65 added, 0 modified, 0 deleted)

Note 1: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: A{O/tsn?N%
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Upcasting is automatic.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Upcasting is automatic.
Field-by-field Comparison
Field Before After
Text Upcasting is {{c1:: automatic}}.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 2: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: C)hi{ye`0-
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::13._Interfaces
An 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

ETH::1._Semester::EProg::13._Interfaces
An 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>
Tags: ETH::1._Semester::EProg::13._Interfaces

Note 3: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: CGEw,Tn>.Y
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
The static type is the type that the compiler sees as assigned to the variable. The dynamic type is the runtime type.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
The static type is the type that the compiler sees as assigned to the variable. The dynamic type is the runtime type.
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.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 4: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: D,hN!b[~0a
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Dog[] dogs = new Dog[5];
Animal[] animals = dogs; // Allowed! (upcasting)
animals[0] = new Cat(); // Compiles but ArrayStoreException at runtime!

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
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>
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 5: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: E(5s[CJXSQ
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Casting to an interface only leads to a compile error for final classes.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Casting to an interface only leads to a compile error for final classes.

Then no subclass could implement the interface.
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.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 6: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: EYTdZt3/*5
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::12._Exceptions
Checked exceptions have to be announced or handled directly.

Back

ETH::1._Semester::EProg::12._Exceptions
Checked exceptions have to be announced or handled directly.

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>
Tags: ETH::1._Semester::EProg::12._Exceptions

Note 7: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: El$[?h8w;k
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::7._Classes_and_Objects
Attribute access modifiers:
  • 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

ETH::1._Semester::EProg::7._Classes_and_Objects
Attribute access modifiers:
  • 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&nbsp;<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>
Tags: ETH::1._Semester::EProg::7._Classes_and_Objects

Note 8: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: E|{Z<-t*y@
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::7._Classes_and_Objects
Private attributes are accessible by all objects of this class.

Back

ETH::1._Semester::EProg::7._Classes_and_Objects
Private attributes are accessible by all objects of this class.

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>
Tags: ETH::1._Semester::EProg::7._Classes_and_Objects

Note 9: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: F$o5lV^z=H
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::13._Interfaces
Note that a class can implement multiple interfaces.
If a method is defined in multiple of those interfaces, it of course has to implemented only once.

Back

ETH::1._Semester::EProg::13._Interfaces
Note that a class can implement multiple interfaces.
If a method is defined in multiple of those interfaces, it of course has to implemented only once.
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>
Tags: ETH::1._Semester::EProg::13._Interfaces

Note 10: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: FXUR__;!h6
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
When a function is called in dynamic dispatch, the attributes of the type in which the actual function is executed will be used.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
When a function is called in dynamic dispatch, the attributes of the type in which the actual function is executed will be used.

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>
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 11: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: FwBjd_dmb+
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance
Constructors are not inherited by subclasses.

Back

ETH::1._Semester::EProg::10._Inheritance
Constructors are not inherited by subclasses.
Field-by-field Comparison
Field Before After
Text Constructors are {{c1:: not inherited}} by subclasses.
Tags: ETH::1._Semester::EProg::10._Inheritance

Note 12: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: G&=W~#L|H<
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::12._Exceptions
There are checked and unchecked exceptions.

Back

ETH::1._Semester::EProg::12._Exceptions
There are checked and unchecked exceptions.
Field-by-field Comparison
Field Before After
Text There are {{c1:: checked}} and {{c1:: unchecked}} exceptions.
Tags: ETH::1._Semester::EProg::12._Exceptions

Note 13: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: G]t&7SvtKp
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
In a dynamic dispatch, the this still refers to the dynamic type, thus even if the method is in a superclass, it will always try to use the "most overriden" method.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
In a dynamic dispatch, the this still refers to the dynamic type, thus even if the method is in a superclass, it will always try to use the "most overriden" method.
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}}.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 14: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: H%0Tkaz:@S
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::13._Interfaces
Interfaces don't define behaviour, only method signatures.

Back

ETH::1._Semester::EProg::13._Interfaces
Interfaces don't define behaviour, only method signatures.
Field-by-field Comparison
Field Before After
Text Interfaces don't define behaviour, only {{c1:: method signatures}}.
Tags: ETH::1._Semester::EProg::13._Interfaces

Note 15: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: HI3~1]W{fH
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
if we have final Cat c = new Cat() the reference is immutable but the attributes of the class are not.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
if we have final Cat c = new Cat() the reference is immutable but the attributes of the class are not.
Field-by-field Comparison
Field Before After
Text if we have <b>final Cat c = new Cat()</b>&nbsp;the {{c1::reference}} is immutable but {{c2:: the attributes of the class are not}}.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 16: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Classic
GUID: HNxps|u:&w
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::7._Classes_and_Objects::2._Enums
Enums are special classes, they are initialised like this:

Back

ETH::1._Semester::EProg::7._Classes_and_Objects::2._Enums
Enums are special classes, they are initialised like this:

public enum Status {
    SUBMITTED,
    PAID,
    ...
}
Field-by-field Comparison
Field Before After
Front Enums are special classes, they are initialised like this:
Back public enum Status {<br>&nbsp;&nbsp;&nbsp; SUBMITTED,<br>&nbsp;&nbsp;&nbsp; PAID,<br>&nbsp;&nbsp;&nbsp; ...<br>}
Tags: ETH::1._Semester::EProg::7._Classes_and_Objects::2._Enums

Note 17: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: HY!(CO](X8
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Private methods are not inherited from the superclass and cannot be called (same as for private attributes).

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Private methods are not inherited from the superclass and cannot be called (same as for private attributes).
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)}}.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 18: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: Hz_:Sp*X(y
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
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

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
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>
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 19: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Image Occlusion-73a2c
GUID: IdA(dVgMPN
added

Previous

Note did not exist

New Note

Front

image-occlusion:rect:left=.8594:top=.1808:width=.1308:height=.1517:oi=1
image-occlusion:rect:left=.8584:top=.3622:width=.1308:height=.1369:oi=1
image-occlusion:rect:left=.8603:top=.5139:width=.127:height=.1295:oi=1
image-occlusion:rect:left=.8603:top=.6582:width=.1232:height=.1332:oi=1

Back

image-occlusion:rect:left=.8594:top=.1808:width=.1308:height=.1517:oi=1
image-occlusion:rect:left=.8584:top=.3622:width=.1308:height=.1369:oi=1
image-occlusion:rect:left=.8603:top=.5139:width=.127:height=.1295:oi=1
image-occlusion:rect:left=.8603:top=.6582:width=.1232:height=.1332:oi=1
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">
Tags: ETH::1._Semester::EProg::10._Inheritance::1._Visibility

Note 20: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: J`K7cc2By_
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
List<Dog> dogs = new ArrayList<>();
List<Animal> animals = (List<Animal>) dogs; // Unchecked cast warning

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
List<Dog> dogs = new ArrayList<>();
List<Animal> animals = (List<Animal>) dogs; // Unchecked cast warning

No Error because of type erasure here.
Field-by-field Comparison
Field Before After
Text <pre><code>List&lt;Dog&gt; dogs = new ArrayList&lt;&gt;(); List&lt;Animal&gt; animals = (List&lt;Animal&gt;) dogs; // {{c1::Unchecked cast warning}}</code></pre>
Extra No Error because of type erasure here.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 21: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Classic
GUID: KZF4,Q[f&D
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::12._Exceptions
We can catch exceptions using: 

Back

ETH::1._Semester::EProg::12._Exceptions
We can catch exceptions using: 

try { } catch (ExceptionName e) { };
Field-by-field Comparison
Field Before After
Front We can catch exceptions using:&nbsp;
Back <div><code>try { } catch (ExceptionName e) { };</code></div>
Tags: ETH::1._Semester::EProg::12._Exceptions

Note 22: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: K]ZyP_vxG;
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::7._Classes_and_Objects
Inside a class (except static functions) we have access to special variable  this and super.

Back

ETH::1._Semester::EProg::7._Classes_and_Objects
Inside a class (except static functions) we have access to special variable  this and super.
Field-by-field Comparison
Field Before After
Text Inside a class (except static functions) we have access to special variable {{c1::&nbsp;<b>this</b>&nbsp;and&nbsp;<b>super</b>}}.
Tags: ETH::1._Semester::EProg::7._Classes_and_Objects

Note 23: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: KnKuMTM<[8
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::7._Classes_and_Objects
A protected attribute can also be accessed by subclasses.

Back

ETH::1._Semester::EProg::7._Classes_and_Objects
A protected attribute can also be accessed by subclasses.
Field-by-field Comparison
Field Before After
Text A protected attribute can also be accessed by {{c1:: subclasses}}.
Tags: ETH::1._Semester::EProg::7._Classes_and_Objects

Note 24: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: LdyT3WH.QQ
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Casting to an interface (if not implemented) leads to a runtime error.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Casting to an interface (if not implemented) leads to a runtime error.

The compiler always assumes a subtype could implement the class.
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.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 25: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: MQ0U{K
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
We can access the parent's attribute of a subclass by casting to the static type of the parent.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
We can access the parent's attribute of a subclass by casting to the static type of the parent.
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}}.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 26: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: MqC&7Qly55
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
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

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
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 -&gt; Even though static type is A}} a2.fct1(); // {{c1:: 12 -&gt; Different output as a2's instance of A has 12}} b1.fct1(); // {{c1::1 -&gt; same here, even though it dynamic dispatches}} b2.fct1(); // {{c1::12}} </code></pre>
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 27: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Classic
GUID: N,~U9>@HR@
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance
How do we implement a subtype in Java?

Back

ETH::1._Semester::EProg::10._Inheritance
How do we implement a subtype in Java?

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>
Tags: ETH::1._Semester::EProg::10._Inheritance

Note 28: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: Nz^YQRPVQ2
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::7._Classes_and_Objects
Properties of classes initialised as public int x; are set to 0, null, false, etc... their default values.

Back

ETH::1._Semester::EProg::7._Classes_and_Objects
Properties of classes initialised as public int x; are set to 0, null, false, etc... their default values.
Field-by-field Comparison
Field Before After
Text Properties of classes initialised as&nbsp;<b>public int x;</b>&nbsp;are set to {{c1:: 0, null, false, etc... their default values}}.
Tags: ETH::1._Semester::EProg::7._Classes_and_Objects

Note 29: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: O:PCKgyN)+
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Java uses dynamic dispatch for function calls, we therefore get the method of the dynamic type.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Java uses dynamic dispatch for function calls, we therefore get the method of the dynamic type.
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}}.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 30: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: O@-k%ZU440
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance
Accessing the parent's parent's variables in Java is not allowed.

Back

ETH::1._Semester::EProg::10._Inheritance
Accessing the parent's parent's variables in Java is not allowed.
Field-by-field Comparison
Field Before After
Text Accessing the parent's parent's variables in Java is {{c1:: not allowed}}.
Tags: ETH::1._Semester::EProg::10._Inheritance

Note 31: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: OGFJDr03D}
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance
super.super.xxx, or x.super.xxx are not allowed an give a compile error.

Back

ETH::1._Semester::EProg::10._Inheritance
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>&nbsp;are not allowed an give a {{c1:: compile error}}.</div>
Tags: ETH::1._Semester::EProg::10._Inheritance

Note 32: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: OGFye&*Y@Y
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
The compiler uses the static type to get attributes.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
The compiler uses the static type to get attributes.
Field-by-field Comparison
Field Before After
Text The compiler uses the {{c1::static}} type to get attributes.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 33: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Classic
GUID: P6>K30cX4E
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::7._Classes_and_Objects
How could someone modify a private variable (aliasing)?

Back

ETH::1._Semester::EProg::7._Classes_and_Objects
How could someone modify a private variable (aliasing)?

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!
Tags: ETH::1._Semester::EProg::7._Classes_and_Objects

Note 34: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: Pi]A?I0a.)
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance
In a subclass, we can access the parent's methods and attributes using super.

Back

ETH::1._Semester::EProg::10._Inheritance
In a subclass, we can access the parent's methods and attributes using super.

You cannot use 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>
Tags: ETH::1._Semester::EProg::10._Inheritance

Note 35: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: QEMy5L&
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
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

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
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>
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 36: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: c>xF]nJjL8
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Animal cat = null; (Cat) cat; leads to a no error, this is always allowed.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
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>
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 37: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: dBkAd*&?D6
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
The dynamic type is always a subtype of the static type.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
The dynamic type is always a subtype of the static type.
Field-by-field Comparison
Field Before After
Text <div>The dynamic type is {{c1::<strong>always a subtype of the static type}}</strong>.</div>
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 38: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: dW?TP?RI{A
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
The dynamic change of a variable can be changed by reassigning the reference to another instance.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
The dynamic change of a variable can be changed by reassigning the reference to another instance.
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}}.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 39: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: f3eqMn1(f/
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Trying to access a method or attribute on a class with a different dynamic than static type leads to a compile error if the static type doesn't define that attribute or method.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Trying to access a method or attribute on a class with a different dynamic than static type leads to a compile error if the static type doesn't define that attribute or method.
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}}.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 40: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: fx//I5}Q?7
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Runtime Errors for Casting:
  1. (Husky) dog;: Casting further down than dynamic type
  2. (Cat) dog; : Casting into sibling type

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Runtime Errors for Casting:
  1. (Husky) dog;: Casting further down than dynamic type
  2. (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>
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 41: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: g%YZ@`35DU
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::1._Visibility
In a subclass, we can only make methods more visible .

Back

ETH::1._Semester::EProg::10._Inheritance::1._Visibility
In a subclass, we can only make methods more visible .

  • protected -> public is okay
  • private -> public okay
public to default  or default to private are not.
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 -&gt; public is okay</li><li>private -&gt; public okay</li></ul><div>public to default&nbsp; or default to private are not.</div>
Tags: ETH::1._Semester::EProg::10._Inheritance::1._Visibility

Note 42: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: g_DbKK.&&1
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Downcasting like (B) A will lead to runtime errors if A is not of type or subtype B.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Downcasting like (B) A will lead to runtime errors if A is not of type or subtype B.
Field-by-field Comparison
Field Before After
Text Downcasting like <b>(B) A</b>&nbsp;will lead to {{c1:: runtime errors}} if {{c2::A is not of type or subtype B}}.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 43: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: gc|oK]2yr^
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
We cannot override attributes inside a subclass they are shadowed.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
We cannot override attributes inside a subclass they are shadowed.
Field-by-field Comparison
Field Before After
Text We cannot override {{c1::attributes inside a subclass}} they are {{c1::shadowed}}.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 44: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: iyK[p{ZDF+
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
A a = new B() when calling a.test we get A’s test attribute.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
A a = new B() when calling a.test we get A’s test attribute.

The compiler uses the static type to get attributes.
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.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 45: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: iz]M]${2
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::7._Classes_and_Objects::2._Enums
Enums have some convenience features:
  • 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

ETH::1._Semester::EProg::7._Classes_and_Objects::2._Enums
Enums have some convenience features:
  • 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 &lt; Status.PAID</code>}}</li></ul>
Tags: ETH::1._Semester::EProg::7._Classes_and_Objects::2._Enums

Note 46: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: j>`+}@t/`y
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
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

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
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>
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 47: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: m5.*#}3,2{
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Casting Compile Errors:
  • 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

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Casting Compile Errors:
  • 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>&nbsp;Assigning static type of sibling or super-type to sub-type}}</li></ul>
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 48: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: mxWnHn=6?K
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::12._Exceptions
Unchecked Exceptions can but don't have to be caught.

Back

ETH::1._Semester::EProg::12._Exceptions
Unchecked Exceptions can but don't have to be caught.
Field-by-field Comparison
Field Before After
Text Unchecked Exceptions {{c1:: can but don't have to be}} caught.
Tags: ETH::1._Semester::EProg::12._Exceptions

Note 49: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: o!E/h>2m7B
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::1._Visibility
Private attributes cannot be accessed in subclasses, but we can still access them as a user through non-overriden methods.

Back

ETH::1._Semester::EProg::10._Inheritance::1._Visibility
Private attributes cannot be accessed in subclasses, but we can still access them as a user through non-overriden methods.
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}}.
Tags: ETH::1._Semester::EProg::10._Inheritance::1._Visibility

Note 50: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: oE~TL)}ZNN
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance
In java a function cannot inherit from multiple super-classes

Back

ETH::1._Semester::EProg::10._Inheritance
In java a function cannot inherit from multiple super-classes

This works only for interfaces.
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.
Tags: ETH::1._Semester::EProg::10._Inheritance

Note 51: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: p,3.jh@oZC
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::7._Classes_and_Objects
Shadowing is when an inner scope variable makes an outer scope one inaccessible.

Back

ETH::1._Semester::EProg::7._Classes_and_Objects
Shadowing is when an inner scope variable makes an outer scope one inaccessible.

public class Person {
    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>&nbsp;&nbsp;&nbsp; private String name;&nbsp; // instance variable<br><br>&nbsp;&nbsp;&nbsp; public void setName(String name) {&nbsp; // parameter shadows instance variable<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.name = name;&nbsp; // use 'this' to access the instance variable<br>&nbsp;&nbsp;&nbsp; }<br>}
Tags: ETH::1._Semester::EProg::7._Classes_and_Objects

Note 52: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: q+A$0CHp#W
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
If we override an attribute inherited from the subclass, it will override the parents attribute.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
If we override an attribute inherited from the subclass, it will override the parents attribute.

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 -&gt; Even though static type is A a2.fct1(); // 12 -&gt; Different output as a2's instance of A has 12 b1.fct1(); // 1 -&gt; same here, even though it dynamic dispatches b2.fct1(); // 12 </code></pre>
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 53: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: qj}+Vy$^iX
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::13._Interfaces
In an Interface: 
  • All methods are public (doesn’t have to be explicit).
  • Any attributes must be public and final.

Back

ETH::1._Semester::EProg::13._Interfaces
In an Interface: 
  • 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>&nbsp;</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>
Tags: ETH::1._Semester::EProg::13._Interfaces

Note 54: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: rcu~6HwhWg
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::7._Classes_and_Objects
Static methods can only access static attributes.

Back

ETH::1._Semester::EProg::7._Classes_and_Objects
Static methods can only access static attributes.
Field-by-field Comparison
Field Before After
Text Static methods can only access {{c1:: static}} attributes.
Tags: ETH::1._Semester::EProg::7._Classes_and_Objects

Note 55: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: rz>>y0Yl@}
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::12._Exceptions
An unchecked Exception extends RuntimeException and does not need to be announced nor caught.

Back

ETH::1._Semester::EProg::12._Exceptions
An unchecked Exception extends RuntimeException and does not need to be announced nor caught.
Field-by-field Comparison
Field Before After
Text An unchecked Exception extends {{c1:: RuntimeException}} and {{c2:: does not need to be announced nor caught}}.
Tags: ETH::1._Semester::EProg::12._Exceptions

Note 56: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: sV73UB+t=%
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::13._Interfaces
An interface can extend another interface.

Back

ETH::1._Semester::EProg::13._Interfaces
An interface can extend another interface.

Note that it extends not implements it.
Field-by-field Comparison
Field Before After
Text An interface can {{c1:: extend}} another interface.
Extra Note that it extends not implements it.
Tags: ETH::1._Semester::EProg::13._Interfaces

Note 57: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: s[]2mb-kIp
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
We can change the static type of a variable by casting.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
We can change the static type of a variable by casting.
Field-by-field Comparison
Field Before After
Text We can change the static type of a variable by {{c1:: casting}}.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 58: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: v:4|0s-7ML
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::13._Interfaces
All implementations of an interface using implements must instantiate all methods declared by the interface , otherwise we get a compiler error.

Back

ETH::1._Semester::EProg::13._Interfaces
All implementations of an interface using implements must instantiate all methods declared by the interface , otherwise we get a compiler error.

(except if the class is abstract, then we don't have to implement them)
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)
Tags: ETH::1._Semester::EProg::13._Interfaces

Note 59: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: w3Z$4;rQou
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
We cannot instantiate abstract classes and interfaces.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
We cannot instantiate abstract classes and interfaces.
Field-by-field Comparison
Field Before After
Text We cannot instantiate {{c1:: abstract classes}} and {{c2:: interfaces}}.
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 60: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: xnz4`TH(j}
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance
What methods cannot be overriden in a subclass:
  • static
  • final
  • private are not inherited either
  • constructors

Back

ETH::1._Semester::EProg::10._Inheritance
What methods cannot be overriden in a subclass:
  • static
  • final
  • private are not inherited either
  • constructors
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>
Tags: ETH::1._Semester::EProg::10._Inheritance

Note 61: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: yj/5Ksg%P1
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::7._Classes_and_Objects
A static attribute is unique amongst all imports . If we don't make it final it can be changed globally.

Back

ETH::1._Semester::EProg::7._Classes_and_Objects
A static attribute is unique amongst all imports . If we don't make it final it can be changed globally.

This means someone could change Math.PI for example.
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.
Tags: ETH::1._Semester::EProg::7._Classes_and_Objects

Note 62: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: yrm.]7QjhX
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::7._Classes_and_Objects
A static method cannot access this or other non-static class methods.

Back

ETH::1._Semester::EProg::7._Classes_and_Objects
A static method cannot access this or other non-static class methods.
Field-by-field Comparison
Field Before After
Text A static method cannot {{c1:: access this or other non-static class methods}}.
Tags: ETH::1._Semester::EProg::7._Classes_and_Objects

Note 63: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: y}<[=^7@`S
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::13._Interfaces
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

ETH::1._Semester::EProg::13._Interfaces
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>
Tags: ETH::1._Semester::EProg::13._Interfaces

Note 64: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: z*_4p
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
The Object type defines:
  • .equals(Object o)
  • .toString()

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
The Object type defines:
  • .equals(Object o)
  • .toString()
Field-by-field Comparison
Field Before After
Text The&nbsp;<b>Object</b>&nbsp;type defines:<br><ul><li>{{c1::<b>.equals(Object o)</b>}}</li><li>{{c2::<b>.toString()</b>}}</li></ul>
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic

Note 65: ETH::1. Semester::EProg

Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID: z=94+q]*#%
added

Previous

Note did not exist

New Note

Front

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Any type is a subtype of the  Object type.

Back

ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
Any type is a subtype of the  Object type.
Field-by-field Comparison
Field Before After
Text <div>Any type is a subtype of the {{c1::&nbsp;<b>Object</b>}} type.</div>
Tags: ETH::1._Semester::EProg::10._Inheritance::2._Polymorphism::3._Static_vs_Dynamic
↑ Top