Quick Summary of Creating and Using Classes in Java - dummies

Quick Summary of Creating and Using Classes in Java

By John Paul Mueller

Develop an understanding of Java class structure that helps you create better classes when you are programming Java applications. Here are the key points to remember about creating and using classes in Java:

  • Methods provide the means of telling a class or object to perform a specific task.

  • A method declaration includes the method scope, whether it’s static or non-static, the return type, method name, argument list, and method body.

  • Properties provide the means of interacting with data inside of a class or object.

  • A property declaration includes the property scope, whether it’s static or non-static, type, variable name, and initial value.

  • Using getters and setters helps protect the data managed by your class from improper use by the caller.

  • Every class you create in Java inherits from a parent class of some sort.

  • A class that you create inherits from the Object class.

  • Use the private scope to completely hide class elements.

  • Use the default scope to hide class elements from access outside the current package.

  • Use the protected scope to hide class elements from access outside of subclasses.

  • Use public scope to allow access of class elements by anyone.

  • A class that relies on events uses an event class to define that event.

  • Event listeners wait for events to happen and then react to them.

  • An implementation of an event as part of a class requires that you provide a means to register and unregister event handlers.

  • You must provide a means of firing the event.

  • Anonymous classes make it possible to express tasks by using significantly less code than would otherwise be needed when using more traditional techniques.

  • Lambda expressions provide a shortcut method for creating specialized versions of anonymous classes that have just one functional interface and one method.

Here are some key words to understand when programming Java with classes:

  • anonymous inner class: A special kind of unnamed class that contains one or more interfaces and one or more methods. You use an anonymous class when a formal class declaration is unneeded or unwanted. Because an anonymous class has no name, you can’t reference it outside the class in which it exists.

  • constructor: A special type of method used to create an object from a class. Every class comes with at least one constructor, the default constructor.

  • event: A special occurrence within a class that the class makes outsiders aware of.

  • event handler: A special method that’s designed to react to events generated by another class.

  • event listener: A special class that’s designed to listen for events that occur within another class.

  • field: A variable that’s defined as part of a class or an object.

  • interface: A class that defines unimplemented methods. Any class inheriting from an interface must provide implementations of the described methods.

  • getter: A special method used to obtain the value of a field and make it accessible outside the class or object.

  • lambda expression: An anonymous class that contains just one functional interface and one method. Normally, a lambda expression is designed to perform a task by using the shortest possible code.

  • method: The means of telling a class or object to perform a specific task.

  • override: To replace the implementation of a method in a parent class with a new implementation in a child class. Often, the parent class supplies a simple method that doesn’t address the requirements of the child class.

  • package: A container used to hold multiple classes together. The simplest package relies on a subdirectory of a directory structure. A package is always defined using the package keyword in the .java file.

  • property: The means of accessing a value stored by a class or object.

  • setter: A special method used to change the value of a field from outside of the class or object.

  • superclass: The parent class of the current class. You access the superclass by using the super keyword.