0

I have three classes like below

public abstract class Parent {
    public void parentCallerMethod() {
        getMethod(helperMethod());
    }
    public abstract void getMethod(String test);

    public String helperMethod() {
        System.out.println("inside parent helperMethod");
        return "inside parent helperMethod";
    }
}

2nd class

public class Child extends Parent {
    @Override
    public void getMethod(String test) {
        System.out.println("Inside child getMEthod....");
    }
} 

Final Class that calls the parent class method

public class FinalClass {
    private void testMethod() {
        new Child().parentCallerMethod();
    }
}

My question is what does new Child().parentCallerMethod(); do? What's new Child(). Why is that I won't be able to do Child.parentCallerMethod()

Is it similar to doing Child child = new Child();

PS : The post title may be wrong. If it's wrong I'll change it based on the answer.

Damien-Amen
  • 7,232
  • 12
  • 46
  • 75
  • Isnt problem that `parentCallerMethod` isnt static ? Then you cant do `Child.parentCallerMethod()` – Ziker Jun 03 '15 at 14:28
  • @Ziker : Yes if I make that static then I will be able to do `Child.parentCallerMethod()` however I'd like to understand what `new Child().parentCallerMethod();` does? – Damien-Amen Jun 03 '15 at 14:30
  • Sounds like your fairly new to coding. new Child() is creating a new instance of the Class Child. You need to have an object to get to the parent method. If you want you could change your methods and or classes static to get access to the method and not create an instance of the class. – edjm Jun 03 '15 at 14:33

4 Answers4

3

new Child().parentCallerMethod() is the same as:

Child child = new Child();
child.parentCallerMethod();

but without conserving the instance variable.

It will call the parent method, since Child doesn't override it. You are not able to call Child.parentCallerMethod() because this method is not static (class method).

Héctor
  • 24,444
  • 35
  • 132
  • 243
0

new Child().parentCallerMethod(); creates a new instance of your Child class, and then calls the method on the instance of your Child class.

Child.parentCallerMethod(); would be calling a static method of the class Child. Which does not exist, so that's why you are not able to call it.

Kuurde
  • 887
  • 2
  • 8
  • 24
0

new Child() creates an instance of the child class. object.something() calls method something on the result of the expression object. So if you substitute new Child() for object and parentCallerMethod for something, you get new Child().parentCallerMethod(), which creates a new Child object and calls the parentCallerMethod method on it.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
0

The code new Child().parentCallerMethod() creates a new Child object and calls the method on that object.

The call Child.parentCallerMethod() would be a static method call, but parentCallerMethod is not declared static. A static method means that the method belongs to the class and cannot access any members of a child object as there is no object.

The new Child() call creates a Child object which could contain fields. In your case, Child doesn't actually contain any fields, so there is nothing to access.

If it did, your methods would be able to access data in the object.

rghome
  • 8,529
  • 8
  • 43
  • 62
  • Is it similar to doing Child child = new Child(); – Damien-Amen Jun 03 '15 at 14:32
  • 1
    Yes. Pretty much exactly the same, except you are not actually assigning the reference of the Child object to anything, so once the method call finishes the Child object will probably no longer be accessible. – rghome Jun 03 '15 at 14:34