Reputation: 73
Ok I'm trying to understand how an abstract class treats implemented methods of an interface. Let's say we have an abstract class that implements 3 methods of an interface and we will initialize this abstract class in another class, where we need to implement only of of those interfaces methods. It works perfectly I'm just trying to understand how java re-overrides the interface method in where abstract class is initialized. I just wanna know how this whole thing works.
public abstract class AnimationListenerAdapter implements Animation.AnimationListener
{
@Override
public void onAnimationStart(Animation animation)
{
}
@Override
public void onAnimationEnd(Animation animation)
{
}
@Override
public void onAnimationRepeat(Animation animation)
{
}
}
fadeIn.setAnimationListener(new AnimationListenerAdapter(){
@Override
public void onAnimationEnd(Animation animation)
{
System.out.println("Anim Ended!");
}
});
Upvotes: 0
Views: 79
Reputation: 11120
If an abstract class A
implements interface B
, A
may or may not implement abstract methods declared in B
:
methods implemented in A
, will be concrete, implemented methods, that you can call/invoke on the instance of a class, that directly or indirectly extends A
;
methods not implemented in A
, will have to be implemented in a first encountered (in the hierarchy) concrete class, that extends A
.
Abstract methods of interface, must be implemented in its implementer abstract class, or the responsibility of implementation of those abstract methods, can be handed off down to the concrete class, that extends that abstract class
You cannot initialize (nor you can instantiate), as such, your abstract class explicitly. The only way abstract class's fields are initialized is when its child concrete class is instantiated and child class's constructor, invokes the abstract class's one.
Upvotes: 1
Reputation: 1720
The thing is your abstract class isn't really abstract here. It implements fully the interface by ... doing nothing.
So, when you do your "new AnimationListenerAdapter()", you're no more required to provide the implementation of the three methods, since it has already one implementation ! But, you are providing a new implementation for onAnimationEnd, which now extends the one from AnimationListenerAdapter (which implemented the one from Animation.AnimationListener).
In brief, you have a first layer of implementation from your abstract class to your interface (even though this isn't really an abstract class here, you can just remove the abstract, nothing is abstract in it ...), then you have a second layer of inheritance between your class and your anonymous class overriding one of its method.
Upvotes: 1