class ExtendsInterfaceDemo
{
public static void main(String arg[])
{
vehicle v1 = new Bike(); // LINE A
v1.companyName("Honda");
v1.vehicleName("Shine");
v1.vehicleType(2);
v1.seatArrangement(2);
OtherFeatures v2 = new Car();
v2.companyName("Hyundai"); // LINE B
v2.vehicleName("i10");
v2.vehicleType(4);
v2.seatArrangement(5);
v2.mileagePerLiter(20);
}
}
interface vehicle // LINE C
{
void companyName(String name);
void vehicleName(String name);
void vehicleType(int i);
void seatArrangement(int i);
}
interface OtherFeatures extends vehicle // LINE D
{
void mileagePerLiter(int i);
}
class Bike implements vehicle // LINE E
{
public void companyName(String name)
{
System.out.println(name + "'s product.");
}
public void vehicleName(String name)
{
System.out.println("Name of vehicle : " + name);
}
public void vehicleType(int i)
{
System.out.println(i + " wheeler.");
}
public void seatArrangement(int i)
{
System.out.println(i + " seater capacity.");
}
}
class Car extends Bike implements OtherFeatures // LINE F
{
public void mileagePerLiter(int i)
{
System.out.println("Mileage : " + i + " Kmpl.");
}
}
Honda's product.
Name of vehicle : Shine
2 wheeler.
2 seater capacity.
Hyundai's product.
Name of vehicle : i10
4 wheeler.
5 seater capacity.
Mileage : 20 Kmpl.
In the above program at LINE C
, we have created an interface vehicle
with abstract methods companyName
, vehicleName
, vehicleType
, seatArrangement
and we also created one more interface otherFeatures
at LINE D
, with mileagePerLiter
method which extends vehicle
. we have create two classes Bike
and Car
at LINE E
and LINE F
, Bike
implements vehicle
it provides body for vehicle
methods and Car
implements otherFeatures
and provides body for only mileagePerLiter
, since Car
extends Bike
methods in Bike
are accessible by Car
also. At LINE A
we have created an object for Bike
with vehicle
reference and accessed the methods of Bike class. At LINE B
we have created another object for Car
with otherFeatures
reference
LINE E
in class Bike remove the method companyName
it shows a compilation error as java.lang.Error: Unresolved compilation problem:
The type Bike must implement the inherited abstract method vehicle.companyName(String) v1.mileagePerLiter(20);