implementing abstract methods/classes in java

Greg Oks picture Greg Oks · Oct 5, 2011 · Viewed 29.6k times · Source

Can I implement abstract methods in an abstract base class A in java?

If the answer is yes and there is an implemented abstract method in a base class A and there is a derived class B from A (B is not abstract). Does B still has to implement that base abstract method?

Answer

raistlin0788 picture raistlin0788 · Oct 5, 2011

If I understand your question correctly, Yes.

public abstract class TopClass {
  public abstract void methodA();
  public abstract void methodB();
}

public abstract class ClassA extends TopClass {
  @Override
  public void methodA() {
    // Implementation
  }
}

public class ClassB extends ClassA {
  @Override
  public void methodB() {
    // Implementation
  }
}

In this example, ClassB will compile. It will use it's own implementation of methodB(), and ClassA's implementation of methodA(). You could also override methodA() in ClassB if desired.