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.

7
  • 2
    What exception did you get? What was the expected behavior? What actually happened? Commented Dec 25, 2019 at 15:24
  • 1
    AbstractMethodError? If so, try recompiling all of your source code/delete all the class files. Commented Dec 25, 2019 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") Commented Dec 25, 2019 at 15:28
  • this.sound is not valid. You need to call the method: this.sound() Commented Dec 25, 2019 at 15:32
  • 1
    @Tom Hawtin - tackline Thank you, its resolved the problem :) Commented Dec 25, 2019 at 15:41

3 Answers 3

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! "); 
   }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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?

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.