Java Refresher - shoikot/shoikot.github.io GitHub Wiki

Java Refresher

A Java program is mostly a collection of objects talking to other objects by invoking each other's methods. Every object is of a certain type, and that type is defined by a class or an interface. Most Java programs use a collection of objects of many different types. Following is a list of a few useful terms for this object-oriented (OO) language:

■ Class: A template that describes the kinds of state and behavior that objects of its type support.

■ Object: At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object that is an instance of that class. That object will have its own state and access to all of the behaviors defined by its class.

■ State: (instance variables) Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object's instance variables make up the object's state.

■ Behavior (methods): When a programmer creates a class, she creates methods for that class. Methods are where the class's logic is stored and where the real work gets done. They are where algorithms get executed and data gets manipulated.

Identifiers and Keywords

All the Java components we just talked about—classes, variables, and methods need names. In Java, these names are called identifiers, and, as you might expect, there are rules for what constitutes a legal Java identifier. Beyond what's legal, though, Java (and Oracle) programmers have created conventions for naming methods, variables, and classes.

Like all programming languages, Java has a set of built-in keywords. These keywords must not be used as identifiers. Later in this chapter we'll review the details of these naming rules, conventions, and the Java keywords

Inheritance

Central to Java and other OO languages is the concept of inheritance, which allows code defined in one class to be reused in other classes. In Java, you can define a general (more abstract) superclass, and then extend it with more specific subclasses. The superclass knows nothing of the classes that inherit from it, but all of the subclasses that inherit from the superclass must explicitly declare the inheritance relationship. A subclass that inherits from a superclass is automatically given accessible instance variables and methods defined by the superclass, but the subclass is also free to override superclass methods to define more specific behavior. For example, a Car superclass could define general methods common to all automobiles, but a Ferrari subclass could override the accelerate() method that was already defined in the Car class.

Interfaces

A powerful companion to inheritance is the use of interfaces. Interfaces are like a 100-percent abstract superclass that defines the methods a subclass must support, but not how they must be supported. In other words, for example, an Animal interface might declare that all Animal implementation classes have an eat() method, but the Animal interface doesn't supply any logic for the eat() method. That means it's up to the classes that implement the Animal interface to define the actual code for how that particular Animal type behaves when its eat() method is invoked.

Finding Other Classes

As we'll see later in the book (for you OCP candidates), it's a good idea to make your classes cohesive. That means that every class should have a focused set of responsibilities. For instance, if you were creating a zoo simulation program, you'd want to represent aardvarks with one class and zoo visitors with a different class. In addition, you might have a Zookeeper class and a PopcornVendor class. The point is that you don't want a class that has both Aardvark and PopcornVendor behaviors (more on that in Chapter 10). Even a simple Java program uses objects from many different classes: some that you created, and some built by others (such as Oracle's Java API classes). Java organizes classes into packages and uses import statements to give programmers a consistent way to manage naming of, and access to, classes they need. The exam covers a lot of concepts related to packages and class access; we'll explore the details throughout the book.