0


I tried to implement a function in a base class which using the function of the childs (defiend as a abstract function in the base class). I think an example will demonstrate the problem in the best way.

abstract class Animal{
   public void doSomthing(){
      this.sound();
   }

   protected abstract void sound();
}

class Dog extends Animal{
   @Override
   protected void sound(){ 
   System.out.println("WAF"); 
 }
}

now when I tried to get the element in run time (by factory method which looks like: Animal factory method("Dog);) and call to the doSomthing method I got exception because it goes to the abstract method, my question is if there is any way the bypass this or another solution for this problem.

dassum
  • 4,727
  • 2
  • 25
  • 38
Lupidon
  • 599
  • 2
  • 17
  • 2
    What exception did you get? What was the expected behavior? What actually happened? – Shloim Dec 25 '19 at 15:24
  • 1
    `AbstractMethodError`? If so, try recompiling all of your source code/delete all the class files. – Tom Hawtin - tackline Dec 25 '19 at 15:26
  • The excepted behavior is that the instance will invoke the method **doSomthing** but when it will pass over the code of the function it will invoke the **sound** method of the child class ("Dog") – Lupidon Dec 25 '19 at 15:28
  • `this.sound` is not valid. You need to *call the method*: `this.sound()` – QBrute Dec 25 '19 at 15:32
  • If you expect an answer, then give us what we need. where is your factory class? we are not sure what your factory method is doing. Also, update your `Animel` class to not have compilation errors. `this.sound` wouldn't do anything but give error – Sunil Dabburi Dec 25 '19 at 15:33
  • Your code contain some compilation error. this.sound should be this.sound(). Just share the factory code. Your Factory should return something like Animal a = new Dog(); Then it will work as expected. – dassum Dec 25 '19 at 15:34
  • 1
    @Tom Hawtin - tackline Thank you, its resolved the problem :) – Lupidon Dec 25 '19 at 15:41
  • @CoderIL - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 28 '20 at 19:24

3 Answers3

2
 class myMain
{
   public static void main(String[]args)
   {
    Animal doggo = new Dog(); // create object for dog
    doggo.animalSound(); // call the sound for dog

   }
}
    class Animal 
{
  public void animalSound() 
  {
    System.out.println("The animal makes a sound");
  }
}
class Dog extends Animal
{
   public void animalSound()
   {
     System.out.println("The Dog Says bow wow! "); 
   }
}
sireee
  • 106
  • 9
1

I do not see any problem with the approach you have mentioned in the description of your question. Maybe you are doing some other mistake. Check the following working code:

abstract class Animal {
    public void doSomthing() {
        sound();
    }

    protected abstract void sound();
}

class Dog extends Animal {
    @Override
    protected void sound() {
        System.out.println("WAF");
    }
}

class AnimalFactory {
    static Animal animal;

    public static Animal factoryMethod(String animalName) {
        if ("Dog".equals(animalName)) {
            animal = new Dog();
        }
        return animal;
    }
}

class Main {
    public static void main(String[] args) {
        Animal animal = AnimalFactory.factoryMethod("Dog");
        animal.sound();
    }
}

Output:

WAF
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

The call to child class method from super class can be done. Refer code snippet mentioned in below link: Can a Parent call Child Class methods?