Lesson 4 | More on classes |
Objective | Important points about classes, subclasses, and superclasses. |
Designing Classes in Java
Java classes serve as the foundation for object-oriented programming in the Java language. As of Java 11, here are some of the most crucial characteristics of Java classes that every developer should be intimately familiar with:
- Encapsulation: Java classes encapsulate both state (attributes) and behavior (methods). Proper encapsulation ensures that the internal state of the object is shielded from external manipulation, thereby promoting data integrity.
- Inheritance: Classes in Java support the concept of inheritance. This allows for the creation of a new class based on an existing class, thereby promoting code reusability. The `extends` keyword is used to denote inheritance.
- Modifiers: Java classes can have access modifiers:
- public,
- private, or
- protected.
These determine the visibility and accessibility of a class and its members. Additionally, the `final` modifier ensures a class cannot be subclassed, and `abstract` means a class cannot be instantiated directly.
- Constructors: These are special methods used to initialize new objects. They share the same name as the class and do not have a return type.
- Methods: Functions defined within a class. They provide the behavior or operations that can be performed on an object of the class.
- Instance vs. Static Members: Class members (both fields and methods) can either be instance members or static members. While instance members belong individually to each object of the class, static members belong to the class itself and are shared among all instances.
- Inner Classes: Java supports nesting one class inside another. Such nested classes can be categorized as static nested classes, inner classes, local inner classes, and anonymous inner classes.
- Interfaces: While not classes in the strictest sense, interfaces define a contract (a set of abstract methods) that implementing classes must adhere to. Java 11 introduced enhancements with respect to interfaces, allowing them to have private methods.
- Garbage Collection: Java classes are subject to garbage collection, which automatically reclaims memory once objects are no longer reachable. Developers need not manually manage memory.
- Annotations: Java classes, methods, fields, and other elements can be annotated to provide metadata. This can influence how programs or libraries interact with those elements.
- Reflection: Java classes have the ability to be introspected at runtime using the Reflection API. This allows for the dynamic inspection of classes, interfaces, fields, and methods.
- JEP 321: HTTP Client (Standard): While not directly a characteristic of Java classes, Java 11 introduced a new standard HTTP client which replaces the `HttpURLConnection` class, and developers should be aware of this when working with HTTP operations in Java 11.
- JEP 330: Launch Single-File Source-Code Programs: Again, while not a direct attribute of classes, Java 11 allows developers to execute a Java class contained in a single source file without the need to compile it explicitly.
In conclusion, a deep understanding of these characteristics will equip a Java developer to design robust and efficient software systems using Java 11. Embracing the core tenets of object-oriented design, coupled with the unique features and enhancements of the Java platform, can lead to elegant and maintainable software solutions.
A few questions on the certification exam will test your ability to define classes. These questions will describe relationships using "is a" and "has a." The "is a" specifies a subclass relationship while "has a" identifies a field variable. For example, consider the description, "An employee is a person that has an integer employee number, and an hourly rate." The following class defines these relationships:
public class Employee extends Person {
int employeeNumber;
double hourlyRate;
}
Subclasses and Superclasses
The following is a collection of definitions and rules concerning subclasses and superclasses:
- A class
X
that extends another class Y
is said to be a subclass[1] of Y
. Y
is said to be a superclass[2] of X
.
- All classes are considered to be subclasses of themselves.
- If a class
X
is a subclass of Y
and Y
is a subclass of Z,
then X
is also a subclass of Z
and Z
is a superclass of X
.
- A strict subclass[3] of a class
X
is any subclass of X
except X
.
- The direct superclass[4] of a class
X
is the class which it extends.
- The
Object
class of the java.lang
package is the highest class in the Java class hierarchy. All classes are subclasses of Object
.
[1]
Subclass: A class that extends another class. All classes are a subclass of themselves.
[2]
Superclass: A class that is extended by another class.
[3]
Strict subclass: A subclass that is not the class itself.
[4]
Direct superclass: The class that is identified in a class's extends clause. If a class is declared without an extends clause, then its direct superclass is java.lang.Object.