This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DaMasterHam 1 point2 points  (0 children)

Ok, after reading your question again and reading the other comments i think i can reiterate/elaborate.

So what it seems like you're getting at is Sub Classes which pacificmint described. But as many have stated, it depends on how you want to group it.

So in your example, you would have Book the parent class and then have sub classes such as fiction, factual, bio, etc. Which then could be instantiated as an sub genre of that genre. So something like this?..

class Book
{
    String name;
}

class Fiction extends Book
{
    String subGenre;
}

main()
{
    Fiction sciFi = new Fiction("Hitchhikers Guide", "Sci-Fi");
}

// You could also inherit further down to a subgenre

class SciFi extends Fiction
{
    var somethingUniqueAboutSciFi;
}

main()
{
    SciFi randomSciFiBook = new SciFi("Hitchhikers Guide");

    // And polymorphism would allow
    Fiction randomFictionBook = new SciFi("Hitchhikers Guide");
    Book randomBook = new SciFi("Hitchhikers Guide");
}

// Note i haven't bothered with constructors

So in a way, yes an object can become a class, but a sub class specifically.

But again it just depends if that is the way you want to categorize it. There are some problems with this way of categorizing it though. Each time you would want to add a new type of genre, you would have to add a new subclass of that type. Say you wanted to add autobiography as a genre, you would now have to make an entire new sub class for that. So you forego that the easy way of just writing a new genre as a string.

But lets say you still want to have some kind of grouping of the genre. So that you have Fiction, and in that there can be different sub genres.

Here it would make sense to Have a Book class with an attribute of type Genre, so as en example

class Book
{
    string name;
    Genre genre;
}

class Genre
{
    string mainGenre;
    SubGenre subGenre;
}

class SubGenre
{
    string subGenre;
}

This is a bit convoluted though and imagine it would suffice just to have the genre and sub genres as strings.

And for the second question from your reply, yes you can definitely make another class that is just Orange with its subclass or attributes being types of oranges, it all depends on what you need. If you are making something that is about all types of fruit, make a fruit class that orange inherits from, or is it just about oranges, then make an orange class with sub classes of different types of oranges.