5

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?

Greg Oks
  • 2,700
  • 4
  • 35
  • 41

7 Answers7

9

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.

raistlin0788
  • 406
  • 4
  • 12
  • +1, though properly speaking, I believe the answer should be "Yes and No" given that the OP actually asked two questions (you only answered the first with words, but the second with code). – CPerkins Oct 05 '11 at 18:08
1

You could have two abstract classes, X and Y, where Y extends X. In that case it could make sense for Y to implement an abstract method of X, while still being abstract. Another non-abstract class Z could extend Y. However, in your example, for A to implement its own abstract methods is a contradiction, the point of making them abstract is so it doesn't provide implementations, it just specifies what the method signatures should look like.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
1

If you implement an abstract method it's not really abstract any more, so no.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
1

Abstract classes can have regular methods. If you want to implement some of the methods of class A and leave rest of the methods abstract, you can do this. However, abstract methods cannot have a body, therefore if you mark a method as abstract, then it has to be implemented by a subclass, you can't implement them in the abstract class. However, you can have an abstract class without abstract methods, then subclass only needs to extend it.

reader_1000
  • 2,473
  • 17
  • 15
1

Yes, you can implement abstract methods in a class which is declared as abstract. If a class is declared abstract that does not mean all its method must be abstract.

For a concrete sub class, it is not mandatory to implement the abstract methods that are already implemented by one of their super class.

Keen Sage
  • 1,899
  • 5
  • 26
  • 44
0

No. Abstract methods are meant to be defined by the subclass(es). For more information, see Abstract Methods and Classes. However, you can define a method in the base class and have the subclass(es) override it, if required.

mre
  • 43,520
  • 33
  • 120
  • 170
0

Yes, but it can't be abstract any more. Abstract means there is no implementation.

What you can do is:

interface I {
    void meth();
}

//and

abstract class A implements I {
    public void meth() {
        //implementation
    }
}

Or:

abstract class A {

    public abstract void meth();
}

//and

abstract class B extends A {
    public void meth() {
    }
}

If A already has an implementation, you can override it in B (if B is concrete), because B inherits that default implementation from A.

andronikus
  • 4,125
  • 2
  • 29
  • 46
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142