abstract classes, can you explain?
Hiya,
Im learning java and am new to the concept of abstract classes. I thought I understood it when I heard it, but I still dont understand something.
If I have a method Info() (say in class Chocolate, that returns the information about the chocolate bar, but is overridden in classes (say Yorkie and Mars which extend chocolate, for which the method Info() is slightly different), then I think i am supposed to use an abstract class, as Chocolate's Info() method has no useful body. However, how then and I supposed to use chocolate, in say Shop class, if I cannot implement it?
Chocolate contains a methods that is overridden in Mars and Yorkie (Info()), and the constructor which is used as super() in Mars and Yorkie.
I never have a Chocolate object, only Mars or Yorkie objects, but the methods are shared (polymorphism), and they are both Chocolate .
I am asking as the hand in date for this is monday, and we are supposed to use these concepts, but I am not sure if I should be using an abstract class for chocolate (or interfaces?). I need the Shop class use the Methods in Chocolate, but only the overridden version in Mars and Yorkie, and get a list of the Mars and Yorkie objects (as Chocolate objects). Should chocolate abstract, and if so how does Shop use it if i cannot implement it? How do you actually use an abstract class? Please reply ASAP!
Thanks!
Re: abstract classes, can you explain?
If you make a method abstract, you can access it in the class where it is abstract, but you need to define it in the class which extends the abstract class.
I will give you an example:
You have a class called ChocolateCookie which is abstract.
ChocolateCookie's super class is Cookie.
In the class Cookie there is a method defined without a body (it's an abstract method).
This means you can execute/access it in the super class (Cookie) but you need to define it in ChocolateCookie.
I hope this helped you.
Regards.
EDIT:
If you want to call the constructor of Cookie then you put on the first line of ChocolateCookie's constructor: super();
Re: abstract classes, can you explain?
Thanks for your help. I understand my problem with this now. :)