-3

I am having trouble with this Java Question:

Consider the following classes:

​
public class Computer extends Mineral {
    public void b() {
        System.out.println("Computer b");
        super.b();
    }

    public void c() {
        System.out.println("Computer c");
    }
}
​
public class Mineral extends Vegetable {
    public void b() {
        System.out.println("Mineral b");
        a();
    }
}
​
public class Animal extends Mineral {
    public void a() {
        System.out.println("Animal a");
    }

    public void c() {
        b();
        System.out.println("Animal c");
    }
}
​
public class Vegetable {
    public void a() {
        System.out.println("Vegetable a");
    }
​
    public void b() {
        System.out.println("Vegetable b");
    }
}

Suppose the following variables are defined:

Vegetable var1 = new Computer();
Mineral   var2 = new Animal();
Vegetable var3 = new Mineral();
Object    var4 = new Mineral();

Indicate on each line below the output produced by each statement shown. If the statement produces more than one line of output indicate the line breaks with slashes as in a/b/c to indicate three lines of output with a followed by b followed by c. If the statement causes an error, write the word error to indicate this.

For the execution of

var1.b()

I was confused about the output

Through careful analysis, we must notice that when we call the method b() of mineral:

public void b() {
        System.out.println("Mineral b");
        a();
    }

We are also calling a method

a()

And therefore, using a class hierarachy diagram, can call the method

Vegetable.a()

source

Bob Smith
  • 75
  • 9
  • 4
    Hey there. Please refrain from depending on third party websites to supply key content for your question. Try to extract the question from the website and include it here on Stack Overflow. – CausingUnderflowsEverywhere Jan 28 '20 at 20:14
  • Sorry, I would do so, but the site disabled copy-pasting, so I had no choice. Hopefully, it is not an issue – Bob Smith Jan 28 '20 at 20:16
  • 3
    Could you try to reword to ask a specific question, the post doesn't seem focused. – CausingUnderflowsEverywhere Jan 28 '20 at 20:26
  • I edited it, basically i am asking if my process of solving these problems is correct. I wrote out my thought process (what i have tried to solve the problem) – Bob Smith Jan 28 '20 at 20:30
  • 1
    You can also use an IDE to verify your logic. I think you should extrapolate on which part of var1.b() you don't understand. And you could recreate the gist of the example in your school material in your question. Your thinking is all correct, so you need to explain what you don't understand. – CausingUnderflowsEverywhere Jan 28 '20 at 20:40
  • 1
    By the way the output of `var1.b()` is `Computer b` / `Mineral b` / `Vegetable a`. You might consider rephrasing your question. – PKey Jan 28 '20 at 20:51
  • Instead of editing your original question with "I understand" - you should accept the correct answer and if you wish - comment on it. – PKey Jan 29 '20 at 06:42

1 Answers1

2

In Vegetable var1 = new Computer();, you have a reference variable of type Vegetable, pointing to an object of type Computer. The assignment is valid, if Vegetable is a super-type of Computer.

The expression var1.b() will be legal (compilation pass), if the type of the reference variable (Vegetable) has a method b(). If the Vegetable type doesn't have a method b(), then the expression will give a compilation error.

If the compilation passes: at runtime, calling var1.b() will invoke the b() method on the object the variable var1 points to (that is, an instance of type Computer). Computer.b() overrides Mineral.b(), so that method will be invoked.

Daniele
  • 2,672
  • 1
  • 14
  • 20