What is top down development?
A problem solving approach where you break a task down into smaller subtasks
Procedural abstraction
Using methods to define tasks and hide complexity.
2 parts of a method?
Declaration and body
What are the 4 parts of the method declaration?
Access level, return type, name, and parameters
What is the difference between access level and access modifier?
Access level is what determines the accessibility of a method, whereas an access modifier is the specific keyword used to determine that
What is another word for access level?
Visibility
What is a class method?
A method declared with the access level “static”, which means it can only be called from the class itself and cannot be called by objects of that class.
What are parameters?
Something that can accept values to pass into the method. When a method is called, parameters allow that statement to pass information into that method for it to manipulate.
Can methods change the data stored in an argument?
No, they cannot change what was passed into them. Parameters pass a copy of the value to the method for it to manipulate and return something new, but methods cannot change the actual value of the parameter.
What happens when you pass an object into a method?
It gives that method a copy of the reference to the object.
How do you separate multiple parameters?
With commas
What is method overloading?
When multiple methods have the same name. The computer differentiates between them by the number and type of their parameters.
What does the return statement do?
Return a value from the method back to the calling statement
What does the keyword void do?
It means that the method doesn’t have to return anything
What are the three parts in a method documentation?
The function, precondition and the post condition
What is the function in method documentation?
It gives a basic overview of what the method does
What is the precondition?
The basic requirements for a method to run. Does not exist for all methods
What is the postcondition?
Describes what is true after the method has been executed
What is a call?
A statement that contains a method name followed by parenthesis. Make the code jump to that method and execute whatever code is inside.
What does local mean?
It means that something is restricted to the piece of code where it was defined. In the context of methods, it means that any variable, object or constant defined in a method can only be access by that method and nothing else
What are the two key parts of an object?
Its state and its behavior
What is the state of an object?
The data it stores
What is the behavior of an object?
The actions and communications it provides
What is the relationship between an object and a class?
A class defines the variables for the state of an object and the methods for the behavior of that object, and an object is a reference of a class.
It’s like classes are the blueprints for objects
What is encapsulation/information hiding?
When you protect an objects data by only allowing it to be changed by methods defined by the object
What is a class?
A data type that defines variables and methods for the state and behavior of objects.
What is client code?
An application that uses one or more classes
What is contained in a class declaration?
access level + class + name + {}
ex: public class Circle {}
What goes inside the body of a class?
Variables, constructors and methods
What are members of a class?
The variables and methods in that class
What is a constructor?
A method used when objects of a class are created- called instantiation. Every time a new object is created the constructor method is called to make it.
The constructor can be helpful to assigning data to an object at the start.
What is the syntax of a constructor?
Constructors are always public, and have the same name as the class.
public + (name of the class) (parameters) {}
Ex:
public Circle (double radius, int diameter) {}
or:
public Box () {}
Can a class have multiple constructors?
Yes, and the compiler will determine which one to use based on the number and types of the parameters
Are classes always public?
Yes! You can declare it as private, but then nothing else could use it and it would defeat the purpose
Can members of a class be private?
Yes, the variables and methods inside of a class can be private.
What are the three kinds of methods a class can have?
Accessor: Used to determine the value of a variable. Usually named something like getRadius. The get shows that the method is used to access or retrieve values
Modifier: Changes the values of variables in the class.
Helper: Methods with the access level static that are only used within the class by other methods to complete tasks
What are instance variables?
Each object has a copy of the variables in the class. These copies are called instance variables. Since an object is an instance of a class, it’s variables are instances of the class variables.
What are class variables?
Variables declared with static, meaning objects cannot change them or get a copy of it. There is only one that all the objects can refer to.
What are instance methods?
Methods that change the state of an object. This includes accessor and modifier methods
What are class methods?
Methods called from the class itself rather than through an object of that class
What is the object class?
Though objects are all instances of another class, the Object class is the superclass of all classes.
What methods do classes inherit from the Object superclass?
equals(obj) - This returns true if the object in the parameters is equal to the object the method was called by
toString() - Returns a String to represent the object
What is ClassCastException?
An error thrown when you use the equals method on two noncompatible objects, like a string and int object
Can classes override the equals and toString methods of the object superclass?
Yes, they can override it to better fit their function and purpose
What is a has-a relationship?
When classes have another class. For example, Strings are a class of object. A class with a string member is demonstrating a has-a relationship
What is the this keyword?
When using the same name for your parameters and the member variables of a class, you can diffrentiate between the two with this
Ex:
private double balance;
public Account(double balance) {
this.balance = balance;
}
The constructor took in a parameter called balance, but wanted to assign it to the class’s member variable called balance
What is object-oriented-development?
An approach to creating software that uses a system of objects that communicate with each other to provide solutions to tasks
What is polymorphism?
An OOP program features that allows objects to assume different types.
When you assign an object a different class, what happens?
It now uses the methods and members of the new class it was assigned.
Example:
Circle wafer;
Disk cookie = new Disk(2, 0.5);
wafer = cookie; //Wafer began as a circle, now a Disk
What is reusability in OOP programming?
It means you can reuse the data and members of a class endlessly by creating objects from them. It reduces development time and decreases the likelihood of bugs
What is modular programming?
A way of programming where each class are in separate files and can be pieced together or used through objects.
What is inheritance?
Creating another class that extends an existing class. Usually this is done when programmers want to create a class that is similar to an existing one, but more specific.
For example, creating a very specific Honda class that extends the Car class
How do you extend a class?
Include the keyword extends in the class declaration:
public class (class name) extends (other class name) {}
In inheritance, what can the “top” class that other classes inherit from be called?
The super class, base class, or parent class
In inheritance, what can the “bottom” class that inherits from the parent/base/super class be called?
The derived class, subclass, or child class
What happens when you inherit a class?
You get all of the classes methods and members that were declared public
Can you inherit all methods or members of a base class?
No, you cannot access methods or members of a class that are declared private or static
What is an is-a relationship?
An is-a relationship is the relationship demonstrated when a class is derived from an existing class.
*This is different from a has-a relationship, which is when a class uses objects from another class.
Can a class inherit multiple classes?
No, java only allows a class to inherit one other class.
How do you call methods of the base class from the subclass?
The super keyword allows you to call methods from the base class in the subclass, access methods from the subclass, and use its constructor.
In simple terms; the super keyword allows you to access all public characteristics of the base class
When an object of a subclass is created, what happens?
The constructor for that subclass is called. However, the constructors for the class it inherits from is also called.
Objects that are part of a hierarchy will call the constructors of the classes they are instances of or extend
*Note that this only happens if none of the constructors have parameters. If they do, the super keyword must be used.
Can a subclass override inherited methods?
Of course! One of the main purposes of inheritance is not only for code reusability, but to also give subclasses a framework that they can tailor to the objects specific needs.
What is an abstract class?
A class that defines an abstract concept.
For example, a Plant class. There is no such thing as a plant. There are trees, and dandelions, but no plant.
Can abstract class be instantiated? (Can an object be created from them)
No, abstract classes cannot be instantiated because they are supposed to represent abstract concepts.
What keyword do you use to create an abstract class?
Abstract
What are abstract methods?
These are methods that must be implemented in the subclasses of an abstract class, but are not defined in the abstract class.
How do you write abstract methods in an abstract class?
To create an abstract method you include the keyword abstract, and define the method’s return type and parameters, but no method body.
abstract String makeSound();
Do abstract methods have to be implemented in the subclasses that inherit from the abstract class?
Yes, they must be implemented.
What is an interface?
An interface is a data structure that allows you to create a class and outline the methods that other classes that implement it should have.
In other words, it sort of allows you to create a blueprint for other classes
What is the difference between an interface and an abstract class?
Interfaces cannot be inherited. They are not part of the hierarchy of inheritance between objects. It can only add behavior to a class.
Abstract methods on the other hand are part of the hierarchy and can be inherited.
What keyword do you use to create an interface?
You use the keyword interface
<access_level> interface <name> {
<return_type> <method_name> (<method_parameters>);
}
Do you declare method’s scope in an interface?
No, all methods in an interface automatically have a public and abstract scope. These methods simply define the return type, name, and parameters that classes implementing this interface must use, not the access level.
How do you declare methods in an interface?
What keyword do you use to implement an interface in a subclass?
implements
public class Circle implements Comparable {}
Do subclasses have to implement all the methods in an interface?
Yes, they must implement every method defined in the interface
Can a class implement multiple interfaces?
Yes!
What is the Comparable interface?
An interface that is part of the java.lang package. Has only one method, compareTo.
What does compareTo(obj) do?
It returns 0 when the objects are equal, a negative integer when obj is less than the object, and a positive integer when obj is more than the object
What is an array?
A structure that can store many of the same kind of data at once.
For example, an array can store 50 integers, or 10 strings, but it cannot store 20 integers and 10 doubles.
What is a piece of data in an array called?
Elements
What do all elements in an array have?
An index, a special number that shows where the element is in an array.
In an array, the index starts at 0.
How do you declare an array?
Can you put elements into an array during declaration?
Yes, in the declaration instead of just saying the number of elements that will be included you can list the elements.
Ex:
String[] friends = [“kernit, “Lucille”, “Sammy”, “Roxy”, “Myah”]
After an array is created, what value is given to each element inside?
The default values of that array type. An array of integers will automatically be 0, an array of strings will automatically be null
How do you access elements in an array?
arrayname[index]
Ex:
System.out.println(friends[2])
How are indexes assigned in an array?
The first value of an array has an index of 0. The index then increases by one as you go through the list. For example, the 5th element will have an index of 4.
What is ArrayIndexOutOfBounds?
An error generated when you try to access an element in a list with an invalid index.
What does traversing do?
Can a method have an array for its parameters?
What is a linear search?
A way of finding a value in an array by going through every single value in the array until the element is found. Can be a bit slow when arrays get very large
What are two dimensional arrays?
Arrays that represent data that corresponds to a grid
How do you declare two dimensional arrays?
How do you search through elements of a two dimensional array?
You often use a nested for loop
How do you get the length of an array?
name.length
What is the ArrayList class?
A class that allows you to create an ArrayList that has methods to add and delete elements also called a collections framework. ArrayLists ONLY hold objects
What are the seven methods of the ArrayList class?
add(int index, obj element) Adds something at index
add(obj element) Adds something to the end of the array
indexOf(obj element) Gives the index of “element” by using its equals method to compare it to each element in the list
remove(int index) Removes element at index
set(int index, obj element) Removes the object at index
size() Returns number of elements in array
What kind of array is ArrayList?
A dynamic array that can vary in size during run time, and shifts the index of elements when items are added or removed
How do you declare an ArrayList?
What happens when you try to store primitive elements in ArrayList?
It will wrap those primitive values in another class to make them objects.
For example, primitive int values will be wrapped in the Integer class and become an object.
Wrapper classes are important because they give each element compareTo or equals() methods that the ArrayList class uses to compare and find values
What does the String class do?
It’s a class that handles and manipulates strings.
Contains the method: toCharArray(), which turns a String object into an array of primitive characters