Inheritance (Part 1) Fattane Zarrinkalam Problem • Three main concepts in an information system of an educational institute: • Teacher • Student • Employee public class Student { private String name; private String lastname; private Date birthDate; private int id; … } public class Teacher { private String name; private String lastname; private Date birthDate; private int rank; … } public class Employee { private String name; private String lastname; private Date birthDate; private int empCode; … } 2 Problem • Issues with this design • • • • Duplicate codes Define duplicate members in classes Increase class size Repetition is the source of many problems • Incomplete update • Increased likelihood of bugs • Lack of attention to the semantic connection of concepts in the real world • All three concepts of teacher, student and employee have a lot in common • Because all three are a special form of the concept of person / person / human 3 Problem • Solution: inheritance • Identify common features between classes • Define common features (attributes and methods) in a class • Superclass • Define the specific features of each concept in a separate class • Subclass • Establish a connection between subclasses and the superclass 4 Inheritance • It is the mechanism in java by which one class is allowed to inherit the features (attributes and methods) of another class. • The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class). 5 Superclass : Person public class Person { private String name; private String lastname; private Date birthDate; public Person(String name, String lastname) { setName(name); setLastname(lastname); } Why is this constructor defined? public Person() { } public void print() { System.out.println(“name: “ + name); System.out.println(“lastname: “ + lastname); } public void setName(String name) { this.name = name; } (continued)6 Superclass : Person public String getName() { return name; } public void setLastname(String lastname) { this.lastname = lastname; } public String getLastname() { return lastname; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public Date getBirthDate() { return birthDate; } … } 7 Subclass1 : Student The keyword used for inheritance is extends. public class Student extends Person { private int id; public Student(String name, String lastname, int id) { setName(name); setLastname(lastname); setId(id); } public void setId(int id) { this.id = id; } The class Student inherits from the Person class public int getId() { return id; } … } 8 Subclass2 : Teacher public class Teacher extends Person { private int rank; public Teacher(String name, String lastname, int rank) { setName(name); setLastname(lastname); setRank(rank); } public void setRank(int rank) { this.rank = rank; } public int getRank() { return rank; } … } 9 Subclass3 : Employee public class Employee extends Person { private int empCode; public Employee(String name, String lastname, int empCode) { setName(name); setLastname(family); setEmpCode(empCode); } public void setEmpCode(int empCode) { this.empCode = empCode; } public int getEmpCode() { return empCode; } … } 10 Inheritance Hierarchy Superclass Person is-a relationship Teacher Student Employee Subclasses 11 Inheritance Hierarchy • A class can be a superclass of other classes and itself a subclass of another class • A class can have several subclasses (direct or indirect) Shape Shape2D Circle Shape3D Rectangle Cube Class Hierarchy Sphere 12 Types of Inheritance • Single inheritance • A class can only have one direct superclass • Multiple inheritance • A class can have several direct superclasses Java doesn’t support multiple inheritance 13 Example public class Person { private String name; private String lastname; private Date birthDate; public class Student extends Person { private int id; public Student(String name, String lastname, int id) { setName(name); setLastname(lastname); setId(id); } public void setId(int id) { public Person(String name, String lastname) { this.id = id; setName(name); } setLastname(lastname); } public int getId() { public Person() { return id; } } public void setName(String name) { … this.name = name; } } public void setLastname(String lastname) { this.lastname = lastname; } … } 14 Example public class Student extends Person { private int id; public Student(String name, String lastname, int id) { setName(name); setLastname(lastname); setId(id); } public void setId(int id) { this.id = id; } Student stu = new Student(“John”, “River”, 87231); stu.setName(“Johnecca”); public int getId() { return id; } … } 15 Example public class Person { private String name; private String lastname; private Date birthDate; public class Student extends Person { private int id; public Student(String name, String lastname, int id) { this.name = name; this.lastname = lastname; this.Id = id; } Compile time error public void setId(int id) { public Person(String name, String lastname) { this.id = id; setName(name); } setLastname(lastname); } public int getId() { public Person() { return id; } } public void setName(String name) { … this.name = name; } } public void setLastname(String lastname) { this.lastname = lastname; } … } 16