PhysicsTeacher.in

High School Physics

Java program – how to implement an Interface through an abstract class

Here is a Java program that helps you to understand how to implement an Interface through an abstract class.

Type, compile, and run the program to understand one specific type of implementation of Interfaces in a Java program via abstract classes.

Program strategy – One abstract class first implements the Interface, but as it is abstract it doesn’t define the methods of the interface. Then one more class is written that extends or inherits the abstract class and defines the method bodies declared in the Interface. Thus the implementation of the interface is done by a class (via a separate abstract class).

Java program – how to implement an Interface through an abstract class

interface IntTest
{
void callBack();
int ivar=10;//assigning a value is a must
}
abstract class InterfaceTest implements IntTest//must be defined abstract
{

}
class InterfaceTest1 extends InterfaceTest
{
public void callBack() //must be declared public
{
System.out.print(“In callBack method”);
// ivar=2; wont compile,error: cannot assign a value to final variable ivar
}

public static void main(String args[])
{
InterfaceTest1 i1=new InterfaceTest1();
i1.callBack();
}
}

See also  Chemical equation and balancing chemical equations
Scroll to top
error: physicsTeacher.in