30 Lecture

CS304

Midterm & Final Term Short Notes

POLYMORPHISM – CASE STUDY: A SIMPLE PAYROLL APPLICATION

Polymorphism is the ability of an object to take on many forms. In the context of programming, this means that a single method can be implemented in different ways by different classes. A simple payroll application is a good example of how polym


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is polymorphism? A) The ability of an object to take on many forms B) The ability of an object to change its type at runtime C) The ability of an object to inherit multiple classes D) The ability of an object to override its superclass methods Answer: A What is a common use case for polymorphism? A) Implementing different payment methods B) Creating complex mathematical algorithms C) Defining new data types D) All of the above Answer: A In a payroll application, how might polymorphism be used? A) To implement a common interface for calculating employee pay B) To ensure that all employees are paid the same amount C) To limit the types of employees that can be added to the system D) To prevent employees from accessing each other's pay information Answer: A What is an interface in Java? A) A concrete implementation of a class B) A template for defining a class C) A set of methods and constants that a class must implement D) A way to declare private methods in a class Answer: C What is method overloading? A) Defining multiple methods with the same name but different parameters B) Defining multiple methods with the same name and same parameters C) Overriding a superclass method with a subclass method D) None of the above Answer: A What is method overriding? A) Defining multiple methods with the same name but different parameters B) Defining multiple methods with the same name and same parameters C) Overriding a superclass method with a subclass method D) None of the above Answer: C What is the difference between method overloading and method overriding? A) Method overloading is done at compile time, while method overriding is done at runtime B) Method overloading is done by the superclass, while method overriding is done by the subclass C) Method overloading is used to define new methods, while method overriding is used to modify existing methods D) Method overloading is based on the number and type of parameters, while method overriding is based on the method name and parameters Answer: D Can a subclass access private methods and fields of its superclass? A) Yes, always B) No, never C) Only if the subclass is in the same package as the superclass D) Only if the superclass declares the methods and fields as protected Answer: B What is an abstract class in Java? A) A class that cannot be instantiated B) A class that does not have any methods C) A class that only has private methods D) A class that can only be used as a superclass Answer: A Can an abstract class have non-abstract methods? A) Yes, but only if the class also has at least one abstract method B) Yes, but only if the class has no abstract methods C) No, an abstract class can only have abstract methods D) None of the above Answer: B


Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is the advantage of using polymorphism in a payroll application? Answer: The advantage of using polymorphism is that it allows for flexibility and extensibility in the payroll application. By using polymorphism, new employee types can be added without having to modify the existing code. How can you implement polymorphism in a payroll application? Answer: Polymorphism can be implemented in a payroll application by using inheritance and virtual functions. The base class can have virtual functions that are overridden by the derived classes. This allows the application to call the appropriate function based on the type of the employee. What is the difference between static and dynamic polymorphism? Answer: Static polymorphism is resolved at compile-time, while dynamic polymorphism is resolved at run-time. In C++, static polymorphism is achieved through function overloading, while dynamic polymorphism is achieved through virtual functions. How can you implement a payroll system that handles different employee types, such as salaried and hourly employees? Answer: A payroll system that handles different employee types can be implemented using inheritance and polymorphism. A base class Employee can be created with virtual functions for calculating pay. Derived classes, such as SalariedEmployee and HourlyEmployee, can be created that inherit from the Employee class and override the pay functions. How can polymorphism improve the maintainability of a payroll application? Answer: Polymorphism improves the maintainability of a payroll application by making it easier to add new employee types and modify existing ones. With polymorphism, new employee types can be added by simply creating a new derived class that inherits from the Employee base class and overrides the necessary functions. What is the advantage of using a virtual destructor in a polymorphic class hierarchy? Answer: The advantage of using a virtual destructor in a polymorphic class hierarchy is that it ensures that the destructor of the derived class is called when an object of the derived class is destroyed through a pointer to the base class. How can you prevent slicing when passing objects of derived classes by value to functions that take parameters of the base class type? Answer: To prevent slicing, objects of derived classes should be passed by reference or by pointer to functions that take parameters of the base class type. What is the purpose of a pure virtual function in an abstract base class? Answer: A pure virtual function in an abstract base class is a function that has no implementation and must be overridden by any derived class. This ensures that any derived class is forced to provide an implementation for the function. What is the difference between an abstract class and a concrete class? Answer: An abstract class is a class that has at least one pure virtual function and cannot be instantiated, while a concrete class is a class that can be instantiated and does not have any pure virtual functions. How can you implement a payroll system that handles overtime pay for hourly employees? Answer: A payroll system that handles overtime pay for hourly employees can be implemented by adding a virtual function for calculating overtime pay to the HourlyEmployee derived class. The function can be called in the payroll calculation function to calculate the total pay for the employee.

Polymorphism is a key concept in object-oriented programming that allows objects of different classes to be treated as if they are of the same class. In a simple payroll application, polymorphism can be used to calculate the pay of different types of employees. For example, there may be different types of employees such as full-time, part-time, and contractors, each with their own way of calculating pay. To implement polymorphism in the payroll application, a base class called "Employee" can be created, which contains the common data and functions required for all types of employees. This class can have virtual functions for calculating pay, which can be overridden by the derived classes to implement their own specific pay calculation logic. The derived classes, such as "FullTimeEmployee" and "PartTimeEmployee", can inherit from the Employee class and implement their own specific logic for calculating pay. They can also override any virtual functions from the base class to implement their own logic. To calculate pay for all types of employees, an array or vector of Employee objects can be created, which can hold objects of all derived classes. A loop can be used to iterate through the array and call the virtual pay calculation function for each object. The polymorphic behavior will ensure that the correct pay calculation function is called for each object, based on its type. Overall, using polymorphism in the payroll application can simplify the code by reducing the need for separate pay calculation functions for each type of employee, and by allowing the use of a single array or vector to store all types of employees.