Vererbung
Motivation
Instrument[] orchestra = new Instrument[50];
orchestra[0] = new Cello();
orchestra[1] = new Violin();
...
for(Instrument i : orchestra)
i.play();
Cellomuss inInstrumentgespeichert werden- Compiler muss
i.play()zusichern können - Polymorphie
extends
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void work() {
doJob();
}
public class Student extends Person {
...
}
Person person = new Person("Chuck Norris");
Student student = new Student("Bruce Lee");
student.work();
Was wird vererbt?
- Methoden
publicprotectedpackage-private- Felder
- alle
private- kein Zugriff!- keine Konstruktoren
- keine
staticMember
Konstruktion
Jeder Konstruktor ruft implizit durch super() einen Konstruktor der Superklasse auf
⟹ nur eine Klasse extendbar
public class Student {}public class Student {
public Student() {
super();
}
}
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public class Student extends Person {
private int nr;
public Student(String name, int nr) {
super(name);
this.nr = nr;
}
- Erste Zeile eines Konstruktors:
this(...);/super(... );
Sonst nirgends! 🚫 - Implizit:
super();

new GermanShepherd();
---> new Dog();
---> new Animal();
---> new Object();
@Override
Subklassen dürfen Methoden überschreiben
public class Person {
public void work() {
doJob();
}public class Student extends Person {
@Override
public void work() {
study();
}
Erlaubte Änderungen
Dog identifyDog(Dog dog) throws NoDogException
Sichtbarkeit generalisieren
public Dog identifyDog(Dog dog) throws NoDogException
protected Dog identifyDog(Dog dog) throws NoDogException
private Dog identifyDog(Dog dog) throws NoDogException 🚫
Rückgabetyp spezialisieren
GermanShepherd identifyDog(Dog dog) throws NoDogException
Animal identifyDog(Dog dog) throws NoDogException 🚫
checked Exceptions spezialisieren
Dog identifyDog(Dog dog)
Dog identifyDog(Dog dog) throws NullPointerException
Dog identifyDog(Dog dog) throws NoGermanShepherdException
Dog identifyDog(Dog dog) throws NoAnimalException 🚫
Dog identifyDog(Dog dog) throws IOException 🚫
final
final int a = 3;
a = 0; 🚫
final Person person = new Person("Max");
person.setName("Karl");
person = new Person("Karl"); 🚫
final class String {
...
}
class CustomString738 extends String 🚫
class Person {
final void work() {
...
}
class Student extends Person{
@Override
void work() { 🚫
study();
}
}
equals
== vergleicht auf Objektgleichheit
Person person = new Person("name");
Person otherPerson = person;
person == otherPerson -> true
new Person("name") == new Person("name") -> false
a.equals(b) vergleicht auf Inhaltsgleichheit
new Student("Karl", 3).equals(new Student("Karl", 3)) -> true
new Student("Max", 5).equals(new Student("Max", 7)) -> false
public class Object() {
public boolean equals(Object obj) {
return (this == obj);
}public class Dog extends Animal{
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
if (!super.equals(o))
return false;
Dog dog = (Dog) o;
return weight == dog.weight &&
Objects.equals(breed, dog.breed);
}
Polymorphie
Person student = new Student("Max", 1);
student.work();Compiletyp reference = new Laufzeittyp();
- Compiletyp
- Existenz
- Sichtbarkeit
- Returntyp
- Exceptions
- Laufzeittyp
- Code dieser Klasse wird ausgeführt
- Subtyp des Compiletyps
Casten
Ein Cast ändert den Compiletyp, kann aber zur Laufzeit eine ClassCastException verursachen
Dog dog = (Dog) "Dog"; 🚫
Animal animal = new ___();
Dog dog = (Dog) animal;
Animal,Cat,Bird- Compiler:
AnimalkönnteDogenthalten ✔️ ClasscastException 💥Dog- Compiler:
AnimalkönnteDogenthalten ✔️ - Laufzeit ✔️
instanceof/.getClass
instanceof- Überprüft, ob Laufzeittyp (Sub)klasse von
Xif (obj instanceof X) { X x = (X) obj; foo(x); }if (obj instanceof X x) { foo(x); } .getClass- Überprüft, ob Laufzeittyp
Xif (obj.getClass() == X.class)