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?

1
  • 1
    why don't you try it yourself Commented Jan 20, 2018 at 6:12

4 Answers 4

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?

Sign up to request clarification or add additional context in comments.

2 Comments

What if the overridden method in parent class is private?
If you try to make private, you see a compiler error Because you are reducing the visibility of the method.
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.

Comments

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

Comments

0

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

2 Comments

What if the subclass have a different implementation for the same ?
Then you can override that method in child class too

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.