3

If the base class and sub class implements the same interface and the method implementation of the abstract method is provided in the base class then do we have to provide the implementation in sub class also?

Prashant
  • 51
  • 5

4 Answers4

1

Yes you can, and the implementation from subclass executes when you have initialisation paradigm

BaseClass v = new SubClass();

That's quite normal polymorphism/ovveriding.

Related : Can an interface extend multiple interfaces in Java?

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

do we have to provide the implementation in sub class also

No, you don't have to do it. If one class extends another, it already has all (public and protected) methods declared in parent class.

But you can provide a different implementation of this method. In this case the method from the parent class will be overriden.

Kirill Simonov
  • 8,257
  • 3
  • 18
  • 42
1

There is no need of implementing the same interface in both parent and child classes as because if you are implementing it in parent class then child will also have the same method but if you want to override you can override it.

public interface Shape {
    void draw();
}

class Parallelogram implements Shape {
    public void draw() {
        System.out.println("This is Parallelogram");
    }
}

public class Square extends Parallelogram {
    @Override
    public void draw() {
        System.out.println("This Parallelogram is Square");
    }

    public static void main(String args[0]) {

        Square square = new Square();
        square.draw();
    }

}

//Output This Parallelogram is Square

public class Rhombus extends Parallelogram {

    public static void main(String args[0]) {

        Rhombus rhombus = new Rhombus();
        rhombus.draw();
    }
}

//Output This is Parallelogram

Sandeep Roniyaar
  • 204
  • 1
  • 8
  • 26
Abhishek Gharai
  • 227
  • 3
  • 15
0

It should not be required as subclass already has the method implementation in base class

codeLover
  • 2,571
  • 1
  • 11
  • 27