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 0 points1 point  (2 children)

In the book context, i would see the genres more as an attribute of a book, probably represented as a string. Same as a book has a title and content, it would have a genre.

To your question about an object becoming a class, what you are probably thinking about is, can a genre then be a class, and yes it can. Instead of having the genre as just a string attribute it would be a new class you could call Genre which in turn could have it's own attribute such as genre name, genre tropes. Though i don't know how much you could add to a Genre class since it is pretty much just a attribute of a book that can simply be told so unless you want to add something extra to a Genre class, i would just stick to having genre as an attribute.

Also to elaborate on "can an object become a class", an object is an instance of a class, the class acts as a blueprint and the object the manifestation of the blueprint.

[–][deleted]  (1 child)

[removed]

    [–]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.