Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: abstract classes, can you explain?

  1. #1
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 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!


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    My Mood
    Cheerful
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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();
    Last edited by Verficon; March 5th, 2011 at 06:40 PM.

  3. The Following User Says Thank You to Verficon For This Useful Post:

    Scotty (March 5th, 2011)

  4. #3
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: abstract classes, can you explain?

    Thanks for your help. I understand my problem with this now.

Similar Threads

  1. Issue with abstract classes
    By zaphod2003 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 7th, 2011, 02:25 AM
  2. noob question on abstract classes
    By joshred in forum Object Oriented Programming
    Replies: 1
    Last Post: September 15th, 2010, 06:27 PM
  3. Java: interfaces and abstract classes
    By pinansonoyon in forum Object Oriented Programming
    Replies: 1
    Last Post: May 6th, 2010, 10:17 AM
  4. [SOLVED] Abstract Classes Help
    By SweetyStacey in forum Object Oriented Programming
    Replies: 10
    Last Post: May 6th, 2010, 06:15 AM
  5. Java program with abstract class along with two subclasses
    By crazydeo in forum Collections and Generics
    Replies: 2
    Last Post: June 10th, 2008, 11:45 AM