Inheritance
class Child extends Parent {
Child() {
super(); // acts like the parent constructor
// note super will be called anyway, but with no parameters passed
}
}
Note: final vars and methods are not inherited
Overriding inherited methods
class Parent {
public void methodName(){
// code...
}
}
class Child extends Parent {
@Override // << note the key word
public void methodName(){
// diffrent code...
}
}
// You can still call the parent version
super.methodName()Using a child as its parent
Parent objName = new Child();
// here the child will be treated like it is a parentChilds and arrays
It is possible to store different child classes in the same array as long as they have the same parent.
// example
Monster dracula, wolfman, zombie1;
dracula = new Vampire();
wolfman = new Werewolf();
zombie1 = new Zombie();
Monster[] monsters = {dracula, wolfman, zombie1};
for (Monster monster : monsters) {
monster.attack();
}