2

When I declare the 'abstract public void show();' in the abstract class Test does that create a brand new show() method or just refer to the show() method declared in the interface Inter? Please clarify.

interface Inter
{
    void show();
}

abstract class Test implements Inter
{

     abstract public void show(); //What does this line signify?
}

2 Answers2

0

Explicitly placing an abstract show method in the class has no functional effect - any concrete class that extends this abstract class will have to implement show() anyway, as it's defined in an interface the class implements.

Some coding conventions encourage listing such methods to make their existence more obvious, but it's a matter of taste mostly.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    When I declare the 'abstract public void show();' in the abstract class Test does that create a brand new show() method or just refer to the show() method declared in the interface Inter? Please clarify. – Pratip Chakraborty Jul 07 '18 at 14:21
  • @PratipChakraborty the latter. You could add an `@Override` annotation to that method in the class to make it more obvious. – Mureinik Jul 07 '18 at 14:27
0

As you might have tested out already, removing the declaration in the abstract class produces no error. It can be safely removed.

If I were to speculate the reasons for this redundant line of code, one reason would be making it easier for subclasses of Test to implement the methods required.

Imagine you are trying to write a Test subclass and the line in question were not there. You go to the definition of Test to find what methods to implement, but you find nothing. You'd have to fo to Inter to see what methods you need to implement. Now imagine the chain of inheritance going much deeper. Do you see how many layers of classes you have to look through to see what methods to implement?

However, these kind of problems can be avoided by using a modern IDE like IntelliJ. It tells you what methods you need to implement automatically.

When I declare the 'abstract public void show();' in the abstract class Test does that create a brand new show() method or just refer to the show() method declared in the interface Inter? Please clarify.

That does not create a new method (no hiding). It overrides the method declared in Inter. Here's a screenshot of IntelliJ:

enter image description here

The little "O" with an upwards arrow indicates overriding.

Sweeper
  • 213,210
  • 22
  • 193
  • 313