The objective of this programming assignment is to experience the use of inherit
ID: 3660238 • Letter: T
Question
The objective of this programming assignment is to experience the use of inheritance in Java and to see how polymorphism works with inheritance in Java. The assignment involves writing three classes, plus a test class. The base class is an employee class which contains a couple of attributes common to all employees and a fundamental method to calculate pay. The two derived classes are a commissioned employee that adds payment of a sales commission as part of the pay calculation, and a union employee which adds overtime payment and union dues as part of the pay calculation. The test program will be structured to include a method which accepts a base class reference and demonstrates polymorphic behavior.Explanation / Answer
Flexible programs focus on polymorphism and not inheritance. Some languages focus on static type checking (C++, Java, C#) which links the concepts and reduces polymorphic opportunities. Languages that separate the concepts can allow you to focus on polymorphism and create more robust code. JavaScript, Python, Ruby, and VB.NET do not have typed variables and defer type checking to runtime. Is the value of static type checking worth giving up the power of pure polymorphism at runtime? Inheritance and polymorphism are independent but related entities – it is possible to have one without the other. If we use a language that requires variables to have a specific type (C++, C#, Java) then we might believe that these concepts are linked. If you only use languages that do not require variables to be declared with a specific type, i.e. var in JavaScript, def in Python, def in Ruby, dim in VB.NET then you probably have no idea what I'm squawking about! :-) I believe that the benefits of pure polymorphism outweigh the value of static type checking. Now that we have fast processors, sophisticated debuggers, and runtime exception constructs the value of type checking at compile time is minimal. Some struggle with polymorphism, so let's define it: Polymorphism is the ability to send a message to an object without knowing what its type is. Polymorphism is the reason why we can drive each others cars and why we have no trouble using different light switches . A car is polymorphic because you can send commonly understood messages to ANY car (start(), accelerate(), turnLeft(), turnRight(), etc) without knowing WHO built the car. A light switch is polymorphic because you can send the message turnOn() and turnOff() to any light switch without knowing who manufactured it. Polymorphism is literally what makes our economy work. It allows us to build functionally equivalent products that can have radically different implementations. This is the basis for price and quality differences in products, i.e. toasters, blenders, etc. Polymorphism through Inheritance: The UML diagram above shows how polymorphism is stated in languages like C++, Java, and C#. The method (a.k.a operation) start() is declared to be abstract (in UML), which defers the implementation of the method to the subclasses in your target language. The method for start() is declared in class Car and specifies only the method signature and not an implementation (technically polymorphism requires that no code exists for method start() in class Car). The code for method start() is then implemented separately in the VolkswagenBeetle and SportsCar subclasses. Polymorphism implies that start() is implemented using different attributes in the subclasses, otherwise the start() method could simply been implemented in the super class Car. Weakening the Link The negative effects of the tight link between inheritance and polymorphism lead both Java and C# to introduce the concept of interface to pry apart the ideas of inheritance and polymorphism but keep strong type checking at compile time. First it is possible to implement the above C++ example exactly using inheritance as shown by the C# below: view sourceprint? 01. // C# polymorphism using inheritance 02. 03. class Car { 04. public virtual boolean start(); // declare signature 05. } 06. 07. class VolkswagenBeetle : Car { 08. public override boolean start() { 09. // implementation code 10. } 11. } 12. 13. class SportsCar : Car { 14. public override boolean start() { 15. // implementation code 16. } 17. } 18. 19. // Invocation of polymorphism 20. Car cars[] = { new VolkswagenBeetle(), new SportsCar() }; 21. 22. for( I = 0; I < 2; i++) 23. Cars[i].start(); In addition, through the use of the interface concept we can write the classes in Java as follows Java polymorphism using interface: 01. // Java polymorphism using interface 02. 03. interface Car { 04. public boolean start(); 05. } 06. 07. class VolkswagenBeetle implements Car { 08. public boolean start() { 09. // implementation code 10. } 11. } 12. 13. class SportsCar implements Car { 14. public boolean start() { 15. // implementation code 16. } 17. } Polymorphism without Inheritance There are languages where you have polymorphism without using inheritance. Some examples are JavaScript, Python, Ruby, VB.NET, and Small Talk. In each of these languages it is possible to write car.start() without knowing anything about the object car and its method 01. # Python polymorphism 02. 03. 04. 05. class VolkswagenBeetle(Car): 06. def start(): # Code to start Volkswagen 07. 08. class SportsCar(Car): 09. def start(): # Code to start SportsCar 10. 11. # Invocation of polymorphism 12. cars = [ VolkswagenBeetle(), SportsCar() ] 13. for car in cars: 14. car.start() The ability to get pure polymorphism stems from these languages only have a single variable type prior to runtime i.e. var in JavaScript, def in Python, def in Ruby, dim in VB.NET. With only one variable type there can not be type error prior to runtime. Historical note: It was only during the time frame when Java and C# were introduced that CPU power was sufficient for interpreted languages to give sufficient performance at run time. The transition from having polymorphism and inheritance tightly coupled to being more loosely coupled depended on run time interpreters being able to execute practical applications with decent performance.Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.