Results 1 to 3 of 3
  1. #1
    Aname is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Exclamation Multiple Subclasses

    Hi there i was wondering if its possible to do a superclass with multiple subclasses or something like that.

    for example:
    i would like to create a book class and a CD class
    the stuff that both classes have in common are

    "double price", "String author" , "String store" and so on

    however the CD class has " double trackrange"
    and class the book has " int pages"

    is it possible to use a main in order to call all subclasses?

    can anyone please use these examples and give me an answer?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Multiple Subclasses

    You could make a super class something like Item, which has the common attributes of these to different classes. Then you can subclass this for Book and CD and you can absolutely handle them easily. Look into the instanceof keyword if you are storing them as an array/list of Items.


    Java Code:
    class Item{
     //shared instance variables
    
     //getters and setters
    }
    
    class Book extends Item{
     //Book specific stuff
    }
    
    class CD extends Item{
      //CD specific stuff
    }
    http://docs.oracle.com/javase/tutori...ubclasses.html
    Last edited by sunde887; 08-11-2012 at 04:01 AM.

  3. #3
    Aname is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: Multiple Subclasses

    thank you, it helps alot now i can start trying to understand how inheritance works :)

Similar Threads

  1. Subclasses
    By WillatSmu in forum New To Java
    Replies: 2
    Last Post: 05-02-2012, 07:01 AM
  2. need help with subclasses
    By thorobred in forum New To Java
    Replies: 6
    Last Post: 02-19-2011, 05:34 AM
  3. Dynamic Subclasses?
    By MrFish in forum Advanced Java
    Replies: 5
    Last Post: 11-07-2010, 01:28 AM
  4. Lost with subclasses
    By vinyacam in forum New To Java
    Replies: 3
    Last Post: 05-09-2010, 07:12 PM
  5. Generics and subclasses
    By Sven in forum Advanced Java
    Replies: 1
    Last Post: 12-22-2008, 06:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •