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:
- Reduce Complexity Management:
Practice Program:
Q1:Create an abstract class Shape with abstract methods calculateArea() and calculatePerimeter().Q2: Design an abstract class BankAccount with abstract methods deposit() and withdraw().
Q3: Create an abstract class Animal with abstract methods makeSound() and move().
Q5: Create an abstract class GeometricShape with abstract methods calculateArea() and calculatePerimeter().
Q6: Design an abstract class Transportation with abstract methods startEngine() and stopEngine().
Q7: Create an abstract class MediaPlayer with abstract methods play() and stop().
Q9: Design an abstract class BankTransaction with abstract methods processTransaction() and printReceipt().
Q10: Create an abstract class Recipe with abstract methods listIngredients() and prepareRecipe().
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.