Question

In: Computer Science

Can someone explain why this is the case? It would be much appreciated. Consider the following...

Can someone explain why this is the case? It would be much appreciated.

Consider the following class definitions:

public class Foo

{

public Foo() { }  

public void method1()

{

System.out.println(’’Foo 1’’);

}

public void method2()

{

System.out.println(’’Foo 2’’);

}

}

public class Goo extends Foo

{

public Goo() { }

public void method1()

{

System.out.println(’’Goo 1’’);

}

}

public class Hoo extends Goo

{

public Hoo() { }

public void method2()

{

System.out.println(’’Hoo 2’’);

}

}

Given these class definitions, what will be the output of this code fragment?

Foo[] elements = new Foo[3];

elements[0] = new Hoo();

elements[1] = new Foo();

elements[2] = new Goo();

for (int i = 0; i < elements.length; i++) {

elements[i].method1();

elements[i].method2();

}

           Answer:

                             Goo1

                             Hoo2

                        Foo1

                             Foo2

                        Goo1

                             Foo2

Solutions

Expert Solution

Here the classes are declared in Inheritance manner like parent and child class.

If the child class and their parent class have same methods with same signature then those methods are called as "method overriding" . In method overriding what it will do is it checks for which class object is created.For that class we call a method if the method is present in that class it will execute that method or else It goes to it's parent class to check for that method if it is present in that method then it executes that method.This process continues until we get the first parent of class.

For the object created for Hoo() it has method 2 not method 1 so,it takes method 1 from Goo and method 2 from itself.

For Foo() it is the main class so,it directly executes it's own methods

For Goo() it has method 1 only so,it executes  method 1 and goes it's parent Foo() for method 2

If we want to execute the methods of parent class from the child we have to use"Super" Keyword.If we use super keyword in child it goes to it's Parent class for execution

----------------------------------------------------------------------------------------------

Foo[] elements = new Foo[3];

elements[0] = new Hoo();

elements[1] = new Foo();

elements[2] = new Goo();

for (int i = 0; i < elements.length; i++) {

elements[i].method1();

elements[i].method2();

}


Related Solutions

Brokers With so much information available on the internet, why would someone choose to utilize a...
Brokers With so much information available on the internet, why would someone choose to utilize a broker, when investing?
Explain to someone who knows the definition of inflation and not much else in economics why...
Explain to someone who knows the definition of inflation and not much else in economics why if expected inflation and/or inflation risk goes up, interest rates will also likely go up. More admiration for you if you use the present value, future value terminology and jointly present the case for interest rates and bond prices.
Can someone explain in terms of physics why or why not a strong magnet can damage...
Can someone explain in terms of physics why or why not a strong magnet can damage a laptop screen.
Would the following groups benefit if the US dollar appreciated? Yes or No. Explain. Dutch pension...
Would the following groups benefit if the US dollar appreciated? Yes or No. Explain. Dutch pension funds holding US government bonds. US manufacturing industries. Australian tourists planning a trip to the US. An American firm trying to purchase property overseas. American tourists planning a trip to London.
Why would someone need to use a call option? Give a scenario of why someone would...
Why would someone need to use a call option? Give a scenario of why someone would want to use one. Also, pick a stock that has a call option written on it and describe how it would benefit someone who would purchase a call option. Showing math is required.
More “money” is not always better. Explain to someone why they may have too much “money”,...
More “money” is not always better. Explain to someone why they may have too much “money”, and if they are in such a position (having too much “money”) what they should do about it. Note: A definition of “money” will probably help in your explanation.​
Can someone explain why the answer to this question is false? If the same quantities are...
Can someone explain why the answer to this question is false? If the same quantities are supplied by firms, but at higher prices, supply has increased a.) true b.) false
Explain as much as you can. What is motivation, and why is it important in the...
Explain as much as you can. What is motivation, and why is it important in the study of consumer behavior? Can motives be measured, and if so, how? [10 pts]
Why is the formal charge of sulfur on sulfuric acid 0 (would be appreciated if you...
Why is the formal charge of sulfur on sulfuric acid 0 (would be appreciated if you could work it out) and what do we do when calculating formal charge and we see a double bond?
Can someone without much intellect become a software engineer?
Can someone without much intellect become a software engineer?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT