2

This is the first class

package test;

public class Project {

public void doSomething (String stuff) {

    writeStuff();
    whichProject(stuff);

}

public void writeStuff(){

    System.out.println("This is stuff");

}


public void whichProject(String stuff){

    System.out.println("This is a random project " + stuff);

}

}

and this is the derived class

package test;

public class Project1 extends Project{

public void whichProject(String stuff){

    System.out.println("Coding project number one: " + stuff);

}

public static void main(String[] args) {

    Project project = new Project1();

    project.doSomething("stuff");

}

}

When running Project1, the output turns out to be:

This is stuff
Coding project number one: stuff

Why does it call whichProject() in Project1 and not the one in Project? After all, isn't doSomething() a method in Project? or when there is a method in the base class inside of another method in the base class then the object to which the variable refers to still determines which method invocation will be called even though we are inside of another method?

Now, if we change the modifier of whichProject() to private so that the class now is

package test;

public class Project {

public void doSomething (String stuff) {

    writeStuff();
    whichProject(stuff);

}

public void writeStuff(){

    System.out.println("This is stuff");

}


private void whichProject(String stuff){

    System.out.println("This is a random project " + stuff);

}

}

the output becomes:

This is stuff
This is a random project stuff

so now the whichProject() method in Project is being called and not the one Project1 even though the variable refers to an object of Project1. In this case, I do not understand at all what is happening. An explanation for both situations (whichProject() with a public modifier and whichProject() with a private modifier) would be appreciated.

1

3 Answers 3

3

In Java, all methods are virtual.

Virtual methods are methods that can be overridden in a derived class as long as the version in the child class has the same signature (return type and arguments).

So, in the original version of Project and Project1, if you have a Project1, Project1's version of public void whichProject(String stuff) will be called even from code in Project's methods.

However, as stated in section 8.4.8.3 of the Java Language Specification, private methods can't be overridden:

Note that a private method cannot be hidden or overridden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the return type or throws clause of such a method bear any relationship to those of the private method in the superclass.

Hence, when code inside Project's doSomthing calls whichProject, it calls Project's private version instead of Project1's public version.

1

Inheritance follow the bottom up approach.

So writeProject() used is from super class class and whichProject(stuff) is from base class.

you can refer this example inheritance and polymorphism

Hope it helps.

1

For inheritance of methods, use this model:

  1. Collect all methods in the current class.
  2. Add methods from super classes which are visible (i.e. not private) and which you haven't seen so far.
  3. Add this list to the topmost class in the inheritance list

When a method is invoked, Java knows the type of the instance. Imagine that this is passed to each method as an invisible method parameter. Methods aren't resolved by looking at the class in which the method is defined.

Instead, Java takes this, calls getClass() and then gets the list of methods as described above. So even when the code in Project is executed, it still uses the methods from the list for Project1.

This changes when you make the method private. Private methods can't be overwritten, they simply don't show up in the list above. Instead, Java inserts code to call it directly without looking into the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.