0

I made an interface as:

interface Castle
    {
    public void sad();
    public void cool();
    }

Then i made a child abstract class of it as:

abstract class Castle2 implements Castle
    {
    abstract void sad();
    }

Here I left the implementation of cool(), and if i complile the above code, it compiled Successfully But when i added 1 more sub class of Castle2 as:

class Castle3 extends Castle2{
public void sad(){
System.out.println("SAD");
}
public static void main(String...args){
new Castle3().sad();
}
}

And tried to compile the above code then it is not even compiling my code stating the following error

Castle.java:13: error: Castle3 is not abstract and does not override abstract method cool() in Castle

When i run javap tool on class Castle2, then i got the following result

abstract class Castle2 implements Castle {
Castle2();
public void sad();
}

Why Compiler is Forcing me to implement a interface Castle's method in class Castle3 which is not even present in class Castle2? And Why Compiler is not Forcing me to implement a interface Castle's method in class Castle2?

Shashank
  • 425
  • 5
  • 10
  • 1
    Possible duplicate of [Why an abstract class implementing an interface can miss the declaration/implementation of one of the interface's methods?](http://stackoverflow.com/questions/197893/why-an-abstract-class-implementing-an-interface-can-miss-the-declaration-impleme) – ifly6 Feb 25 '16 at 07:22
  • `cool` is indeed present in `Castle2` (it's inherited). If you have an object `Castle2 x`, you can call `x.cool()`. Therefore, it has to be implemented in every non-abstract subclass. – ajb Feb 25 '16 at 07:22

2 Answers2

0

Because we cannot create object of abstract class. But if abstract class has a subclass, this subclass can be instantiated. Thats why we need implement all methods in subclass

sanky jain
  • 873
  • 1
  • 6
  • 14
0

That is because a concrete class must have implementation because they can be instantiated. Suppose they allow concrete class not to implement all the methods of an interface, there will arise a problem. If in the code we call the unimplemented method, JVM wont be having the address of the unimplemented method.

But abstract classes can not be instantiated. Thats why it is not mandatory to implement methods of an interface by an abstract class.

Subhankar
  • 692
  • 7
  • 25