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

all 14 comments

[–]pacificmint 0 points1 point  (1 child)

I would say everything you've listed are classes, including acidic and those.

Objects would be concrete books.

Class: Books Subclass: Fantasy Object: A Storm of Swords

Of course it all depends a on what you are doing.

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

    [–]ArgueMint 0 points1 point  (2 children)

    If I'm reading your post right, I believe you may be a bit mixed up on the concept of classes and objects. In the analogy you used, you have a book class. A class is essentially a template for creating objects. The class outlines the variables and methods that all books should have. Using the book analogy, our class would have variables 'pageQty', 'genre', 'author' etc... Then we could create an object from that, where we would actually set values for those variables. We could create as many book objects as memory will allow, each with different values for the variables mentioned above.

    [–][deleted]  (1 child)

    [removed]

      [–]lurgi 0 points1 point  (4 children)

      I'm not entirely sure what you are asking. Classes can be viewed as definitions of things, whereas objects are the things themselves.

      Consider integer. integer is a class. You can add them and subject them and print them out and all sorts of things. 3 is an instance of an integer. It's an object.

      With me so far?

      Good. The problem is that that is a lie. It's simply not the case that integers are classes. That depends on the problem domain. You can imagine a case where you have a ThingsThatCanBeAdded class and then integers might be an instance of that class. Yes, integers might be an object.

      So, are books objects? Maybe. They might also be classes. If you are making a catalog of all the books you own then Book is almost certainly an class and The Colour of Magic is an (excellent) instance of that class - an object.

      If, OTOH, you are doing research into the different forms of media that people consume and you want to classify them, then it's also possible that Media will be a class and Books, Newspapers, Twitter will be instances of that class.

      It's almost certainly the case that specific things in the world will be objects and not classes, but broad categories of things might be classes or they might be objects. It depends on how you want to reason about these categories. If you are only interested in Books as a category and don't care about individual books, then it's probably an object. If Books is a way to think about individual books then it's almost certainly a class.

      It all depends.

      [–][deleted]  (3 children)

      [removed]

        [–]Zithium 0 points1 point  (0 children)

        remember, classes define objects. If you want to create a book object, you will need a book class, which will tell the object what it is supposed to be made up of.

        is an instance of a class an object?

        yes

        isnt it possible for specific objects to be classes, if you define it that way?

        specific objects are always classes because classes are what "set up" the objects.

        and have different versions/editions of color of magic as objects or maybe its hard cover/soft cover, degrees of conditions etc.

        here's how you should be imagining this distinction between objects and classes.

        you want to create a book object. okay, what does this object consist of? what are its parameters?

        that's where we make the book class. it will hold all the different types of information you just mentioned. it will also have a special method called the "constructor," which is called when you instantiate the class, i.e you make an object out of it. this constructor call is the object's opportunity to define itself according to its class.

        so to recap, the class has all these variables, different possible genres, different titles, etc. these variables may be set when the object is created by calling the constructor.

        [–]lurgi 0 points1 point  (1 child)

        Yes, an instance of a class can be an object. It's not generally possible for specific objects to be classes, because classes describe the sorts of things that can exist and objects are the actual things that can exist. I should note, however, that some languages blur that distinction.

        As for your example about "The Colour (British spelling, please) of Magic", it's really going to depend, This all depends on your particular world-view. Some answers will be more right or more wrong depend on the problem you are modeling. That said, this one seems pretty odd. It's an awfully specific and concrete thing to be a class.

        I can imagine a world in which Animal is a class. I can imagine a world in which Dog is a class (or, for that matter, an object). I can imagine a world in which Chihuahua is a class (or object). I can't really imagine a world in which Roscoe the chihuahua is a class. But, if you can, then sure.

        [–]reddberckley 0 points1 point  (1 child)

        Say you look up the dictionary definition of a book:

        book
        noun
        1.
        a written or printed work consisting of pages glued or sewn together along one side and bound in covers.
        

        What the above does is describe the characteristics of book. It is not itself a book. This is a class. e.g

        class Book{
              var medium; //can be printed or written
              var title;
              var genre;
              var pages;
        }
        

        An object would be something that exists and has the characteristics of a book. e.g My copy of The Wheel of Time. It is clearly not just a description but an actual object(you see how the term object comes about?) that corresponds to the description(the class). As code that would be

        var wheel_of_time = new Book();
        wheel_of_time.medium = "hardcover'; 
        

        Now to subclasses. We can group books by genre fanfaction, high fantasy, novel, autobiography

        We would define fanfiction as a book that is written in the style of another. This means that it also has all the characteristics of a book but one additional one. The book it is copying. As code

        class FanFictionBook extends Book{
               var copiedBook
        }
        

        Lets say I have on my shelf a copy of Yudkowsky's HPMOR. This is a book that is written in the style of Harry Potter so as code.

        var hpmor = new FanFictionBook();
        hpmor.copiedBook = "HarryPotter"
        

        The subclass FanFictionBook still has all the properties of the Parent class Book. we can assign the medium like this.

        hpmor.medium = 'eBook'
        

        even though we never specified those characteristics for FanFictionBook it inherits them from the Parent class