Question: ----------------------------- package inherit; public class SubClass{ } ------------------------ package inherit; public class SuperClass { } --------------------- package test; import inherit.SubClass; import inherit.SuperClass; import junit.framework.TestCase; public

-----------------------------
package inherit;
public class SubClass{
}
------------------------
package inherit;
public class SuperClass {
}
---------------------
package test; import inherit.SubClass; import inherit.SuperClass; import junit.framework.TestCase;
public class TestInherit extends TestCase{ SubClass sub; SuperClass sup; SuperClass subsup; public void setUp(){ sub = new SubClass(); sup = new SuperClass(); subsup = new SubClass(); } public void test01(){ assertEquals("Sub method A", sub.methodA()); assertEquals("Sub method B", sub.methodB()); }
public void test02() { assertEquals("Super method A", sup.methodA()); assertEquals("Super method B", sup.methodB()); } public void test03() { assertEquals("Sub method A", subsup.methodA()); assertEquals("Super method B", subsup.methodB()); } }
-----------------------
write the code of the program above by using java language.
CompSci 251: Intermediate Computer Programming Lab 5 - Spring 2020 Introduction This week's lab will look at one aspect of polymorphism, static and dynamic binding. We will have a subclass and a superclass. We will assign instances of these objects to different variables and call the same method on them, exploring whether an overridden version is called. Dynamic Binding vs Static Binding When we assign an instance of a subclass to a variable of a superclass, the compiler continues to treat that variable as if it is a reference to an instance of the superclass. However, when the program is running, it can see that the object referred to by the variable is actually an instance of the subclass Now when we call a method on this object, we might call version from the superclass, or we might call the version from the subclass. In general, in Java, we wait until runtime to decide which version to execute. This is called dynamic binding, and it results in the method for the subclass being called. However, in cases where we cannot actually override the method, the compiler decides which method to call, and it will be the method of the superclass. This is called static binding, and happens when the method is declared as final (can't be overridden), private, or static. What to do You are given Testinherit.java. You must implement SuperClass and SubClass in such a way that all the tests will pass. Pay careful attention to what the tests expect, and make use of static and dynamic binding to solve this puzzle. Note: your solution may result in a warning, since you may have to do something slightly unusual to pass these tests. That's ok. To Get Credit Bare minimum: Finish SubClass and SuperClass, and get testo1 and testo2 to pass. Good: Get all tests to pass. CompSci 251: Intermediate Computer Programming Lab 5 - Spring 2020 Introduction This week's lab will look at one aspect of polymorphism, static and dynamic binding. We will have a subclass and a superclass. We will assign instances of these objects to different variables and call the same method on them, exploring whether an overridden version is called. Dynamic Binding vs Static Binding When we assign an instance of a subclass to a variable of a superclass, the compiler continues to treat that variable as if it is a reference to an instance of the superclass. However, when the program is running, it can see that the object referred to by the variable is actually an instance of the subclass Now when we call a method on this object, we might call version from the superclass, or we might call the version from the subclass. In general, in Java, we wait until runtime to decide which version to execute. This is called dynamic binding, and it results in the method for the subclass being called. However, in cases where we cannot actually override the method, the compiler decides which method to call, and it will be the method of the superclass. This is called static binding, and happens when the method is declared as final (can't be overridden), private, or static. What to do You are given Testinherit.java. You must implement SuperClass and SubClass in such a way that all the tests will pass. Pay careful attention to what the tests expect, and make use of static and dynamic binding to solve this puzzle. Note: your solution may result in a warning, since you may have to do something slightly unusual to pass these tests. That's ok. To Get Credit Bare minimum: Finish SubClass and SuperClass, and get testo1 and testo2 to pass. Good: Get all tests to pass
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
