Question: 6.1 The Concept of Inheritance Using Inheritance for Modeling Specialization subclasses inherit You use inheritance to model a relationship between classes in whid.h from superclasses


6.1 The Concept of Inheritance Using Inheritance for Modeling Specialization subclasses inherit You use inheritance to model a relationship between classes in whid.h from superclasses that repre- one class represents a more general concept and another a more sent more general concepts.specialized concept. For example, consider a class Manager that inher its from the class Employee. This is a valid use of inheritance because managers are a special type of employee. Every manager is an employee, but not every employee is a manager. The more general class is called the superclass and the more spe- cialized class the subclass A subclass can define addi- tional methods and fields.specialized subclass objects may have additional methods and fields. Generally, the subclass extends the capabilities of the superclass. The Consider this simple Employee class: public class Emp loyee public Employee (String aName) name aName; public void setSalary(double aSalary) salaryaSalary public String getNameO return name; public double getSalaryO t return salary; private String name; private double salary; Here is a Manager class that adds a new method and a new field. The Subclasses can override methods by giving a newbclass also ouerrides an existing uperclass, giving a definition for a method that new definition. The Manager version of getSalary will compute the exists in the superclass. method of the s sum of the base salary and the bonus. public class Manager extends Emp loyee public Manager(String aName).. .J public void setBonus (double aBonus) bonus aBonus; I/ new method public double getSalaryO. . . // overrides Employee method private double bonus; // new field Note that Java uses the extends keyword to denote inheritance. You will see the code for the Manager constructor and the getSalary method later in this chapter. Figure 1 shows the class diagram
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
