Polymorphism

package polymorphismsample;

/**
 *
 * @author Nurhak
 */
public class PolymorphismSample {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ComputerEngineer ce = new ComputerEngineer("Nurhak", "Kaya", "IT");
        ce.addProgrammingLang("Java");
        ce.addProgrammingLang("C#");
        Accountant ac = new Accountant("Jessie", "J");
        Manager mn = new Manager("Michael", "M");
        mn.addEmployee(ce);
        mn.addEmployee(ac);
        mn.getReport();
    }
}

package polymorphismsample;

/**
 *
 * @author Nurhak
 */
public class Employee {

    protected String name;
    protected String surname;

    public Employee(String name, String surname) {
        this.name = name;
        this.surname = surname;
    }

    public void getReport() {
        System.out.println("Name: " + name + " Surname: " + surname);
    }
}

package polymorphismsample;

/**
 *
 * @author Nurhak
 */
public class Engineer extends Employee {

    protected String[] projects;
    protected String department;
    protected int projectCount;

    public Engineer(String name, String surname, String department) {
        super(name, surname);
        this.department = department;
        this.projects = new String[5];
        this.projectCount= 0;
    }

    public void addProject(String project) {
        projects[projectCount] = project;
        projectCount++;

    }

    @Override
    public void getReport() {
        super.getReport();
        System.out.println("Department: " + department);
        System.out.println("Projects: ");
        for (int i = 0; i < projectCount; i++) {
            System.out.println("\t" + projects[i]);
        }
    }
}

package polymorphismsample;

/**
 *
 * @author Nurhak
 */
public class ComputerEngineer extends Engineer {

    protected String[] programmingLangs;
    protected int programmingLangCount;

    public ComputerEngineer(String name, String surname, String department) {
        super(name, surname, department);
        this.programmingLangs = new String[5];
        this.programmingLangCount = 0;
    }

    public void addProgrammingLang(String programmingLangName) {
        programmingLangs[programmingLangCount] = programmingLangName;
        programmingLangCount++;
    }

    @Override
    public void getReport() {
        super.getReport();
        System.out.println("Title: Computer Engineer");
        System.out.println("Programming Languages: ");
        for (int i = 0; i < this.programmingLangCount; i++) {
            System.out.println("\t" + programmingLangs[i] + " ");
        }
    }
}

package polymorphismsample;

/**
 *
 * @author Nurhak
 */
public class Accountant extends Employee {

    private String[] accountingPrograms;
    private int accountingProgramCount;

    public Accountant(String name, String surname) {
        super(name, surname);
        this.accountingPrograms = new String[5];
        this.accountingProgramCount = 0;
    }

    public void addAccountingProgram(String accountingProgram) {
        accountingPrograms[accountingProgramCount] = accountingProgram;
        accountingProgramCount++;
    }

    @Override
    public void getReport() {
        super.getReport();
        System.out.println("Title: Accountant");
        System.out.println("Accounting Programs: ");
        for (int i = 0; i < accountingProgramCount; i++) {
            System.out.println("\t" + accountingPrograms[i]);
        }
    }
}

package polymorphismsample;

/**
 *
 * @author Nurhak
 */
public class Manager extends Employee {

    private Employee[] employees;
    private int employeeCount;

    public Manager(String name, String surname) {
        super(name, surname);
        this.employees = new Employee[5];
        this.employeeCount = 0;
    }

    public void addEmployee(Employee employee) {
        employees[employeeCount] = employee;
        employeeCount++;
    }

    @Override
    public void getReport() {
        for (int i = 0; i < employeeCount; i++) {
            employees[i].getReport();
            System.out.println(employees[i]);
        }
    }
}

Comments

Popular posts from this blog

Solution for the fault: "System.Diagnostics.Process.Start is not working on IIS, but working on ASP.NET Development Server"

Solution for "Can not obtain Oracle client information from registry"