Java Exam 3 Review

From Knowledge Kitchen
Jump to navigation Jump to search


Prior knowledge

By nature of the material, the exam will include what was tested previously. So you would be wise to brush up:

Inheritance

We now know there are multiple types of inheritance in Java:

  • inheritance from a class
    • concrete classes
    • abstract classes
  • inheritance via interfaces
    • a class can implement more than one interface
    • interfaces can pass on both abstract and default methods

Polymorphism

The reference to an object is of a different type than the object's declared type.

Mammal barstein = new Human("Foo", "Barstein");

Polymorphic references can be of:

  • an ancestor class type
  • an interface type

Polymorphic references can not be of:

  • a child class type

In many cases, polymorophism is practically applied when it is convenient to do batch processes on a bunch of objects that share a common ancestor. In such situations, we place those various objects into some sort of collection (array, ArrayList, etc) that is typed as the ancestor class or interface that all the objects share in common.

//say you have a bunch of Mammals
Mammal[] mammals = {
    new Human("Foo", "Barstein"),
    new Dog("Fido"),
    new Mouse("Minnie")
};

//do some kind of batch process on all these Mammals
for (Mammal mammie : mammals) {
    //do some things all Mammals can do, regardless of their declared type
    mammie.tickle(); 

    //do some things only a particular declared sub-type of Mammal can do
    if (mammie instanceof Dog) {
        ((Dog) mammie).bark();
    }
}

Interfaces

Interfaces are a reference type that provide a form of inheritance, similar to classes, but with some key differences in purpose and implementation. Interfaces are often named with words ending in the suffix, "able"].

Raison d'être

Interfaces are used to impose a contractual obligation on classes that they implement a particular set of methods with specific signatures. Why might this be useful?

Limitations

Interfaces can contain only:

  • static constants
  • static methods
  • abstract methods
  • default methods

All methods and properties in an interface are public.

In what case are default methods useful?

Abstract classes

Like interfaces, abstract classes can contain abstract methods and provide a form of inheritance. In what cases are abstract classes preferable to interfaces? To concrete classes?

Raison d'être

In what case are abstract classes preferable to interfaces?

Limitations

Unlike interfaces, abstract classes can contain any sorts of properties and methods. Thus, they are in many ways similar to concrete classes. However...

Unlike concrete classes, abstract classes...

  • can contain abstract methods
  • can not be instantiated

Exceptions and errors

Errors are irremediable failures that are usually due to external factors outside the scope of your code

  • stack overflow
  • running out of memory
  • hard drive problems
  • database problems

Exceptions represent known problematic situations that can potentially be handled in code by one of two means:

  • throws declaration
  • try/catch

Exceptions are represented in code as objects of various classes that extend Exception class.

Recursion

Methods that invoke themselves. A form of iteration, but different from loops.

Classic examples

The classic pattern: handle the base case, and then recurse on the remainder. Examples:

  • Fibonacci numbers
  • Computing powers
  • Counting digits
  • Flipping strings backwards
  • Linear search
  • Fractal images

Termination

The recursion must terminate somehow in order not to trigger a stack overflow error. How does it stop recursing?