Achiever
Achiever

Reputation: 1636

What mistake I have done in Interface part of the code?

I am beginner in Java. I know the concept of Interface. Interface is mainly used to achieve full abstraction and to support the functionalities of Multiple Inheritance and then loose coupling.

There can be abstract methods and static constants. It cannot be instantiated and similar like Abstract class. Interface is a blueprint of a class and it represents Is-A-Relationship.

I myself just tried this sample program:

interface Printable{
void print();
}
public class A implements Printable{
public void Print(){
    System.out.println("Prints..");
}
public static void main(String args[]){
    A obj=new A();
    obj.print();
}
}

Output is,

Compiling the source code....
$javac A.java 2>&1
A.java:4: error: A is not abstract and does not override abstract method print() in         Printable
public class A implements Printable{
^
A.java:10: error: cannot find symbol
obj.print();
^
symbol: method print()
location: variable obj of type A
2 errors

What does it mean by "A is not abstract and cannot override abstract method print()"?

What mistake I have done here? So I can learn from my mistakes!

Upvotes: 1

Views: 138

Answers (3)

Nikhil
Nikhil

Reputation: 1043

please check your spell of Print(). it is in lowercase in interface

Upvotes: 0

DGomez
DGomez

Reputation: 1480

The error is very clear

A.java:4: error: A is not abstract and does not override abstract method **print()** in         Printable

As you see the method is print() not Print()

Upvotes: 0

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44439

void print()

vs

void Print()

It's a capital mistake.

You are getting the error

A is not abstract and does not override abstract method print()

Because abstract classes can implement an interface without actually implementing the methods it defines.

See here:

In the section on Interfaces, it was noted that a class that implements an interface must implement all of the interface's methods. It is possible, however, to define a class that does not implement all of the interface methods, provided that the class is declared to be abstract.

The other part of the error message is from the above "capital" mistake.

Upvotes: 4

Related Questions