Extending Airline Services using Inheritance & Polymorphism
Business Scenario
Previously, we learned that Encapsulation protects data through controlled access.
Welcome back,Developers!
Pre-Lab Preparation
OOP concepts - Instance variables, Instance methods, Constructors
Basic understanding of inheritance and types of inheritance
Concept:
In the real world scenario, inheritance is typically implemented for reusability. Child classes are created using the base/ parent class. In the SkyReserve system, we can declare the child classes as ‘DomesticFlight’ and ‘InternationalFlight’.
Task 1: Implement fare method overloading in the Flight class.
1
create a new package “com.itvedant.flightmodeloop.polymorphism”
2
create new class ‘Flight’
3
declare another instance method with the name ‘calculateFare()’ that will calculate the fare based on discount percent
4
declare the main method to create the object and call the methods
5
run the code to check the output
Task 2: Apply inheritance to create child classes and objects.
1
declare the child classes as shown below
2
create the child class objects, and check the output
3
run the code to check the output
Note that, when the object of a child class is created, the parent class constructor is called first and then the child class constructor is created.
4
create the object of InternationalFlight and check the output
Task 3: Apply method overriding in child classes.
1
redeclare the calculateFare() in child class
2
create the object and run the code
Great job!
We have implemented the polymorphism - method overloading concepts within the Flight class
We have applied overloading concepts in constructors in Flight class
We have implemented inheritance, so as to incorporate reusability
We have applied overriding concept in child classes
Checkpoint