well sunil !
Inheritance is similar in Java and C++. Java uses the extends keyword instead of the : token. All inheritance in Java is public inheritance; there is no analog to the C++ features of private and protected inheritance.
The keyword extends indicates that you are making a new class that derives from an existing class. The existing class is called the superclass, base class, or parent class. The new class is called the subclass, derived class, or child class. The terms superclass and subclass are those most commonly used by Java programmers, although some programmers prefer the parent/child analogy, which also ties in nicely with the "inheritance" theme.
The Employee class is a superclass, but not because it is superior to its subclass or contains more functionality. In fact, the opposite is true: subclasses have more functionality than their super classes. For example, as you will see when we go over the rest of the Manager class code, the Manager class encapsulates more data and has more functionality than its superclass Employee.
NOTE
The prefixes super and sub come from the language of sets used in theoretical computer science and mathematics. The set of all employees contains the set of all managers, and this is described by saying it is a superset of the set of managers. Or, put it another way, the set of all managers is a subset of the set of all employees.
Our Manager class has a new field to store the bonus, and a new method to set it:
class Manager extends Employee
{
. . .
public void setBonus(double b)
{
bonus = b;
}
private double bonus;
}There is nothing special about these methods and fields. If you have a Manager object, you can simply apply the setBonus method.
Manager boss = . . .;
boss.setBonus(5000);Of course, if you have an Employee object, you cannot apply the setBonus method—it is not among the methods that are defined in the Employee class.
However, you can use methods such as getName and getHireDay with Manager objects. Even though these methods are not explicitly defined in the Manager class, they are automatically inherited from the Employee superclass.
Similarly, the fields name, salary, and hireDay are inherited from the superclass. Every Manager object has four fields: name, salary, hireDay, and bonus.
When defining a subclass by extending its superclass, you only need to indicate the differences between the subclass and the superclass. When designing classes, you place the most general methods into the superclass and more specialized methods in the subclass. Factoring out common functionality by moving it to a superclass is common in object-oriented programming.
However, some of the superclass methods are not appropriate for the Manager subclass. In particular, the getSalary method should return the sum of the base salary and the bonus. You need to supply a new method to override the superclass method:
class Manager extends Employee
{
. . .
public double getSalary()
{
. . .
}
. . .
}How can you implement this method? At first glance, it appears to be simple—just return the sum of the salary and bonus fields:
public double getSalary()
{
return salary + bonus; // won't work
}However, that won't work. The getSalary method of the Manager class has no direct access to the private fields of the superclass. This means that the getSalary method of the Manager class cannot directly access the salary field, even though every Manager object has a field called salary. Only the methods of the Employee class have access to the private fields. If the Manager methods want to access those private fields, they have to do what every other method does—use the public interface, in this case, the public getSalary method of the Employee class.
So, let's try this again. You
Answered by
raj u
, an ibibo Master,
at
11:43 AM on September 12, 2008