Java Final
Regular Expression Common Matching Symbols
. - Matches any character ^regex - Finds regex that must match at the beginning of the line. regex$ - Finds regex that must match at the end of the line. [abc] - Set definition, can match the letter a or b or c. [abc][vz] - Set definition, can match a or b or c followed by either v or z. [^abc] - When a caret appears as the first character inside square brackets, it negates the pattern. This pattern matches any character except a or b or c. [a-d1-7] - Ranges: matches a letter between a and d and figures from 1 to 7, but not d1. X|Z - Finds X or Z. XZ -Finds X directly followed by Z. $ - Checks if a line end follows.
Over-ride the following abstract method: public abstract double earnings();
//In the concrete sublcass @Override public double earnings() {return getWeeklySalary;}
Override Syntax
@Override public String toString() { }
A class that contains abstract methods must be an _______________ class even if that class contains some concrete methods.
A class that contains abstract methods must be an abstract class even if that class contains some concrete methods.
A class that does not implement all the methods of the interface is an _________________ class and must be declared ______________.
A class that does not implement all the methods of the interface is an abstract class and must be declared abstract.
A _____________ method is a public static method that creates and initializes an object of a specified type (possibly of the same class), then returns a reference to it.
A factory method is a public static method that creates and initializes an object of a specified type (possibly of the same class), then returns a reference to it.
A __________ class cannot be extended to create a subclass
A final class cannot be extended to create a subclass.
A ________ method in a superclass cannot be overridden in a subclass.
A final method in a superclass cannot be overridden in a subclass.
Subclass Characteristics
A subclass can be a superclass of future subclasses. A subclass can add its own fields and methods. A subclass is more specific than its superclass and represents a more specialized group of objects. The subclass exhibits the behaviors of its superclass and can add behaviors that are specific to the subclass. ◦ This is why inheritance is sometimes referred to as specialization.
Inheritance issues
A subclass can inherit methods that it does not need or should not have. ◦ Even when a superclass method is appropriate for a subclass, that subclass often needs a customized version of the method. ◦ The subclass can override (redefine) the superclass method with an appropriate implementation.
A superclass's private members are ___________ from its subclasses.
A superclass's private members are hidden from its subclasses
A superclass's protected members can be accessed by members of that _________________, by members of its ____________________ and by members of other classes in the same ___________________.
A superclass's protected members can be accessed by members of that superclass, by members of its subclasses and by members of other classes in the same package.
It's acceptable to call a ___________ method from a constructor.
It's acceptable to call a static method from a constructor.
Java views each file as a __________________ stream of ___________.
Java views each file as a sequential stream of bytes.
Inheritance
◦ A new class is created by acquiring an existing class's members and possibly embellishing them with new or modified capabilities.
Abstract methods do ___ provide implementations.
Abstract methods do not provide implementations.
All calls to methods are resolved at ___________________ time, based on the ____ of the object to which the array refers. ◦ Known as ____________ _______________ or _____________ _____________.
All calls to methods are resolved at execution time, based on the type of the object to which the array refers. Known as dynamic binding or late binding.
All interface members must be ____________.
All interface members must be public.
All public and protected superclass members retain their original _____________ ______________ when they become members of the subclass.
All public and protected superclass members retain their original access modifier when they become members of the subclass.
Abstract classes
An abstract class provides a superclass from which other classes can inherit and thus share a common design. Used only as superclasses in inheritance hierarchies, so they are sometimes called abstract superclasses. Cannot be used to instantiate objects—abstract classes are incomplete. Subclasses must declare the "missing pieces" to become "concrete" classes, from which you can instantiate objects; otherwise, these subclasses, too, will be abstract.
An ________________ _________________is any class above the direct superclass in the class hierarchy.
An indirect superclass is any class above the direct superclass in the class hierarchy.
An interface declaration begins with the keyword ____________ and contains only _______________ and ______________ methods
An interface declaration begins with the keyword interface and contains only constants and abstract methods public interface Example { }
At execution time, the type of the object to which the variable refers determines the actual method to use. ◦ This process is called _______________ ______________.
At execution time, the type of the object to which the variable refers determines the actual method to use. ◦ This process is called dynamic binding.
Classes that can be used to instantiate objects are called ____________ _______________.
Classes that can be used to instantiate objects are called concrete classes. Concrete classes provide the specifics that make it reasonable to instantiate objects.
Composition-Based Designs
Composition is loosely coupled. When you compose a reference as an instance variable of a class, it's part of the class's implementation details that are hidden from the class's client code If the reference's class type changes, you may need to make changes to the composing class's internal details, but those changes do not affect the client code.
Computers store files on ______________ _____________ devices, including hard disks, flash drives, DVDs and more. Data maintained in files is ___________________ data.
Computers store files on secondary storage devices, including hard disks, flash drives, DVDs and more. Datamaintained in files is persistent data.
________________ classes provide implementations of every method they declare (some of the implementations can be inherited).
Concrete classes provide implementations of every method they declare (some of the implementations can be inherited).
Constructors and static methods ___________ be declared ________________.
Constructors and static methods cannot be declared abstract.
Constructors are ___ inherited.
Constructors are not inherited.
Do not call _____________________ methods from constructors.
Do not call overridable methods from constructors.
Each concrete subclass of an abstract superclass also must provide concrete implementations of each of the superclass's abstract methods.
Each concrete subclass of an abstract superclass also must provide ____________ implementations of each of the superclass's ____________ methods.
Polymorphism
Enables you to "program in the general" rather than "program in the specific." ◦ Polymorphism enables you to write programs that process objects that share the same superclass as if they were all objects of the superclass; this can simplify programming.
Every class in Java directly or indirectly extends __________.
Every class in Java directly or indirectly extends Object.
Every object knows its own _____ and can access this information through the ________ method, which all classes inherit from class Object.
Every object knows its own class and can access this information through the getClass method, which all classes inherit from class Object.
Has-a represents ________________.
Has-a represents composition. In a has-a relationship, an object contains as members references to other objects
If the code does not include an explicit call to the superclass constructor, Java implicitly calls the superclass's _____________ constructor.
If the code does not include an explicit call to the superclass constructor, Java implicitly calls the superclass's default or no argument constructor.
In Java SE 8, interfaces also may contain public _______________ methods with concrete default implementations that specify how operations are performed when an implementing class does not ________________ the methods.
In Java SE 8, interfaces also may contain public default methods with concrete default implementations that specify how operations are performed when an implementing class does not override the methods.
In general, use inheritance only for true ________ relationships in which you can assign a subclass object to a superclass reference.
In general, use inheritance only for true is-a relationships in which you can assign a subclass object to a superclass reference.
Inheritance-Based Designs
Inheritance creates tight coupling among the classes in a hierarchy. Each subclass typically depends on its direct or indirect superclasses' implementations. Changes in superclass implementation can affect the behavior of subclasses, often in subtle ways. Tightly coupled designs are more difficult to modify than those in loosely coupled, composition-based designs. Change is the rule rather than the exception—this encourages composition.
Inheritance is done at ___________ time. Composition is more ___________—it, too, can be done at ___________ time, but it also can be done at ______________ time because non-final references to composed objects can be modified. ▪ We call this __________________ composition
Inheritance is done at compile time. Composition is more flexible—it, too, can be done at compile time, but it also can be done at execution time because non-final references to composed objects can be modified. ▪ We call this dynamic composition
_______________ offer a capability requiring that unrelated classes implement a set of common methods.
Interfaces offer a capability requiring that unrelated classes implement a set of common methods.
Is-a represents _________________.
Is-a represents inheritance In an is-a relationship, an object of a subclass can also be treated as an object of its superclass.
Java does not allow subclasses to inherit from more than one ______________, but it allows a class to inherit from one superclass and implement as many _____________ as it needs.
Java does not allow subclasses to inherit from more than one superclass, but it allows a class to inherit from one superclass and implement as many interfaces as it needs.
Java supports only single inheritance, in which each class is derived from exactly ___________________ superclass.
Java supports only single inheritance, in which each class is derived from exactly one direct superclass.
Objects of ___ classes that _______________ a common superclass can be treated as _______ of that superclass.
Objects of all classes that extend a common superclass can be treated as objects of that superclass.
Try Catch Syntax
On method called, add throws ErrorType public static int example(int a, int b) throws Error1 { return a / b; } try { } catch (ErrorType ErrorName) { printf(ErrorName); } finally { always executes }
Once a class implements an interface, all objects of that class have an _______ relationship with the interface type, and all objects of the class are guaranteed to provide the __________________ described by the interface.
Once a class implements an interface, all objects of that class have an is-a relationship with the interface type, and all objects of the class are guaranteed to provide the functionality described by the interface.
The Java compiler does allow the assignment of a superclass reference to a subclass variable if you explicitly cast the superclass reference to the subclass type. This is called ___________________.
The Java compiler does allow the assignment of a superclass reference to a subclass variable if you explicitly cast the superclass reference to the subclass type. ◦ A technique known as downcasting that enables a program to invoke subclass methods that are not in the superclass.
The _________ ______________ is the superclass from which the subclass explicitly inherits.
The direct superclass is the superclass from which the subclass explicitly inherits.
The first task of a subclass constructor is to call its direct superclass's ______________ explicitly or implicitly
The first task of a subclass constructor is to call its direct superclass's constructor explicitly or implicitly.
The last constructor called in the chain is always _____________ constructor. Original ____________ constructor's body finishes executing last.
The last constructor called in the chain is always Object's constructor.Original subclass constructor's body finishes executing last.
To enable a subclass to directly access superclass instance variables, we can declare those members as __________ in the superclass.
To enable a subclass to directly access superclass instance variables, we can declare those members as protected in the superclass. protected final String firstName;
To use an interface, a concrete class must specify that it ________________ the interface and must declare _______ method in the interface with specified signature.
To use an interface, a concrete class must specify that it implements the interface and must declare each method in the interface with specified signature.
When creating a class, rather than declaring completely new members, you can designate that the new class should ______________ the members of an existing class. ◦ Existing class is the ______________. ◦ New class is the _)_______________.
When creating a class, rather than declaring completely new members, you can designate that the new class should inherit the members of an existing class. ◦ Existing class is the superclass ◦ New class is the subclass
When the compiler encounters a method call made through a ____________, the compiler determines if the method can be called by checking the ____________ ___________ ___________.
When the compiler encounters a method call made through a variable, the compiler determines if the method can be called by checking the variable's class type. ◦ If that class contains the proper method declaration (or inherits one), the call is compiled.
When you use a composition approach instead of inheritance, you'll typically create a _________ number of ___________ classes, each focused on one responsibility.
When you use a composition approach instead of inheritance, you'll typically create a larger number of smaller classes, each focused on one responsibility.
With polymorphism, we can design and implement systems that are easily _____________________
With polymorphism, we can design and implement systems that are easily extensible. ◦ New classes can be added with little or no modification to the general portions of the program, as long as the new classes are part of the inheritance hierarchy that the program processes generically. ◦ The new classes simply "plug right in." ◦ The only parts of a program that must be altered to accommodate new classes are those that require direct knowledge of the new classes that we add to the hierarchy.
Regular Expressions Meta Characters
\d - Any digit, short for [0-9] \D - A non-digit, short for [^0-9] \s - A whitespace character, short for [ \t\n\x0b\r\f] \S - A non-whitespace character, short for \w - A word character, short for [a-zA-Z_0-9] \W - A non-word character [^\w]
Useful String Methods
boolean matches(String regex) Tells whether or not this string matches the given regular expression. String[] split(String regex) Splits this string around matches of the given regular expression. char charAt(int index) Returns the character at the specified index. char[] toCharArray() Converts this string to a new character array. boolean equals() Compares string to another object
Abstract method syntax
public abstract void draw();
To implement more than one, use a comma-separated list of interface names after keyword implements in the class declaration, as in:
public class ClassName extends SuperclassName implements FirstInterface, SecondInterface() { }
Extends Syntax
public class subExample extends superExample { }
Superclass constructor call syntax
public exampleClass (String x, String y, String z) { super(x, y); //Calls superclass constructor this.otherAttributes = z; }