// Base interface
interface Vehicle
{
int MAX_SPEED = 120; // Interface variable (public static final)
void start(); // Abstract method
void stop();
}
// Extended interface
interface ElectricVehicle extends Vehicle
{
void charge();
}
// Implementing the extended interface
class Tesla implements ElectricVehicle
{
public void start()
{
System.out.println("Tesla is starting");
}
public void stop()
{
System.out.println("Tesla is stopping");
}
public void charge()
{
System.out.println("Tesla is charging");
}
void showMaxSpeed()
{
// Accessing interface variable
System.out.println("Maximum speed of Tesla: " + MAX_SPEED + " km/h");
}
}
// Main class
public class TestAllInterfaces
{
public static void main(String[] args)
{
Tesla myCar = new Tesla();
myCar.start(); // Method from Vehicle
myCar.charge(); // Method from ElectricVehicle
myCar.stop(); // Method from Vehicle
myCar.showMaxSpeed(); // Access interface variable
}
}
OUTPUT
Tesla is starting
Tesla is charging
Tesla is stopping
Maximum speed of Tesla: 120 km/h
// Base interface
interface Person
{
String ORGANIZATION = "Tungal School"; // Interface variable
void showRole();
}
// Extended interface
interface Teacher extends Person
{
void teach();
}
// Implementing the extended interface
class MathTeacher implements Teacher
{
public void showRole()
{
System.out.println("Role: Teacher");
}
public void teach()
{
System.out.println("Teaching Math");
}
void showOrganization()
{
// Access interface variable
System.out.println("Organization: " + ORGANIZATION);
}
}
// Main class
public class SchoolSystem
{
public static void main(String[] args)
{
MathTeacher teacher = new MathTeacher();
teacher.showRole(); // Method from Person
teacher.teach(); // Method from Teacher
teacher.showOrganization(); // Accessing interface variable
}
}