top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can abstract class implements another interface in java?

+2 votes
464 views
Can abstract class implements another interface in java?
posted Mar 6, 2016 by Deepak Jangid

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

Yes, abstract class can implement interface by using implements keyword. Since they are abstract, they don’t need to implement all methods. It’s good practice to provide an abstract base class, along with an interface to declare Type. One example of this is java.util.List interface and corresponding java.util.AbstractList abstract class. Since AbstractList implements all common methods, concrete implementations like LinkedList and ArrayList are free from burden of implementing all methods, had they implemented List interface directly. It’s best of both world, you can get advantage of interface for declaring type, and flexibility of abstract class to implement common behavior at one place.

answer Mar 7, 2016 by Karthick.c
0 votes

A curious thing happens in Java when you use an abstract class to implement an interface: some of the interface's methods can be completely missing (i.e. neither an abstract declaration or an actual implementation is present), but the compiler does not complain.
For example, given the interface:

public interface IAnything {
void m1();
void m2();
void m3();
}
the following abstract class gets merrily compiled without a warning or an error:

public abstract class AbstractThing implements IAnything {
public void m1() {}
public void m3() {}
}

answer Mar 9, 2016 by Ashish Kumar Khanna
...