Eksempelkode om arv:
public class InvalidNameException extends IllegalArgumentException { private Person person; private String name; public InvalidNameException(Person person, String name) { this.person = person; this.name = name; } public Person getPerson() { return person; } public String getName() { return name; } @Override public String getMessage() { return "Du prøvde å sette navnet til " + name + ", men det er ugyldig for " + person; } } public class Person { private String name; public Person(String name) { this.name = name; } @Override public String toString() { return "[Person " + name + " (" + super.toString() + ")]"; } public String getName() { return name; } private String legalCharacters = "QWERTYUIOPÅASDFGHJKLØÆZXCVBNM"; public void setName(String name) { for (int i = 0; i < name.length(); i++) { char c = Character.toUpperCase(name.charAt(i)); if (legalCharacters.indexOf(c) < 0) { throw new InvalidNameException(this, name); } } this.name = name; } @Override public boolean equals(Object obj) { if (obj instanceof Person && ((Person) obj).name.equals(this.name)) { return true; } return false; } public static void main(String[] args) { Person person1 = new Person("Hallvard"); Person person2 = new Person("Hallvard"); Collection<Person> personer = Arrays.asList(person1, person2); System.out.println(person1 == person2); System.out.println(person1.equals(person2)); System.out.println(personer.contains(new Person("Hallvard"))); person1.setName("ruy389weyfsuid"); } }
public class Bok { private String title; public Bok(String title) { this.title = title; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @Override public String toString() { return "Bok [title=" + title + "]"; } } public class Ordbok extends Bok { int antallOrd; public Ordbok(String title, int antallOrd) { super(title); this.antallOrd = antallOrd; } @Override public String toString() { return "Ordbok [antallOrd=" + antallOrd + ", title=" + getTitle() + "]"; } } public class Tegneseriealbum extends Bok { int antallStriper; public Tegneseriealbum(String title, int antallStriper) { super(title); this.antallStriper = antallStriper; } } public class Bibliotek { private Bok bok; private Ordbok nynorsk; private Tegneseriealbum pondus; public void init() { bok = new Bok(); bok.title = "Sult"; nynorsk = new Ordbok(); nynorsk.title = "Nynorsk ordbok"; nynorsk.antallOrd = 12345; pondus = new Tegneseriealbum(); pondus.title = "Flat firer"; pondus.antallStriper = 128; } public void run() { Bok bok1 = nynorsk; Bok bok2 = pondus; Ordbok ordbok1 = (Ordbok) bok2; } public static void main(String[] args) { Bibliotek program = new Bibliotek(); program.init(); program.run(); } }
public class Bok { private String title; public Bok(String title) { setTitle(title); } public String getTitle() { return title; } protected boolean isValidTitle(String title) { return title.indexOf(' ') < 0; } public void setTitle(String title) { if (! isValidTitle(title)) { throw new IllegalArgumentException("Ikke gyldig!"); } this.title = title; } @Override public String toString() { return "Bok [title=" + title + "]"; } } public class Ordbok extends Bok { int antallOrd; public Ordbok(String title, int antallOrd) { super(title); this.antallOrd = antallOrd; } @Override public String toString() { return "Ordbok [antallOrd=" + antallOrd + ", title=" + getTitle() + "]"; } @Override protected boolean isValidTitle(String title) { return super.isValidTitle(title) && title.endsWith("ordbok"); } } public class Tegneseriealbum extends Bok { int antallStriper; public Tegneseriealbum(String title, int antallStriper) { super(title); this.antallStriper = antallStriper; } } public class Bibliotek { private Bok bok; private Ordbok nynorsk; private Tegneseriealbum pondus; public void init() { bok = new Bok("Sult"); nynorsk = new Ordbok("Nynorsk ordbok", 12345); pondus = new Tegneseriealbum("Flat firer", 128); } public void run() { Bok bok1 = nynorsk; Bok bok2 = pondus; if (bok1 instanceof Ordbok) { ((Ordbok) bok1).antallOrd = 10; } } public static void main(String[] args) { Bibliotek program = new Bibliotek(); program.init(); program.run(); } }