Professional Documents
Culture Documents
interface myinterface
{
void disp();
}
public class MyClass
{
public static void main(String[] args)
{
myinterface m1,m2=null;
m1.disp();
m1.fun();
m1.toString();
m1.equals(m2);
m1.hashCode();
}
}
FlightOpeartions.java(Contract)
?
1 interface FlightOpeartions{
2 void getAllAvailableFlights();
3 void booking(BookingObject bookingObj);
4 }
BookingObject.java
?
1 class BookingObject{}
BritishAirways.java (Vendor 1)
?
1 class BritishAirways implements
2 FlightOpeartions{
3
4 public void getAllAvailableFlights(){
5 //get british airways flights
6 in the way
//they told us to fetch
flight details.
}
7
8 public void booking(BookingObject
9 flightDetails){
10 //place booking order in a way
11 British airways
12 //told us to place order for
13 seat.
}
}
Emirates.java (Vendor 2)
?
1 class Emirates implements
2 FlightOpeartions{
3
4 public void getAllAvailableFlights(){
5 //get Emirates flights in the
6 way
7 //they told us to fetch flight
8 details.
9 }
10
11 public void booking(BookingObject
12 flightDetails){
//place booking order in a way
Emirates airways
//told us to place order for
seat.
}
}
So to summarize,
For Interface:
Interface is used when you don't know anything
about implementation but know the contract that
implementer should have to accomplish the task.
For Abstract class:
Abstract class is used when you know partial
implementation, where say out of 5 methods, you
know implementation of 3 methods and don't know
implementation of 2 methods in that case 2
methods will be abstract and you need to rely on
implementer as a contract to must provide body of
abstract methods to accomplish the task.
7)How many ways we can achieve Polymorphism in
Java?
Ans:-
Polymorphism using class
Class Animal
{
Int legs;
Virtual Void makeSound()
{
Cout<<”Groul”;
}
}
Class Cat extends Animal
{
Void makeSound()
{
Cout<<”meow…”;
}
}
Animal
Bird
IFlying f=list.nextObj();