1

Consider my code below which serves to book flight ticket using interface

import java.io.*;

interface FlightDetails {
    void getFlightDetails();
    void book(Ticket ticket);
}

class AirIndia implements FlightDetails {
    public void getFlightDetails() {
        System.out.println("Flight Details of Air India");
    }

    public void book(Ticket ticket) {
        System.out.println("Ticket booked in Air India");
    }
}

class Emirates implements FlightDetails {
    public void getFlightDetails() {
        System.out.println("Flight Details of Emirates");
    }

    public void book(Ticket ticket) {
        System.out.println("Ticket booked in Emirates");
    }
}

class Lufthansa implements FlightDetails {
    public void getFlightDetails() {
        System.out.println("Flight Details of Lufthansa");
    }

    public void book(Ticket ticket) {
        System.out.println("Ticket booked in Lufthansa");
    }
}

class Ticket{

}

public class InterfaceFlight {
    public static void main(String[] args) throws IOException {
        FlightDetails flight = null;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("1.Air India\n2.Emirates\n3.Lufthansa");
        System.out.print("Enter choice = ");
        int choice = Integer.parseInt(br.readLine());
        if(choice == 1){
            flight = new AirIndia();
            flight.book(new Ticket());
        } else if(choice == 2){
            flight = new Emirates();
            flight.book(new Ticket());
        } else if(choice == 3){
            flight = new Lufthansa();
            flight.book(new Ticket());
        }
    }
}

This above implementation could have been done without the use of interface by writing separate classes and creating object for the respective flights

Then Why do we need interface??

Thanks in Advance!

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
freakydavid
  • 114
  • 6
  • The `interface` hides the implementation detail of the classes, it also allows the class to be extended from a different base class, which still adhering to the `interface` contract – MadProgrammer May 23 '18 at 05:18
  • "This above implementation could have been done without the use of interface by writing separate classes and creating object for the respective flights" Try actually doing that and see what it's like. Now imagine if you need to do some work on the object then return it, what do you return? – Turksarama May 23 '18 at 05:19
  • Interfaces strength resides in providing generic types, which is the concept of objects with different types being treated in a similar way. – Charfi Omar May 23 '18 at 05:22
  • The answer to this question is in the very basics of "Interface". You should consider digging into OOP concepts first. – Abrar May 23 '18 at 05:26
  • Now I clearly understand the concept – freakydavid May 23 '18 at 05:41

3 Answers3

2

An interface is a contract (or a protocol, or a skeleton or a common understanding) of what the classes can do.

When a class implements a certain interface, it promises to provide implementation to all the abstract methods declared in the interface.

Interface defines a set of common behaviors. The classes implement the interface agrees to these behaviors and provide their own implementation to the behaviors. This allows you to program at the interface, instead of the actual implementation.

In short: A class is just an implementation of that contract nothing else

user207421
  • 305,947
  • 44
  • 307
  • 483
Yati Sawhney
  • 1,372
  • 1
  • 12
  • 19
1

No doubt your code works even without the interface at this moment, but. Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

For example, if you have multiple Flight classes, each implementing FlightDetails , if later on you want to change the structure of the FlightDetails interface, it helps you to easier change the structure of all your Flight classes

Adya
  • 1,084
  • 9
  • 17
0

When a class implements an interface it defines or overrides the blank functions/methods of the interface. All the classes which implements the interface agrees on defining the methods of the interface (like a contract).

Uses:

Interfaces are used to encode similarities which the classes of various types share, but do not necessarily constitute a class relationship.

Another use of interfaces is being able to use multiple inheritance.

Interface is a tool to ensure that classes which have similar behaviors implements the neccessary methods which makes those behaviour.