Rosifr Rosifr - 23 days ago 6
Java Question

How to Implement Parent Class Method From Child Class Methods

In the parent class we have an abstract method:

public abstract void parentMethod();


and in the children classes a method is defined as such:

public void parentMethod()
{
System.out.println("this is child class 1")
}


and

public void parentMethod()
{
System.out.println("this is child class 2")
}


respectively.

How would the implementation of the parent classes be completed?

Answer

You can't call children methods from a parent class. Consider reversing the hierarchy (making the parent the child class instead) or make the method part of an external handler that processes objects of the class. Can you provide a sample code then I can advise you further?

Edit:

Here I'm assuming you mean you want to call the child method using a parent container. Doing this it would seem like you're having a parent object but it would call the child method.

abstract class Parent 
{
     abstract public void parentMethod();
}

class ChildClass extends Parent
{
    @Override
    public void parentMethod()
    {
        System.out.println("Child method");
    }
 }

And then you just Parent child = new ChildClass(); and child.parentMethod(); if I understand you correctly.