![]() ![]() When it comes to abstract class in simple words these are incomplete classes i.e they have methods which know what to do but they don't hnow how to get it done. ![]() You can even separate class from instance (this particular pepper is an instance, whereas on the recipe card it's a class).Concrete classes are the classes in which all the methods are declared and defined i.e in simple words each method knows what it has to do and how it how's to do it Each one is a pepper, so you can say "inherit the pepper class" (layman: take everything you know about a pepper and apply it to this specific pepper, pepper has a color attribute, a red pepper is color=red). For instance, there are green, yellow and red peppers. Now you can take this as far as you want to. And each one has methods/actions that you can perform with them (knife = cut(pepper)). Each one of these has properties (knife= handle_color: black, blade_type: serrated, etc.). Now to move into OOP: an ingredient is an object, so is a tool (bowl, knife, spoon, etc.). This is a loose example, but it works well. So when you boot a computer, you take your basic ingredients out of storage and put them on the counter (loading the OS). You are like the system bus (moving things around, etc.). Processor is the cooking apparatus (stove). I start by saying that hard drives are cabinets/storage closets. This could start the whole Abstract Class vs Interface debate, but that's a whole new page.įor everything computer related, I use a cooking dinner example. ![]() So we want to give a 10% raise to full-time employees, but nothing to part-time ones.ĭifficult to give good examples - I generally tend to use interfaces, can't really remember in the past year or so when I've used an abstract class. Every derived type of Employee (Part-Time, Full-Time, Contract, Consultant) has to implement this.Īn implementation may be: public class FullTimeEmployee : Employee However, giving a bonus may not be a straightforward affair, depending on grade etc. We can model this in the base class with the first 3 properties. Public string FirstName Įvery employee has a name and an associated job grade. So you may have: public abstract class Employee Say you wanted to implement an employee hierarchy in your code. I've been trying to thing of a good real-world example and have struggled, so can only really come up with a code example. By passing an interface around our code we're not tying ourselves down to specific implementations.Īn Abstract Class simply provides us with a means to put in place specification in a base class that derived types must implement, as well as common code that can be used by all derived types. How or what it does behind the scenes is of no concern to us. We know that anything implementing this interface has to provide an implementation for Read and Write. In computing terms, we use an interface in the same way. A device communicating with it doesn't need to know or care how the memory stick is going about its job, all it needs to know is that when we ask for data to be written, it is written conversely, when we ask to read data from it we expect to receive the data. An abstract class is generally used as a building basis for similar classes. Abstract classes and interfaces are semantically different, although their usage can overlap. It conforms to the specifications of USB. The answer by Guffa is a much better answer as it explains correctly when each should be used. It is only useful when someone takes that specification and implements it. A this time you'll need to extend the DVD Player abstract class to add your own little components.Īn interface is simply a specification. You can't simply have "a" DVD player, it has a brand and most of the time it has its own firmware. The play button will launch the content of the DVD.īut even if DVD Player provide the basic behavior of a DVD player, not everything is done. And they must correctly implement Player. Every constructor of DVD player must add some special functions to transform a simple unknown player into a DVD player. The DVD player Abstract classĪnd then from the Play Interface, I go to the VCR (or DVD) player. I usually try to avoid the "contract" word which can be misunderstood. Everybody knows what these buttons mean, even if you don't know what exactly will be done, you can use anything with this interface and you know that the little arrow will "play" and other arrows will probably send you forward or backward.Įverything that will have those buttons will provide a standard behavior that any user will know before even starting to use them. This Interface allow any user to "play" something. From this image you can teach anybody what an interface is. In my Java courses I often use this kind of image and ask: "What is this ?"Įvery time someone will say "that's a player". ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |