Public int gethours return 40 works 40 hours week

Info icon This preview shows pages 11–16. Sign up to view the full content.

View Full Document Right Arrow Icon
public int getHours() { return 40; // works 40 hours / week } public double getSalary() { return 40000.0; // $40,000.00 / year } public int getVacationDays() { return 10; // 2 weeks' paid vacation } public String getVacationForm() { return "yellow"; // use the yellow form } } • Lawyers, Secretaries, etc. have similar behavior to the above. How to implement those classes without redundancy?
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
12 Inheritance syntax public class name extends superclass { – Example: public class Lawyer extends Employee { ... } • By extending Employee , each Lawyer object now: receives a copy of each method from Employee automatically can be treated as an Employee by client code
Image of page 12
13 Overriding methods override : To replace a superclass's method by writing a new version of that method in a subclass. No special syntax is required to override a method. Just write a new version of it in the subclass. public class Lawyer extends Employee { // overrides getSalary method in Employee class; // give Lawyers a $5K raise public double getSalary() { return 45000.00; } }
Image of page 13

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
14 super keyword • Subclasses can call overridden methods with super super. method ( parameters ) – Example: public class Lawyer extends Employee { // give Lawyers a $5K raise (better) public double getSalary() { double baseSalary = super.getSalary() ; return baseSalary + 5000.00; } } This version makes sure that Lawyers always make $5K more than Employees, even if the Employee's salary changes.
Image of page 14
15 Calling super constructor super( parameters ); – Example: public class Lawyer extends Employee { public Lawyer(String name) { super(name); // calls Employee constructor } ... } – super allows a subclass constructor to call a superclass one.
Image of page 15

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern