One of Java's strengths is its excellent support for object-oriented programming.
This support is provided through classes and interfaces, which are the building blocks of all Java programs.
The ability to effectively use classes and interfaces is an essential Java programming skill.
The certification exam recognizes the importance of class and interface fundamentals and contains a number of questions that test your knowledge of them.
This module reviews these topics and identifies any important points that you should know in preparation for the exam.
This module will review your knowledge of Java classes and interfaces, provide examples of their use, and help you to satisfy the following exam objectives:
- Declare classes and inner classes, making appropriate use of all permitted modifiers (such as
abstract, and so on).
- Identify correctly constructed class declarations (of all forms, including inner classes), interface declarations, and implementations.
- State the benefits of encapsulation in object-oriented design, and write code that implements tightly encapsulated classes and the relationships "is a" and "has a."
- Write code to construct instances of any concrete class, including normal top-level classes, inner classes,
static inner classes, and anonymous inner classes.
- For a given class, determine if a default constructor will be created, and if so, state the prototype of that constructor.
Classes and interfaces are building blocks of an application. Efficient and effective class design makes a significant impact on the overall application design.
Imagine if, while designing your classes, you did not consider effective packaging, correct overloaded or overridden methods, or access protection you might lose on extensibility, flexibility, and usability of your classes.
For example, if you did not override methods hashCode() and equals() correctly in your classes, your seemingly
objects might not be considered equal by collection classes like HashSet or HashMap.
Imagine if you did not use the right access modifiers to protect your classes and their members, they could be subject to unwanted manipulation by other classes from the same or different packages. The creation of overloaded methods is another
domain, which is an important class design decision. It eases instance creation and use of methods.
Class design decisions require an insight into understanding correct and appropriate implementation practices. When armed with adequate information you willbe able
to select the best practices and approach to designing your classes.
The topics covered in this chapter will help you design better classes by taking you through multiple examples. This module covers
- Access modifiers
- Method overloading
- Method overriding
- Virtual method invocation
- Use of the instanceof operator and casting
- Override methods from class Object to improve the functionality of your class
- How to create packages and use classes from other packages