Question

In: Computer Science

Write 3 classes with three levels of hierarchy: Base, Derive (child of base), and D1 (child...

Write 3 classes with three levels of hierarchy: Base, Derive (child of base), and D1 (child of Derive class). Within each class, create a “no-arg” method with the same signature (for example void m1( ) ). Each of this method (void m1( ) ) displays the origin of the Class type. Write the TestDynamicBinding Class: (the one with the ‘psvm’ ( public static void main (String[] args)) to display the following. You are NOT allowed to use the “standard object.m1() “to display the message in the main(…) method. That would NOT be polymorphic! (minus 10 points for not using polymorphic method) Explain why your solution demonstrates the dynamic binding behavior? (minus 5 points for lack of explanation) From Parent Class From Derived:Parent Class From Derived:Parent Class From D1:Derived Class Explain why this demonstrates dynamic polymorphism: (minus 4 points for lack of explanation)

Solutions

Expert Solution

class Base
{
void m1()
{
System.out.println("Origin: Base Class");
}
}
class Derive extends Base
{
void m1()
{
System.out.println("Origin: Derived Class");
}
}
class D1 extends Derive
{
void m1()
{
System.out.println("Origin: D1 - Child of Derive Class");
}
}

class TestDynamicBinding
{
public static void main(String args[])
{
Base base = new Base(); // object of Base class
Derive derive = new Derive(); // object of Derive class
D1 d1 = new D1(); // object of D1 class
  
Base reference; // Reference of type Base
reference = base; // reference referring to the object of Base class
reference.m1();   //call made to Base Class m1 method
  
reference = derive;   // reference referring to the object of Derive class
reference.m1(); //call made to Derive Class m1 method
  
reference = d1;    // reference referring to the object of D1 class
reference.m1(); //call made to D1 Class m1 method
}
}

Explanation:

1) Here we have used Method overriding to  supports dynamic binding. A linking procedure call to the overridden method m1() is made at run time rather than doing it at the compile time. The code to be executed for this specific procedural call is also known at run time only.

Base base = new Base(); Base reference;
reference = base; reference.m1() -> makes a call to m1() method of Base class at run time

Similarly,

reference = derive; reference.m1() -> makes a call to m1() method of Derive class at run time

reference = d1; reference.m1() -> makes a call to m1() method of D1 class at run time

2) Base reference; This is the superclass reference and Java determines which method will be executed based upon the type of the object being referred to it at the run time of the call.

eg:

reference = derive; reference.m1().

Here, 'derive' is the object of the Derive class and 'reference' is referred to the it.

So, the method m1() of Derive class is executed at run-time.


Related Solutions

Derive the general equation describing energy levels for a 3-D quantum dot.
Derive the general equation describing energy levels for a 3-D quantum dot.
There are three levels (levels 1 – 3) of assessment that a student must pass to...
There are three levels (levels 1 – 3) of assessment that a student must pass to complete an external course. From previous information, out of 1,000 students who undertook the course, the failures were 150 at level 1; 300 at level 2; and 120 at level 3. To be awarded a degree, students’ needs to pass all three levels. To be awarded a diploma, a student must pass at least 2 levels. Students who can pass only 1 level gets...
Write a program in C or C++ that spawns three child processes. The first child sends...
Write a program in C or C++ that spawns three child processes. The first child sends a signal to the parent (you'll need the parent's pid!), which the parent shall catch. The second child waits for 10 seconds and then terminates. Once the parent detects that both of these has happened, it should signal the third child to terminate.
Refactor the following classes so they are both derived from a base class called Person. Write...
Refactor the following classes so they are both derived from a base class called Person. Write the code for the new base class as well. Try to avoid repeating code as much as possible. Write the classes so that any common methods can be invoked through a pointer or reference to the base class. #include <string> #include <cmath> using namespace std; class Student { private: string name;    int age;    int studyYear; public:    Student(string,int,int); void study();    void...
Write an application and perform the following:(NO USER INPUT) -Create at least three classes such as:...
Write an application and perform the following:(NO USER INPUT) -Create at least three classes such as: 1. listnode- for data and link, constructors 2. Singlelinkedlist-for method definition 3. linkedlist-for objects -Insert a node at tail/end in a linked list. -and display all the nodes in the list. -Delete a node at a position in the list Note: Add these methods in the same application which we have done in the class. this is what we've done in the class: class...
Discuss three (3) of the levels of risk assessment as they could be applied to your...
Discuss three (3) of the levels of risk assessment as they could be applied to your company. For example, Home Depot:        1.    Operational risk assessment: Home Depot faces the risk that their employees are not adequately trained to meet the needs of its customers.        2.    Supply chain risk assessment: Home Depot faces the risk that the quality of the products offered by any supplier declines.        3.    Customer risk assessment: Home Depot faces the risk of another housing...
Derive the saturated model for three way table. Derivation of 3 way contingency table for a...
Derive the saturated model for three way table. Derivation of 3 way contingency table for a saturated log linear model.
Derive a graph showing the market for Apples in Equilibrium. a.) Mention Three (3) facts associated...
Derive a graph showing the market for Apples in Equilibrium. a.) Mention Three (3) facts associated with the equilibrium point for this market: b.) What happens if the price of Apples falls below the Equilibrium Price? c.) What factors a=may shift the Demand Curve for Apples to the right?
JAVA Practice Exam 2 The project practice_exam_2 contains three (3) classes: Testing – This is the...
JAVA Practice Exam 2 The project practice_exam_2 contains three (3) classes: Testing – This is the file you will use to execute your program and test your cases. Each section refers to one or more specific array(s). From here, you can run the whole program or one section at a time. TestCases – This file contains all test cases. NO NEED FOR YOU TO PAY ATTENTION TO IT. YourCode – This is where you will be writing your code. Implement...
3.Write the formula for the conjugate acid of each of the following bases. Base: Conjugate acid:...
3.Write the formula for the conjugate acid of each of the following bases. Base: Conjugate acid: HS− HCO3− CO32− HPO42− SO42− Acid Conjugate base (a) HNO2 (b) H2SO4 (c) H2S (d) HCN (e) HCOOH
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT