Dharmendra Awasthi
Dharmendra Awasthi

Reputation: 9

Implementing interface to a abstract class Java

I was testing a program in which i was trying to implement a interface on abstract class . as given below

 interface Inf{
    void display();
}

abstract class InfTst implements Inf{

}

class InterfaceTest extends InfTst{
    void display(){
        System.out.println("Hello");
    }
}

but it is shows error

error: display() in InterfaceTest cannot implement display() in Inf void display(){

what is this error means and how resolve it, please help me.

Upvotes: 0

Views: 84

Answers (2)

Onic Team
Onic Team

Reputation: 1658

In concrete classes by default package is private. A concrete method means, the method have complete definition so must be your method modifier are always public.

   package domain;
    interface Inf {
      void display();
     }
    abstract class InfTst implements Inf {
    }

 class InterfaceTest extends InfTst {
    public void display() {
    System.out.println("Hello");
}
}

 public class StackOverFlow extends InterfaceTest {
  public static void main(String[] args) {

    StackOverFlow sof = new StackOverFlow();
    sof.display();

}
}

output : - Hello

Upvotes: 0

Y.E.
Y.E.

Reputation: 922

When you omit access modifier on an interface it defaults to public, but on concrete classes it defaults to package-private.

Change your method signature to below in concrete class InterfaceTest

public void display(){..}

Upvotes: 4

Related Questions