S.No: 16 Exp.
Name: Write a Java program to implement Interface Date:2022-12-11
Aim:
Write a Java program that implements an interface.
Create an interface called Car with two abstract methods String getName() and int getMaxSpeed() .
Also declare one default method void applyBreak() which has the code snippet
Page No: 1
System.out.println("Applying break on " + getName());
7 2 5 0 A 1 G 4 1 2 : DI
In the same interface include a static method Car getFastestCar(Car car1, Car car2) , which returns
car1 if the maxSpeed of car1 is greater than or equal to that of car2, else should return car2.
Create a class called BMW which implements the interface Car and provides the implementation for the
abstract methods getName() and getMaxSpeed() (make sure to declare the appropriate fields to store name and
maxSpeed and also the constructor to initialize them).
Similarly, create a class called Audi which implements the interface Car and provides the implementation for
the abstract methods getName() and getMaxSpeed() (make sure to declare the appropriate fields to store name
and maxSpeed and also the constructor to initialize them).
Create a public class called MainApp with the main() method.
Take the input from the command line arguments. Create objects for the classes BMW and Audi then print the
fastest car.
Note:
Java 8 introduced a new feature called default methods or defender methods, which allow developers to
add new methods to the interfaces without breaking the existing implementation of these interface. These default
methods can also be overridden in the implementing classes or made abstract in the extending interfaces. If they
A- E S C- 5 2 0 2- 1 2 0 2
are not overridden, their implementation will be shared by all the implementing classes or sub interfaces.
Below is the syntax for declaring a default method in an interface :
public default void methodName() {
System.out.println("This is a default method in interface");
}
Srinivasa Ramanujan Institute of Technology
Similarly, Java 8 also introduced static methods inside interfaces, which act as regular static methods in
classes. These allow developers group the utility functions along with the interfaces instead of defining them in a
separate helper class.
Below is the syntax for declaring a static method in an interface :
public static void methodName() {
System.out.println("This is a static method in interface");
}
Note: Please don't change the package name.
Source Code:
q11284/MainApp.java
package q11284;
interface Car {
public String getName();
public int getMaxSpeed();
public default void applyBreak(){
System.out.println("applying Break on "+getName());
}
public static Car getFastestCar(Car a,Car b){
Page No: 1
if(a.getMaxSpeed()>b.getMaxSpeed())
return a;
else
return b;
7 2 5 0 A 1 G 4 1 2 : DI
}
}
class BMW implements Car {
String name;
int speed;
public BMW(String n,String s){
speed=Integer.parseInt(s);
name=n;
}
public String getName(){
return name;
}
public int getMaxSpeed(){
return speed;
}
}
class Audi implements Car {
A- E S C- 5 2 0 2- 1 2 0 2
String name;
int speed;
public Audi(String n,String s){
speed=Integer.parseInt(s);
name=n;
}
public String getName(){
Srinivasa Ramanujan Institute of Technology
return name;
}
public int getMaxSpeed(){
return speed;
}
}
public class MainApp {
public static void main(String args[]) {
BMW bmw=new BMW(args[0],args[1]);
Audi audi=new Audi(args[2],args[3]);
Car max=Car.getFastestCar(bmw,audi);
System.out.println("Fastest car is : "+max.getName());
}
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Fastest car is : BMW
Test Case - 2
User Output
Page No: 1
Fastest car is : Maruthi
7 2 5 0 A 1 G 4 1 2 : DI
A- E S C- 5 2 0 2- 1 2 0 2
Srinivasa Ramanujan Institute of Technology