Example11:- Test.java
interface A
{
void disp();
}
class Test implements A
{
}
Output:-
C:\JAVATECH>javac Test.java
Test.java:5:
error: Test is not abstract and does not override abstract method disp() in A
class
Test implements A
^
1
error
Explanation:
-Interface
methods must be override in implemented class otherwise class must be declared
as abstract.
Example12:- Test.java
interface A
{
void disp();
}
abstract class Test implements A
{
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>javap Test
Compiled
from "Test.java"
abstract
class Test implements A {
Test();
}
Example13:- Test.java
interface A
{
void disp();
}
abstract class Test implements A
{
void disp()
{
System.out.println("Implemented method");
}
}
Output:-
C:\JAVATECH>javac Test.java
Test.java:7:
error: disp() in Test cannot implement disp() in A
void
disp()
^
attempting to assign weaker access
privileges; was public
1
error
Explanation:
-by
default interface methods are public and abstract so in the implemented class
we cannot decrease visibility but we can increase.
Example14:- Test.java
interface A
{
void disp();
}
abstract class Test implements A
{
public void disp()
{
System.out.println("Implemented method");
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>javap Test
Compiled
from "Test.java"
abstract
class Test implements A {
Test();
public void disp();
}
Comments
Post a Comment