Abstraction

Abstraction is the implementation of data hiding.
Abstraction and Impelemts are based on the same concepts. In Abstraction, you need to declare all abstract functions in all subclasses otherwise an error occurs but In Impelemnt it is not necessary to declare all.

Importance:

  1. Reduce Complexity Management:


Practice Program:

Q1:Create an abstract class Shape with abstract methods calculateArea() and calculatePerimeter().
 Create subclasses like Circle, Rectangle, and Triangle that implement these methods.

Q2: Design an abstract class BankAccount with abstract methods deposit() and withdraw().
 Create subclasses like SavingsAccount and CheckingAccount that implement these methods.

Q3: Create an abstract class Animal with abstract methods makeSound() and move(). 
Implement subclasses like Dog, Cat, and Bird that provide concrete implementations for these methods.

Q4: Design an abstract class Employee with abstract methods calculateSalary() and displayDetails().
 Implement subclasses like Manager, Engineer, and Salesperson with specific salary calculation logic.

Q5: Create an abstract class GeometricShape with abstract methods calculateArea() and calculatePerimeter(). 
Implement subclasses like Triangle, Rectangle, and Circle with their respective calculations.

Q6: Design an abstract class Transportation with abstract methods startEngine() and stopEngine().
 Implement subclasses like Car, Bus, and Motorcycle with specific engine behaviors.

Q7: Create an abstract class MediaPlayer with abstract methods play() and stop().
 Implement subclasses like AudioPlayer, VideoPlayer, and ImagePlayer with media-specific play and stop behaviors.

Q8: Implement a program that allows users to draw shapes (e.g., circles, squares) using abstraction.
 Create an abstract class Shape with methods for drawing and subclasses for each shape type.

Q9: Design an abstract class BankTransaction with abstract methods processTransaction() and printReceipt(). 
Implement subclasses like DepositTransaction and WithdrawTransaction.

Q10: Create an abstract class Recipe with abstract methods listIngredients() and prepareRecipe().
 Implement subclasses for different recipes, each with its list of ingredients and preparation steps.

High-Level Programs:

Q1:Write a program to calculate the ticket fare in different type of seats XYZ Airline.

1.            Make an abstract class name “Seat” and its consist of 1 abstract method intCalculate_Seat_Price(intnofseats)

2.            Make another class name “Business Class” that extend “seat” class and also implement the abstract method in “Business Class” class.

3.            Make another class name “First Class” that extend “seat” class and also implement the abstract method in “First Class” class.

4.            Make another class name “Economy Class” that extend “seat” class and also implement the abstract method in “Economy Class” class.

In main class call all method and show the price of the seat price of respective category.

Q2:Create an abstract class 'Bank' with an abstract method 'getBalance'. $100, $150 and $200 are deposited in banks A, B and C respectively. 'BankA', 'BankB' and 'BankC' are subclasses of class 'Bank', each having a method named 'getBalance'. Call this method by creating an object of each of the three classes.

Q3:We have to implement this task using the concepts of interfaces and inheritance. 

1.       Create a Geometricobject<<interface>> with :

+getPerimeter (): double

+getArea (): double

 

2.       Now create another Resizable <<interface>> with:

+resize (percent: double):  double

4.    Create a class Circle implementGeometricobject<<interface>> with:

radius: double

+Circle(radius : double)

+toString():String

+getPerimeter () : double  (use formula  V = 2Ï€r)

+getArea() : double    (use formula  V = Ï€r2)

5.     Create a class ResizableCircle inherit Circle class and implement Resizable <<interface>>   with following implementation:

+ResizableCircle (radius : double)

+tostring (): String

+resize (percent:double):double      it will increase the radius how many percent given in parameter by using  (radius *= percent/100.0) and return radius.

 

Now in main class create objects of classes who implements interfaces. Call their functions and observe its working.