In this java tutorial under the abstraction we are taught to hide the implementation details and make our code less coupled. But on this video we are not taught about abstract classes. Isn’t abstract classes not related to the abstraction? why they are in a separate video. Also Can someone explains how abstract classes hide the implementation details ?
The main point of abstract classes is that they cannot be instantiated; you will not invoke an abstract class with any technique. You cannot use an object or instance member, or class member with a (.) operator to invoke it. This way, the class hides the implementation. However, if the methods are also abstract, then the concrete class must implement them. If used like this, they work like interfaces. Interfaces are by nature abstract and their methods are abstract unless you explicitly make them otherwise, and they are used to contain/list “ideas” (in the form of methods) that need to be implemented by other classes. This is useful when you need a template for methods when many other classes need to implement the same methods. That’s why interfaces are said to contain methods of a particular functionality or role (Hamedani, 2023).
Hamedani, M. (20230. Ultimate Java Part 2: Object-Oriented Programming. Interfaces: What are interfaces?
Regarding interfaces, as @King_Sardius said they contain methods of a particular role. One analogy would be to consider the handle of a door for a car. We know the role of a door handle for a car, but each car handle can look and feel different. The underlying functionality of the handle stays the same (it opens the car door). But the way you may open the door could be different, this refers to the implementation. So the concept or idea of a car door handle is in essence an interface, it would have an Open() method. But each concrete class (which could represent a car) would have its own different implementation of the Open() method from the interface (you could pull the handle, swipe it with a key fob, press a button, etc).
So the idea remains the same, its a handle that opens a car. More specifically, the handles functionality is the interface (the action of how you would operate the handle, with the Open() method). The concrete class that implements the Open() method/action from the interface, is the implementation (how the action of opening the car actually works).
To further this, the concept of abstraction using interfaces or abstract classes is heightened as you can now (using the example above) have multiple concrete classes that each have their own implementation of how a car door handle opens (just by using the interface). This means that if you want to change one concrete class you only need to go to one place. If you want to deep dive into why abstraction is so important in OOP I’d recommend to read the gang of four book. Also you should check out the refactor guru who discusses design patterns that exploit the idea of abstraction to write code that solves generic and common problems in OOP.
Great explanation sir. Could you please confirm that the book is called “the gang of four”? if not, what is the proper name of the book or books? I love to read anything that elucidates Java. Look at my Java books below Not that Mosh isn’t enough, I just like different perspectives on the same matter. In the end, Mosh is the by far the most practical and the one I enjoy the most. Mosh is exactly what I need.
The gang of four book is an incredibly popular book that covers software design patterns for object-oriented programming. The book is called “Design Patterns: Elements of Reusable Object-Oriented Software”, its written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides.
Alongside this book, if you are interested in seeing (in your programming language) how these design patterns look you can check out the refactoring guru here. The refactoring guru has some fantastic resources on design patterns, with diagrams, code examples, and great explanations.
Nice collection of books though, its great to see fellow programmers with an interest in progressing themselves. Keep up the great work!
Thank you, kid sir! I wish you the same. Book added to my resource stack.