Home / Questions / Please I need help with the following questions in JAVA programming language. Please comment the cod
Please I need help with the following questions in JAVA programming language. Please comment the code to help me understand, thanks.
Exercise 1: Inheritance
1. Firstly, create and compile a simple class called Parent. Give it the following behaviour:
a. A default constructor that does nothing other than print out “Parent default
constructor” using System.out
b. A single method called getMessage which returns a String, e.g. “Parent message”
2. Next, create and compile a class called Child. Give it the following behaviour
a. Do not give it a constructor
b. Override the parent’s getMessage method to return an alternative String. E.g.
“Child message”
c. A main method which creates an instance of the Child object, and then writes the
value returned by its getMessage method to the command line.
d. What happens when the class is run?
3. Alter the Child class to give it a default constructor which prints out “Child default
constructor”. Compile and run the application again and identify what happens.
4. Alter the implementation of the getMessage method in the Child class so that it first
calls the parent class method, then concatenates the result with its own value to build a
combined message. E.g. returning “Parent message and Child message”.
5. Alter the Parent class by
a. Adding a new constructor that accepts a String argument. This should be used to
initialise a private member variable, myMessage. Again write a message to the
console that indicates that the constructor has been called.
b. Alter the getMessage method so that it returns the value of the myMessage
rather than a fixed message.
c. Now alter the Child class so that it from its default constructor it calls the new
constructor on the parent class.
6. Alter the myMessage member variable in the Parent class so that it is declared to be
protected. Confirm that the Child class can now refer to the variable directly, rather
than having to call its parent’s version of getMessage to build the combined message.
Can you think of the pros and cons of the two different mechanisms?
Exercise 2: Abstract Classes and Methods
1. Create an abstract class called AbstractParent. Give it the following two methods:
a. A public method called getMessage that accepts no parameters and returns a
String. This method should call a second method, generateString that again
just returns a String.
b. Define generateString so that it a protected abstract method.
2. Create a second class called SecondChild, which extends AbstractParent.
a. Implement the generateString method defined by its parent to return a
suitable String
b. Add a main method that creates an instance of SecondChild and invokes its
getMessage method. Can you identify what is happening here?
Apr 02 2020 View more View Less
Equipment(net)$80,000,original equipment is $140,000. Total asset is $132,000. Current liability $24,000, note payable $30,000, total liability $54000. Common stock $50,0...
Sep 17 2020What is the crucial problem in economics?
May 06 2021Only include as incremental expenses in your capital budgeting analysis
Nov 27 2017The temptation is remarkable. The run is long. The body screams, "No more!" So there you are in the middle of a marathon and a shortcut crosses your mind. Cheating on thi...
Jul 25 2021A system comprises a set of interactive sub-systems. Which sub-system consists of jobs that require the interaction of personnel with the general environment?(a)Technical...
Dec 11 2017Which one of the following statements is correct?Over time, the average unexpected return will be zero.The expected return minus the unexpected return is equal to the tot...
Sep 02 2020A local country club runs a very successful â19th Holeâ that golfers frequent for a few drinks after a round of golf. The club also offers hot snacks to these golfers...
Jul 19 2021What is the difference between common risk and independent risk?
Jun 07 2021(4 points) Equilibrium discrimination and crowding. Suppose there are two occupations: teachers and janitors. a) (2 points) Draw hypothetical supply and demand graphs for...
Apr 06 20201. When reviewing a Form 4562, what information will a lender learn about the business?A. The amount of any capital gains tax payments.B. The amount of proceeds received ...
Jun 15 2021