We cannot override attributes inside a subclass they are shadowed.
Note 1: ETH::1. Semester::EProg
Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
modified
Note Type: Horvath Cloze
GUID:
gc|oK]2yr^
Before
Front
Back
We cannot override attributes inside a subclass they are shadowed.
After
Front
We cannot override attributes inside a subclass, they are shadowed.
Back
We cannot override attributes inside a subclass, they are shadowed.
class Animal {
String name = "Animal";
String getName() {
return "Animal";
}
}
class Dog extends Animal {
String name = "Dog"; // Shadows Animal.name (doesn't override it)
@Override String getName() { return Dog"; } // Overrides Animal.getName()
}
Animal a = new Dog();
System.out.println(a.name); // "Animal" — field access uses static type
System.out.println(a.getName()); // "Dog" — method call uses dynamic type
String name = "Animal";
String getName() {
return "Animal";
}
}
class Dog extends Animal {
String name = "Dog"; // Shadows Animal.name (doesn't override it)
@Override String getName() { return Dog"; } // Overrides Animal.getName()
}
Animal a = new Dog();
System.out.println(a.name); // "Animal" — field access uses static type
System.out.println(a.getName()); // "Dog" — method call uses dynamic type
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | We cannot override {{c1::attributes inside a subclass}} they are {{c1::shadowed}}. | We cannot override {{c1::attributes inside a subclass}}, they are {{c1::shadowed}}. |
| Extra | class Animal { <br> String name = "Animal"; <br> String getName() { <br> return "Animal"; <br> } <br>} <br><br>class Dog extends Animal { <br> String name = "Dog"; <span style="font-style: italic;">// Shadows Animal.name (doesn't override it)</span> <br> @Override String getName() { return Dog"; } <span style="font-style: italic;">// Overrides Animal.getName()</span> <br>}<br><br>Animal a = new Dog(); <br>System.out.println(a.name); <span style="font-style: italic;">// "Animal" — field access uses static type</span> <br>System.out.println(a.getName()); <span style="font-style: italic;">// "Dog" — method call uses dynamic type</span> |
Note 2: ETH::1. Semester::EProg
Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
modified
Note Type: Horvath Cloze
GUID:
iz]M]${2
Before
Front
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
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
After
Front
Enums have some convenient 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
Enums have some convenient 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 convenien |
Enums have some convenient features:<br><ul><li>{{c1::can compare using <code>==::compare}}</code></li> <li>{{c2::use <code>.name()</code> to get string representation::string}}</li> <li>{{c3::go from string to enum using <code>Status.valueOf("PAID")::string conversion}}</code></li> <li>{{c4:: Ordering using order in declaration <code>Status.SUBMITTED < Status.PAID::order</code>}}</li></ul> |
Note 3: ETH::1. Semester::EProg
Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
modified
Note Type: Horvath Cloze
GUID:
m5.*#}3,2{
Before
Front
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
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
After
Front
Possible casting problems:
- String s = (String) new Integer(5); Casting between unrelated classes
- Animal a2 = new Cat(); Dog d2 = (Dog) a2; ClassCastException!
Back
Possible casting problems:
- String s = (String) new Integer(5); Casting between unrelated classes
- Animal a2 = new Cat(); Dog d2 = (Dog) a2; ClassCastException!
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | Possible casting problems:<br><ul><li>{{c1::String s = (String) new Integer(5); Casting between unrelated classes }}</li><li>{{c2::Animal a2 = new Cat(); Dog d2 = (Dog) a2; ClassCastException!}}</li></ul> |
Note 4: ETH::1. Semester::EProg
Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
modified
Note Type: Horvath Cloze
GUID:
oE~TL)}ZNN
Before
Front
In java a class cannot inherit from multiple super-classes
Back
In java a class cannot inherit from multiple super-classes
This works only for interfaces.
After
Front
In Java a class cannot inherit from multiple super-classes.
Back
In Java a class cannot inherit from multiple super-classes.
This works only for interfaces.
Field-by-field Comparison
| Field | Before | After |
|---|---|---|
| Text | In |
In Java a class cannot inherit from {{c1:: multiple super-classes}}. |
Note 5: ETH::1. Semester::EProg
Deck: ETH::1. Semester::EProg
Note Type: Horvath Cloze
GUID:
modified
Note Type: Horvath Cloze
GUID:
q+A$0CHp#W
Before
Front
If we override an attribute inherited from the subclass, it will override the parents attribute.
Back
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
After
Front
If we override an attribute inherited from the parentclass, it will override the parents attribute.
Back
If we override an attribute inherited from the parentclass, 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 |
If we override an attribute inherited from the parentclass, it will {{c1::override}} the parents attribute. |
| Extra | <code>class A { <br> public int x = 5; <br> public void fct1() { <br> System.out.println(this.x); <br> } <br>} <br><br>class B extends A { <br> public B() { <br> super.x = 10; <br> } <br> public B(int a) { <br> super.x = a; <br> } <br>} <br><br>A a1 = new B(1); <br>A a2 = new B(12); // As these are different instances, their attributes are separate <br>B b1 = new B(1); <br>B b2 = new B(12); <br><br>a1.fct1(); // 1 -> Even though static type is A <br>a2.fct1(); // 12 -> Different output as a2's instance of A has 12 <br>b1.fct1(); // 1 -> same here, even though it dynamic dispatches <br>b2.fct1(); // 12 </code> |