INHERITANCE & POLYMORPHISM By Grady Laksmono Inherit variables or methods from the other classes. The one who inherit the method is the child to the parent class. Example: Rectangle is the parent, and square is the child. Child will have access to public and protected access modifier from parent. 4 level of access modifier: 1. public 2. protected - any child, and any other class from same package 3. <package> - only other classes in the same package 4. private - current class We can override a class to be less restrictive, not more restrictive. If we want to make the parent‘s class not to be overridden by child class, we must use final keyword for our parent’s class. public class Child extends Parent { Public void m1() { m2(); // calls m2’s method super m2(); // same as just call m2(); } Public void m2() { m2(); // recursive, calling myself super m2(); // call the parent’s m2(); } } public class Parent { Public void m2() { } } If inherit abstract, any child must override the abstract method by: 1. Override every method those are declared as an abstract. 2. Declare yourself as an abstract. super keyword must be declared on the first line of the constructor’s class of a child. Lab exercise: Employee simulation to simulate inheritance and polymorphism hierarchy EXPLANATIONS ARE IN THE CODE’S COMMENTS EMPLOYEE INHERITANCE AND POLYMORPHISM HIERARCHY Person Employee getSalary employeeID title SalariedEmployee Salary HourlyEmployee hourlyRate numHour BasePlusCommissionEmployee CommissionPercentage SalesAmount // Person.java package com; public class Person { private String fname; private String lname; // Person have First Name // Person have Last Name public Person(String fname, String lname) { this.fname = fname; this.lname = lname; } public String getName() { return fname + " " + lname; } } // To get employee’s name // Employee.java package com.boeing; import com.Person; // Employee.java is a child of Person.java. Employee public abstract class Employee extends Person { “IS A” Person. private int employeeID; private String title; /* Child inherit First name, Last name. Plus, employee will need an ID and a title */ public Employee(String fname, String lname, int employeeID, String title) { super(fname, lname); this.employeeID = employeeID; this.title = title; } /* Overriding getSalary() abstract method from parent. Abstract method getSalary() gets overridden */ public abstract float getSalary(); // toString() method is used if you want to print an object public String toString() { return getName() + ":" + employeeID + " - " + title; } } // SalariedEmployee.java package com.boeing; // Salaried employee “IS AN” employee public class SalariedEmployee extends Employee { private float salary; /* Child inherit First name, Last name, Employee ID, and Title from Employee.java. Plus, Salaried Employee will need to have Salary */ public SalariedEmployee(String fname, String lname, int employeeID, String title, float salary) { super(fname, lname, employeeID, title); this.salary = salary; } // Abstract method getSalary() gets overridden public float getSalary() { return salary; } } // BasePlusCommissionEmployee.java package com.boeing; /* BasePlusCommissionEmployee “IS A” Salaried Employee AND “IS AN” Employee */ public class BasePlusCommissionEmployee extends SalariedEmployee { private float commissionPercentage; private float salesAmount; /* Child inherit First name, Last name, Employee ID, Title, and Salary from SalariedEmployee.java. Plus, Base plus Commission Employee will need to have commission percentage */ public BasePlusCommissionEmployee(String fname, String lname, int employeeID, String title, float salary, float commissionPercentage) { super(fname, lname, employeeID, title, salary); this.commissionPercentage = commissionPercentage; } /* Moreover, Base plus commission employee would need sales amount to calculate how much commission they got */ public void setSalesAmount(float salesAmount) { this.salesAmount = salesAmount; } // Abstract method getSalary() gets overridden public float getSalary() { return super.getSalary() + salesAmount * commissionPercentage; } } // HourlyEmployee.java package com.boeing; /* HourlyEmployee “IS AN” Employee */ public class HourlyEmployee extends Employee { private float hourlyRate; private float numHours; /* Child inherit First name, Last name, Employee ID, and Title from Employee.java. Plus, Hourly Employee would need Hourly Rate */ public HourlyEmployee(String fname, String lname, int employeeID, String title, float hourlyRate) { super (fname, lname, employeeID, title); this.hourlyRate = hourlyRate; } /* Moreover, we need to know how much is the number of hours they work so that we can count up the total on how much do we have to pay */ public void setNumHours(float numHours) { this.numHours = numHours; } // Abstract method getSalary() gets overridden public float getSalary() { return hourlyRate * numHours; } } // TestEmployee.java package com; import import import import com.boeing.BasePlusCommissionEmployee; com.boeing.Employee; com.boeing.HourlyEmployee; com.boeing.SalariedEmployee; public class TestEmployee { // Main method public static void main(String[] args) { // Initialization for employees in hard code Employee sal = new SalariedEmployee("Kobe", "Bryant", 100, "Basetball Player", 2000000.00f); Employee base = new BasePlusCommissionEmployee("Bill", "Gates", 101, "Chairman of the Board", 50000.00f, 0.5f); Employee hour = new HourlyEmployee("Kevin", "Federline", 102, "French Fry Maker", 7.25f); /* Downcasting. However, downcasting is not very recommended and dangerous because we don’t know which child is the one that is going to be used in a program. If you got a wrong type, then you will get an exception. There are different branches, and by down casting, you are changing the compile time type. */ ((BasePlusCommissionEmployee)base).setSalesAmount(10000.00f); ((HourlyEmployee)hour).setNumHours(10f); System.out.println(sal + " " + sal.getSalary()); System.out.println(base + " " + base.getSalary()); System.out.println(hour + " " + hour.getSalary()); } } Although downcasting is very dangerous and not very recommended, “UPCASTING” is NOT dangerous at all because Java only have single inheritance, thus it will only upcasted it once, “UP” one branch. INTERFACE on the next meeting will get around Java single inheritance. This is where you can inherit from many parents.