2

Nearly in all of the "Abstraction" principle definitions, it says sth. like "Hiding the irrelevant (or extra, not needed to know) codes from the user". And I could not get how this is about "abstraction".

Let's say "This is because we can define the methods in the base abstract classes, and we can use these methods after we created the objects in child classes without knowing the internal functionality" But we could already have base non-abstract classes for this purpose. We could have already created objects and used the functionality of the base class without knowing the internal structure of those methods. How is this feature belongs to "abstraction" ????

The main advantage of creating an abstract base class over a non-abstract base class is, that it can not be instantiated; so it is just be there for creating a "common concept", right? But other than that, i don't see a difference between them in the sense of hiding the internal structure.

Let's say I have 2 types of coffee-machine; CheapMachine and ExpensiveMachine. I created a non-abstract base class and added several methods with definitions like StartMachine(), GetOptimumHeat(), etc. And then I created a CheapMachine instance CP, and called these methods easily, without knowing the internal functionality of these methods.

So, did I use abstraction principle by just creating a base class then?

I am confused like I feel I got the main idea but what would be the best description of this principle? For ex., How would you describe this principle in an interview?

noob
  • 21
  • 1
  • 2
    Possibly have a look at the answers here: https://stackoverflow.com/questions/6802573/interfaces-whats-the-point – Paddy Jan 28 '22 at 13:45
  • 2
    TLDR You are not allowed to make instances of an abstract class, so, whenever you are working with its members - you will always be dealing with some other, concrete class. From technical point of view, abstract classes allow you to have methods with defined signature, but no implementation (subclasses will be forced to provide implementation for each such method). Normal classes typically won't allow you to skip implementation. You can throw an exception inside, or leave it logically empty by returning some form of null, but from technical point of view this method has an implementation. – Ivan Koshelev Jan 28 '22 at 13:54

0 Answers0