Class ABC implements Interface containing method m1 and m2 Class ABC has provided implementation for method m2 Can you create object of Class ABC

Class ABC implements Interface containing method m1 and m2 Class ABC has provided implementation for method m2 Can you create object of Class ABC

Answer: By default all method of interface are abstract, If any class implements interface then it’s mandatory to implements all its method otherwise you will get compile time exception and you can not create object of class. Please have example below:

Interface:

package com.javahonk.interfacetest;

public interface AInterface {
	public void m1();
	public void m2();
}

 

Class which implements interface with only one method:

package com.javahonk.interfacetest;

public class ABC implements AInterface{

	public static void main(String[] args) {
		System.out.println("testing");

	}

	@Override
	public void m2() {
		// TODO Auto-generated method stub

	}

}

 

If you try to compile above class you will below error:

C:\workspace\MainMethodTest\src\com\javahonk\interfacetest>javac ABC.java
ABC.java:3: cannot find symbol
symbol: class AInterface
public class ABC implements AInterface{
                            ^
ABC.java:10: method does not override or implement a method from a supertype
        @Override
        ^
2 errors

 

Leave a Reply

Your email address will not be published. Required fields are marked *