Abstract classes and polymorphism Assignment Help

Assignment Help: >> Abstraction in Java >> Abstract classes and polymorphism

Abstract  classes and  polymorphism:

You may be thinking that all this abstract class definition is rather a lot of trouble for not much reward. So in this section we present an example to show how it can be useful.

An inheritance hierarchy consists of a group of related classes, which by definition have some similarities (related by inheritance) and some differences (otherwise we would need only one class). We can take advantage of these similarities to write software that is extensible in certain ways, without extensive modification.

Suppose we have stored details of many employees of different categories in an array. But what type should the array be. If we define an array of MonthlyEmployee we cannot store WeeklyEmployee objects in it, and so on. Therefore we must declare an array of the highest type in the hierarchy, namely Employee:

Employee staff []= new Employee [MAX_EMPLOYEES];

Since we are not creating Employee objects, only Employee references, this is all right. We can then store references to objects of any subclass of Employee in the array.

Remember that a subclass reference can always be assigned to a variable declared as being of a superclass type. The opposite assignment is not possible - a superclass reference cannot be directly assigned to a variable declared as being of a subclass type - but it may be possible to do this by using a cast.

We can now take advantage of the common behaviour imposed on the hierarchy of classes by the Employee abstract class. If we want to display the wages for all employees we can proceed as follows:

public void displayPayroll ()
{
for (int e = 0; e < staff.length; e++)
{
int pay = staff [e].calculatePay(); String name = staff [e].getName(); System.out.println(name + " " + pay);
}
}

Here we are using the calculatePay method, which we know every employee object must have regardless of whether it is a MonthlyEmployee object, a CarMonthlyEmployee object or any other subclass of Employee. This is an example of polymorphism.

We say that the method invocation for calculatePay is polymorphic because Java executes the version of the calculatePay method appropriate to the object type. For MonthlyEmployee objects it uses the calculatePay method defined in that class, for CarMonthlyEmployee objects it uses the calculatePay method defined in that class, which overrides the calculatePay method in its superclass, and so on.

The getName method invocation may not be polymorphic because this is defined in the Employee class and so is the same for all subclasses, unless it has been overridden.

So where is the payoff. Suppose we want to extend the Employee class hierarchy by introducing new classes for junior employees still in training, where we need to store various special details, such as the employees' training programmes. We might need to have WeeklyTrainee and MonthlyTrainee classes that are subclasses of WeeklyEmployee and MonthlyEmployee respectively.

If we define these two new classes in this way, the payoff is that we do not have to change any parts of the code that use polymorphism, such as the displayPayroll method above. This will work for all types of employees, including the new ones, without any change to the source code - although we do, of course, have to recompile the program. This extensibility is very helpful in limiting the effect of changes to existing software.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Abstract  classes and  polymorphism homework and assignments? Live Abstract  classes and  polymorphism experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Abstract  classes and  polymorphism homework help, java assignment help and Abstract  classes and  polymorphism projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd