-5

what is need of abstract methods of abstract Class or methods of interfaces, when we have to provide their implementation in Child Classes. isn't it better to provide this methods directly in child Classes rather than making them abstract methods in abstract Class or in interfaces ??

Sumit
  • 7
  • 2
  • 1
    The point is reusability and reducing redundancy, or in simpler words, you can implement only the methods you need, and can reuse the original abstract Class again and again. – Keyur PATEL Jun 13 '17 at 09:58
  • but what is the actual need. when you have to provide whole implementation of method again and again in all child class, then where Comes Code reusability and where is reducing redundancy??? – Sumit Jun 13 '17 at 10:01
  • I believe you should really start by studying how inheritance works. This question arises from the lack of knowledge on what polymorphism is. – Federico Dipuma Jun 13 '17 at 10:09
  • I have an idea of inheritance and oops, but I am asking the actual practical use of that Concept. not the theoretical type definition that interface is like a "Contract" between Class and interface. my Q is what is real benefit. where is Code reusability Comes in picture from implementing abstract methods. – Sumit Jun 13 '17 at 10:18
  • What I'm saying is that if you do not understand the benefits of abstract methods and interfaces, then you need to deepen inheritance concepts. [Here](https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html) you may find a good explanation. – Federico Dipuma Jun 13 '17 at 10:20
  • Possible duplicate of [Interface vs Abstract Class (general OO)](https://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo) – Willem van Ketwich Jun 13 '17 at 11:13
  • Abstract methods are just a contract that says if you implement this abstract class you must have this method in the class. – JWP Jun 13 '17 at 13:25

1 Answers1

1

If you define abstract methods in an abstract class, the non-abstract methods of the abstract class can call these methods without knowing how they will be implemented.

Besides, defining abstract methods in either abstract classes or interfaces allows you to write code that only relies on the abstract class or interface methods, without depending on the concrete sub-classes or implementing classes.

For example:

public abstract class AbstractClass
{
    public abstract void method1();

    public void method2() {
        ...
        method1();
        ...
    }
}

public class ConcreteClass extends AbstractClass
{
    public void method1() {
        ...
    }
}

Without defining method1 as abstract in AbstractClass, you couldn't call method1 from method2 in AbstractClass, since it is only implemented in the concrete sub-class ConcreteClass.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • my question is when you have to define method1 in ConcreteClass then what is benefit of defining it in "AbstractClass". what problem in this public class ConcreteClass { public void method1() { ... } } – Sumit Jun 13 '17 at 10:07
  • @Sumit As I have shown in the code sample, it allows you to call `method1` from `AbstractClass` even though `AbstractClass` doesn't have an implementation of that method. – Eran Jun 13 '17 at 10:08
  • that's what I am asking, what is use of "method1" of "AbstractClass" when it doesn't have any implementation, why not to use it directly in ConcreteClass. – Sumit Jun 13 '17 at 10:12
  • @Sumit `AbstractClass` can still call a method that it doesn't implement itself (and is implemented by its concrete sub-classes instead). That's a common design pattern (see https://en.wikipedia.org/wiki/Strategy_pattern). – Eran Jun 13 '17 at 10:48