Classroom Assignment <2>: Demonstrating Polymorphism in Java
Learning Objective:
To understand and implement the concept of Overloading in Java by creating a class and implementing method with
different sets of input arguments.
Expected Completion Time:
Best Case: 20 minutes
Average Case: 25 minutes
Assignment Details:
Create a Java class and create two methods with the same name passing different input arguments.
Requirements:
- Create a Java class.
- Inside this class, define the reportStep method with multiple overloaded versions:
- One version should accept two input arguments: a String for the message (msg) and another String for the status
(status).
- Another version of the reportStep method should accept three input arguments: a String for the message (msg), a
String for the status (status), and a boolean parameter (snap) to indicate whether to take a snapshot.
- Create a main method to demonstrate the usage of the overloaded reportStep method.
- Call both versions of the reportStep method with different sets of input arguments to showcase method
overloading.
Hints to Solve:
- Include print statements inside each methods and pass input values
- Initialize objects and test the functionality by calling methods.
Reference Links:
Overloading in Java - Oracle Docs
Expected Outcome:
Upon completion, you should be able to:
- Grasp the fundamentals of method Overloading in Java.
- Understand the concept of compile time polymorphism
Demonstrating Overriding in Java
Learning Objective:
To understand and effectively utilize method overriding in Java to enable a subclass to provide a specific implementation
for a method defined in its superclass.
Assignment Details:
Create a Java class with a method and create a subclass that inherits from Super class and overrides the method
functionality.
Requirements:
- Create a superclass.
- Implement a method in the Super class.
- Create a subclass and Override that method in the sub class.
- Demonstrate the concept by creating objects for both classes and calling their methods.
Hints to Solve:
- Use appropriate keyword for inheritance.
- Use proper annotation before overriding in the sub class.
- Initialize objects and test the functionality by calling methods.
Reference Links:
Method Overriding in Java - Oracle Docs
Expected Outcome:
Upon completion, you should be able to:
- Grasp how method overriding facilitates polymorphism, enabling the selection of the appropriate method
implementation at runtime based on the object type.