0% found this document useful (0 votes)
124 views18 pages

Real Time Scenario For Abstract Class and Interface: Industry Oriented Java-1

1. In this scenario, an interface would be useful because we only need to define the contract methods that vendors need to provide to get flight details and book flights, without knowing their specific implementations. 2. An abstract class could also work if we knew some behaviors but needed to leave certain methods abstract, like how to connect to vendors' systems, to be implemented by each vendor. 3. Overall, an interface fits best here as we only need to define the standard methods for vendors to return flight data and handle bookings without concerning ourselves with their individual processes.

Uploaded by

dev eloper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views18 pages

Real Time Scenario For Abstract Class and Interface: Industry Oriented Java-1

1. In this scenario, an interface would be useful because we only need to define the contract methods that vendors need to provide to get flight details and book flights, without knowing their specific implementations. 2. An abstract class could also work if we knew some behaviors but needed to leave certain methods abstract, like how to connect to vendors' systems, to be implemented by each vendor. 3. Overall, an interface fits best here as we only need to define the standard methods for vendors to return flight data and handle bookings without concerning ourselves with their individual processes.

Uploaded by

dev eloper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Real Time Scenario for Abstract

class and Interface

Industry Oriented Java-1


Abstract Class

• A class that is declared using “abstract” keyword is


known as abstract class.
• It can have abstract methods(methods without body) as
well as concrete methods (regular methods with body).
• A normal class(non-abstract class) cannot have abstract
methods.
Need an abstract class?

• We have a class Animal that has a method sound() and the subclasses of it
like Dog, Lion, Horse, Cat etc.
• Since the animal sound differs from one animal to another, there is no point to
implement this method in parent class.
• This is because every child class must override this method to give its own
implementation details, like Lion class will say “Roar” in this method
and Dog class will say “Woof”.
Real Problem Scenario

• Consider we want to start a service like "makemytrip.com" or


"expedia.com", where we are responsible for displaying the flights from

various flight service company and place an order from customer.

• Lets keep our service as simple as, Displaying flights available from
vendors like "airasia", "british airways" and "emirates".
• Place and order for seat to respective vendor.

How should we design our application considering interfaces and abstract class? In this
scenario, interface is useful or abstract class?

We are just a middle man/aggregator and our task is to first enquire "airasia", then enquire
"british airways" and at last enquire "emirates" about the list of flights available and later if
customer opts for booking then inform the respective flight vendor to do booking.
Hint

• For this, first we need to tell "airasia", "british


airways" and "emirates" to give us list of flights,
internally how they are giving the list that we don't
care.
• This means I only care for method
"getAllAvailableFlights()"
Hint(Contd..)

• "getAllAvailableFlights()" from "airasia" to return list of


flights.
"getAllAvailableFlights()" from "british airways" to
return list of flights.
• "getAllAvailableFlights()" from "emirates" to return list
of flights.
Solution
• In this situation Interface is useful.
• Because we are not aware of the implementation of all the 2
methods required, and what we know is the contract methods that
vendor(implementer) should provide.
• so due to this total abstraction and for defining the contract,
interface is useful in this place.
Solution Scenario

interface FlightOpeartions{
void getAllAvailableFlights();
void booking(BookingObject bookingObj);
}
class BookingObject{}
Solution Scenario(Contd..)
class BritishAirways implements FlightOpeartions{

public void getAllAvailableFlights(){


//get british airways flights in the way
//they told us to fetch flight details.
}

public void booking(BookingObject flightDetails){


//place booking order in a way British airways
//told us to place order for seat.
}
}
Solution Scenario(Contd..)
class Emirates implements FlightOpeartions{

public void getAllAvailableFlights(){


//get Emirates flights in the way
//they told us to fetch flight details.
}

public void booking(BookingObject flightDetails){


//place booking order in a way Emirates airways
//told us to place order for seat.
}
}
Solution Scenario(Contd..)
• Scenario,
Consider we want to start a service like Bulk SMS sender, where we take orders from various
telecom vendors like Airtel, France Telecom, Vodafone etc.
For this, we don't have to setup our own infrastructure for sending SMS like Mobile towers but we need to
take care of government rules like after 9PM, we should not send promotional SMS, we should also not send
SMS to users registered under Do Not Disturb(DND) service etc. Remember, we need to take care of
government rules for all the countries where we are sending SMS.
Note: for infrastructure like towers, we will be relying on vendor who is going to give us order.
Example, In case of,
Vodafone request us for bulk messaging, in that case we will use Vodafone towers to send SMS.
Airtel request us for bulk messaging, in that case we will use Airtel towers to send SMS.
What our job is to manage Telecom Regulations for different countries where we are sending SMS.
Solution Scenario(Contd..)
public void eastablishConnectionWithYourTower(){
//connect using vendor way.
//we don't know how, candidate for abstract method
}

public void sendSMS(){


eastablishConnectionWithYourTower();
checkForDND();
checkForTelecomRules();
//sending SMS to numbers...numbers.
destroyConnectionWithYourTower()
}

public void destroyConnectionWithYourTower(){


//disconnect using vendor way.
//we don't know how, candidate for abstract method
}

public void checkForDND(){


//check for number present in DND.
}

public void checkForTelecomRules(){


//Check for telecom rules.
}
Solution Scenario(Contd..)
• Out of above 5 methods,
Methods we know is "sendSMS()", "checkForDND()", "checkForTelecomRules()".
• Methods we don't know is "eastablishConnectionWithYourTower()",
"destroyConnectionWithYourTower()".
• we know how to check government rules for sending SMS as that is what our job is but
we don't how to eastablish connection with tower and how to destroy connection with tower
because this is purely customer specific, airtel has its own way, vodafone has its own way etc.
So in the given scenario, we know some methods but there also exist some methods which are unknown and
depends on customers.
In this case, what will be helpful, abstarct class or interface?
Solution
• In this case, Abstract class will be helpful, because you know partial things
like "checkForDND()", "checkForTelecomRules()" for sending sms to users
but we don't know how to eastablishConnectionWithTower() and
destroyConnectionWithTower() and need to depend on vendor specific way
to connect and destroy connection from their towers.
Solution Scenario(Contd..)
abstract class SMSSender{

abstract public void eastablishConnectionWithYourTower();

public void sendSMS(){


/*eastablishConnectionWithYourTower();
checkForDND();
checkForTelecomRules();

sending SMS to numbers...numbers.*/


}

abstract public void destroyConnectionWithYourTower();

public void checkForDND(){


//check for number present in DND.
}
public void checkForTelecomRules(){
//Check for telecom rules
}
}
Solution Scenario(Contd..)
class Vodafone extends SMSSender{
public void eastablishConnectionWithYourTower() {
//connecting using Vodafone way
}
public void destroyConnectionWithYourTower() {
//destroying connection using Vodafone way
}
}
class Airtel extends SMSSender{
public void eastablishConnectionWithYourTower() {
//connecting using Airtel way
}
public void destroyConnectionWithYourTower() {
//destroying connection using Airtel way
}
}
Summary
• 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

You might also like