Inheritance doesn't work like that! Here are some slightly more complete definitions:
Code:
public class Person() {
public Person(String firstName, String surname) {
this.firstName = firstName;
this.surname = surname;
}
public String getName() {
return firstName + " " + surname;
}
private String firstName;
private String surname;
}
public class Policeman extends Person {
public Policeman(String firstName, String surname, int hatSize) {
super(firstName, surname);
this.hatSize = hatSize;
}
public void arrest(Person p) {
// some code here....
}
private int hatSize;
}
Here is how they would be used:
Code:
public class TestClasses {
public static void main(String[] args) {
Person robber = new Person("Robin", "Banks");
System.out.println("Person: " + robber.getName());
Policeman pc_plod = new Policeman("Chief", "Wiggum", 12);
System.out.println("Policeman: " + robber.getName());
pc_plod.arrest(robber);
// You can't call robber.arrest(pc_plod).
}
}
Bookmarks