I am trying to find the real solution to using abstract classes to hide the implementation means why we need to use an abstract class to hide the implementation suppose we are working on a big project and of course, our code will be shared with other developers means suppose I have created a class and that class could be used by other developers and if someone else means developer wants to use my class I will give them a library of my class and they will import my class and they will create the object of my class that`s set here I want to know that here developer who is creating a class object is not able to see my class implementation then why we need to use an abstract class?
-
An abstract class isn't about hiding anything. I'm not sure what gave you the idea that it is. An abstract class is about providing some default functionality, while still requiring someone else to create a concrete instance of a class that inherits from your abstract class. – mason Sep 01 '22 at 13:09
-
https://stackoverflow.com/questions/3344816/when-and-why-to-use-abstract-classes-methods – epascarello Sep 01 '22 at 13:17
-
https://softwareengineering.stackexchange.com/questions/96947/why-should-i-declare-a-class-as-an-abstract-class – epascarello Sep 01 '22 at 13:18
-
I have read some answers that abstract class is used for hiding the implementation that`s why I am so confused so please let me know how abstract class is hiding the implementation. – Sudarshan Patil Sep 02 '22 at 02:33
1 Answers
Let's imagine that we have the following class of Pizza
:
public abstract class Pizza
{
public abstract decimal Cost();
}
And then imagine you want to build some web site where you can show all prices of all pizzas in your city. So users can compare costs of pizza of all pizza stores.
Well, let's create PizzaComparison
class where we can get price of different Pizza different cafes and then we need to store somewhere this pizza's cost:
public class PizzaComparison
{
public void SaveCurrentPrice(Pizza pizza)
{
decimal price = pizza.Cost();
// then you can save it to database and show all prices for users
}
}
Let's see more accurate the above code. How does it hide the implementation? Method SaveCurrentPrice
of class PizzaComparison
does not know anything about of a concrete implementation of Cost()
method of Pizza
class. It only knows some abstract details like that method should return decimal
value and this value should be a price of pizza. But where is implementation details? Implemetation details are placed and hidden in subclasses, derived classes that inherit from Pizza
class.

- 36,391
- 15
- 88
- 148
-
it`s ok but I want to know if someone wants to use my abstract class so how can I give them my abstract and concrete classes? and suppose I gave them the library of my abstract class and a concrete class, of course, they will import my abstract class, and then they will use my abstract class so here I want to know that user can see an implementation of my concrete class then how here is implementation hide? – Sudarshan Patil Sep 02 '22 at 05:42
-
@CODERBABA As a good practice it is better to have one question per topic. It helps to keep questions clean and readable. If you want to ask other questions, it is better to create a new question. Regarding my reply, If you feel that my reply is helpful, then you can upvote or mark my reply as an answer to simplify the future search of other users. [How does accepting an answer work?](https://meta.stackexchange.com/a/5235/309682) – StepUp Sep 02 '22 at 05:46