In: Computer Science
Give an example of how to build an array of objects of a super class with its subclass objects. As well as, use an enhanced for-loop to traverse through the array while calling a static method(superclass x). Finally, create a static method for the class that has the parent class reference variable.
public class Colleges
{
private static Teacher[] teachers;
public Colleges()
{
teachers= new Teacher[]{new Dean("Sapo",10000),new Teacher("James",5000),new Teacher("Jessie",5001)};
}
public static void main(String[] args)
{
Colleges colleges= new Colleges();
teachers[0].setBonus(50);
System.out.println(teachers[0].getBonus());
}
}
public class Teacher extends Person
{
int salary;
public Teacher(String name,int salary) {
super(name);
setSalary(salary);
// TODO Auto-generated constructor stub
}
public void setSalary(int salary)
{
this.salary= salary;
}
public int getSalary()
{
return salary;
}
}
public class Dean extends Teacher {
private final int bonus;
public Dean(String name, int salary, int bonus) {
super(name, salary);
this.bonus = bonus;
}
@Override
int getSalary() {
return super.getSalary() + bonus;
}