How to call abstract methods from abstract classes called by inheritance class

I want to call the method of the abstract class from the abstract class called by the inherited class.

Abstract class:

public abstract class Abstract {
protected void updateMotionY(float deltaTime) {
System.out.println("Abstrcat updateMotionY");
}
public void update( float deltaTime) {
this.updateMotionY(deltaTime);
}
}

Inheritance:

public class Obj extends Abstract {
@Override
protected void updateMotionY(float deltaTime) {
System.out.println("updateMotionY");
super.updateMotionY(deltaTime);< br /> }
@Override
public void update(float deltaTime) {
super.update(deltaTime);
}
}

Main method class:

public static void main(String[] args) {
(new Obj()).update(10.0f);
}

Whenever I try to call the new Obj().update() method in the main class, it will print “updateMotionY” and “Abstrcat updateMotionY”. I want to get only “Abstrcat updateMotionY”.

Can anyone tell me how to solve this problem?

(new Obj()).update(10.0f) calls Obj::update calls Abstract: : update calls this.updateMotionY. Because this is an instance of Obj, it calls Obj :: updateMotionY.

This will print “updateMotionY”.

Then call super .updateMotionY(deltaTime), it is Abstract :: updateMotionY.

This will print “Abstrcat updateMotionY”.

This is the end of the call hierarchy, everything is unfolding.

p>

Fundamentally speaking, your confusion seems to stem from this.updateMotionY(deltaTime); in the Abstract class, it is parsed as updateMotionY in the Obj class. That is basically the whole point of polymorphism.

One thing you can do is to add a private method that contains the actual implementation (so that it cannot be overridden), and follow it:

public abstract class Abstract { 
private void motionY(float dt)
{
System.out.println("Abstrcat updateMotionY");
}

protected void updateMotionY(float deltaTime) {
motionY(deltaTime);
}
public void update(float deltaTime) {
motionY(deltaTime);
}
}

I want to call the method of the abstract class from the abstract class called by the inherited class.

Abstract class:

< /p>

public abst ract class Abstract {
protected void updateMotionY(float deltaTime) {
System.out.println("Abstrcat updateMotionY");
}
public void update(float deltaTime) {< br /> this.updateMotionY(deltaTime);
}
}

Inheritance class:

public class Obj extends Abstract { 
@Override
protected void updateMotionY(float deltaTime) {
System.out.println("updateMotionY");
super.updateMotionY(deltaTime);
}< br /> @Override
public void update(float deltaTime) {
super.update(deltaTime);
}
}

Main method class:< /p>

public static void main(String[] args) {
(new Obj()).update(10.0f);
}

Whenever I try to call the new Obj().update() method in the main class, it will print "updateMotionY" and "Abstrcat updateMotionY". I want to get only "Abstrcat updateMotionY".

< p>Can anyone tell me how to solve this problem?

(new Obj()).update(10.0f) calls Obj::update calls Abstract::update calls this.updateMotionY. Because this is Obj's An instance, so it calls Obj::updateMotionY.

This will print "updateMotionY".

Then call super.updateMotionY(deltaTime), which is Abstract::updateMotionY .

This will print "Abstrcat updateMotionY".

This is the end of the call hierarchy, everything is unfolding.

Basically, your The confusion seems to stem from this.updateMotionY(deltaTime); in the Abstract class, it resolves to updateMotionY in the Obj class. That is basically the whole point of polymorphism.

One thing you can do is to add A private method that contains the actual implementation (so that it cannot be overridden), and follow it:

public abstract class Abstract {
private void motionY(float dt)< br /> {
System.out.println("Abstrcat updateMotionY");
}

protected void updateMotionY(float deltaTime) {
motionY(deltaTime);
}
public void update(float deltaTime) {
motionY(deltaTime);
}
}

Leave a Comment

Your email address will not be published.