Program to overcome the problem of Multiple Inheritance in Java using Interface

 

interfaces

Program to overcome the problem of Multiple Inheritance in Java using Interface:

Problem Statement :

Create Vehicle Interface with name, maxPassanger, and maxSpeed variables. Create LandVehicle and SeaVehicle Inteface from Vehicle interface. LandVehicle has numWheels variable and drive method. SeaVehicle has displacement variable and launch method. Create Car class from LandVehicle, HoverCraft from LandVehicle and SeaVehicle interface. Also create Ship from SeaVehicle. Provide additional methods in HoverCraft as enterLand and enterSea. Similarly provide other methods for class Car and Ship. Demonstrate all classes in a application

Introduction:

An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.

Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements.

 Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.

An interface is similar to a class in the following ways:  An interface can contain any number of methods.

An interface is written in a file with a .java extension, with the name of the  interface matching the name of the file.  The byte code of an interface appears in a .class file

             Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name. However, an interface is different from a class in several ways, including:

            You cannot instantiate an interface.

 An interface does not contain any constructors.

All of the methods in an interface are abstract.

            An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.  An interface is not extended by a class; it is implemented by a class.

Program 

import java.util.*;
interface vehicle
 {
    String nm="Nano";
 int maxpassenger=4;
 double maxspeed=15.9;
}
interface landvehicle extends vehicle
{
 int numwheel=4;
 void drive();
void enterland();  
}
interface seavehicle extends vehicle
{
 double displ=20.0;
 void launch();
void entersea();
}
class car implements landvehicle
{
public void drive()
{
System.out.println("Name:"+nm);
System.out.println("Maxpassenger:"+maxpassenger);
System.out.println("Maxspeed:"+maxspeed);
System.out.println("No of wheels:"+numwheel);
}
public void enterland()
{
System.out.println("Entering land");
}
}
class hovecraft implements landvehicle,seavehicle
{

public void enterland()
{
System.out.println("Entering land");
}
public void entersea()
{
System.out.println("Entering sea");
}
public void drive()
{
System.out.println("Drive");
}
public void launch()
{
System.out.println("Launch");
}
}
class ship implements seavehicle
{
public void launch()
{
System.out.println("Name:"+nm);
System.out.println("Maxpassenger:"+maxpassenger);
System.out.println("Maxspeed:"+maxspeed);
System.out.println("Displacement:"+displ);
}
public void entersea()
{
System.out.println("Entering sea");
}
}

class inter
{
public static void main(String args[])
{
System.out.println("*****Land vehicle******");
landvehicle l=new car();
l.drive();
System.out.println("*****Hovercraft vehicle******");
landvehicle l1=new hovecraft();
l1.enterland();
landvehicle l2=new hovecraft();
l2.drive();
seavehicle s1=new hovecraft();
s1.entersea();
seavehicle s2=new hovecraft();
s2.launch();
System.out.println("*****Sea vehicle******");
seavehicle s=new ship();
s.launch();
}

   
}

*****Land vehicle******

Name:Nano     

Maxpassenger:4

Maxspeed:15.9 

No of wheels:4

*****Hovercraft vehicle******

Entering land

Drive

Entering sea

Launch

*****Sea vehicle******

Name:Nano

Maxpassenger:4

Maxspeed:15.9

Displacement:20.0



1 Comments

Previous Post Next Post