Search code examples
javascriptabstraction

What are some good examples of Abstractions in JavaScript for AP Comp. Sci. Principles?


I am currently working on my performance task for the AP CSP course. I have all the points down besides Abstraction. Can someone please give me a good example. It would technically need to be a "student-made" abstraction. I am working on a project that bases on a "job finder" for the medical field. Thanks


Solution

  • I am assuming that by "Abstraction" you mean the declaring of a class to be Abstract, such that you cannot make objects of that class without the help of a subclass. An example would be a program where you need to keep track of animals in a zoo, and group them by their environment type. Thus you might have:

    public abstract Class Animal { }

    public abstract Class AquaticAnimal extends Animal { }

    public abstract Class LandAnimal extends Animal { }

    public abstract Class AvianAnimal extends Animal { }

    and then you could have all kinds of non-abstract, declarable classes that funnel into one or another category, like:

    public Class Gopher extends LandAnimal { }

    could all of this be done with some identifying primitive data that is a parameter of the object? absolutely. The benefits of doing this instead are:

    -While you cannot make objects of an abstract class, you can make arrays of them, where all the objects are subclasses, You can have a LandAnimal[] that contains gazelles, lions, and snakes.

    -implementation of methods is made easier. I could declare a method in my Animal class called orderFood() so that each animal has a defined way to order more food. I can also declare something like checkFeathers() in the AvianAnimals class so each AvianAnimal has to have a method for how their feathers can be checked for damage. this way i dont have to make an interface for each group i want if they are already defined.

    -probably more, but that's it off the top of my head, maybe other people will have more.

    (edit: example code is all in java)