lecture 4
Animal foo = new Dog(); // ok
override heiachy
dog[] is animal[]
final no sub class
String is final
Dog dog = new Animal() // cannot go up
Dog dog = new GoldenRetriver // ok
GoldenRetriver cc = new Dog() // no
Goldenretriver goldenrever = (Goldenretrive) dog // depends on if dog is GoldenRetriver
Abstract class
super
invoke super constructor
public class Cat extends Animal {
@Override protected String makeNoise(){
}
}
- attempt to assign weaker access privileges' was public (cannot override it in animal)
@Override public String makeNoise(){
return "meow"
}
Abstract
public abstract class Animal{
public abstract String makeNoise();
}
Abstract class you might not need the implementation
Object class
getClass()
// returns the Class class instance
hashcode()
// a hash code value for this object
private final String name
@Override public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if ((obj == null ) || (getClass() != obj.getClass()) {
return false;
}
// for every propery under consideration for equality, check
Dog that = (Dog) obj
return (nam == null ? that.name == null : name.equals(that.name));
}
@Override public int hashCode() {
int result = this.id == null ? 0 : id.hashCode();
result = 31 (prime number) * result + (dateTime == null ? 0 : dateTime.hashCode());
return result;
//return (this.name == null ? 0 : this.name.hashCode());
}
if equals gethashCode must be the same
网友评论