Sunday, December 31, 2017

Java: Abstract Classes and Interfaces

Building on what I learned previously, Java also makes use of abstract classes. These special objects can't be used on their own, but are more of a building block that other, more complex objects are built upon. "An abstract class has no life of its own. In order to use an abstract class, you have to create an ordinary (non-abstract) class that extends the abstract class" (Burd 419).

From the last example, I made an abstract class called Command. This object can't be made on it's own, as it has no actual function, but is what makes sure that all future commands based on this will follow the same hierarchy.

These abstract classes are important because without the class being abstract, someone, including yourself, might accidentally try to create an instance of the abstract class, which shouldn't happen. Adding abstract ensures that the code won't be misused accidentally. By adding abstract methods, or functions, this means that all classes that are based on it must create their own code for those methods. The benefit to using abstract classes to be extended over other similar types of classes such as interfaces is that a child can only inherit from one abstract class, but can inherit from many interfaces. This means you can limit the inheritance of a child, preventing a mess of many different inherited methods and variables.

However, with interfaces, a child can inherit from many different interfaces at the same time. This means that you can have an interface for commands that are for staff and commands that are only allowed to be used in certain groups. As an example, "[Barry is] an instance of  a Person class with all the fields that any person has - name, address, age, height, weight, and so on. He can also implement more than one interface: Because Barry implements a Professor interface, he must have methods named teachStudents, adviseStudents, and gradePapers. Because he implements an Author interface, he must have methods named writeChapters, reviewChapters, answerEmail, and so on" (Burd 411).

Here I made a command that is used to change the prefix that is used before a command in chat so that the program can tell the difference between a chat message and a command. The command extends a command, so it inherits the structure of a command, and also implements StaffCommand and RestrictedCommand. The StaffCommand inheritance requires the permissionLevel function which gives a number used to represent a hierarchical system where the lower the number, the less permissions the user has. Therefore, this command requires a user with permissions of 4 or greater. The RestrictedCommand inheritance requires the canUseCommand function. This takes the chat channel that the message was sent in and if the channel's ID matches the one provided, the command can be used. Otherwise, the command will not be run.




Do you think that having multiple systems of inheritance is more confusing, or is it nice to have a greater variety of tools to work with in order to keep everything organized and categorized?

Works Cited:
Burd, Barry. Java for Dummies. 7th ed., John Wiley & Sons, 2017.

No comments:

Post a Comment