Final Exam Flashcards

1
Q

INHERITANCE

A

REDUCES PROGRAM DEVELOPMENT TIME

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses

class MountainBike extends Bicycle {

    // new fields and methods defining
     // a mountain bike would go here

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The direct superclass of a subclass (specified by the keyword extends in the first line of a class declaration) is the superclass from which the subclass inherits. An indirect superclass of a subclass is two or more levels up the class hierarchy from the subclass.

A

Superclass Subclass

Shape Circle, Triangle, Cube

Student UndergradStuedent

Employee Faculty, Staff

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Single Inheritance

A

A class is derived from one direct superclass. In multiple inheritances a class is derived from more than one direct superclass.

Java does not support multiple inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Subclass

A

is more specific than its superclass and represents a smaller group of objects

Every object of a subclass is also an object of that class’s superclass.

However a superclass object is NOT an object of its class’s subclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

is-a relationship

A

Represents inheritance

A object of a subclass also can be treated as an object of its superclass

Ex: Vehicle, represents all vehicles, cars, trucks, boats, bicycles etc…..

**public class Animal**{
 }
public class Mammal extends **Animal{**
 }
public class Reptile extends **Animal**{
 }
public class Dog extends **lMamma**{
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

A Has-a relationship

A

Represents composition,

A class object contains refrences to objects of other classes

Ex: A car has a, steering wheel

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Superclasses and Subclasses

A

Single inheritance relationships form treelike hierarchial structures,

a superclass exists in a hierarchical relationship with its subclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Protected Members

A

A superclass’s public memebers are accessible whereever the program has a reference to an object of that superclass or one of its subclass

A superclass’s private members can be accessed directly only within the superclass’s declaration

A superclass’s protected members have an intermediate level of protection btw public and private access. They can be accessed by memebers of its subclass, and by members of other classes in the SAME package

Superclass’s private members are hidden in its subclasses and can be accessed only through the public or protected methods inherited from the superclass\

Overridden canbe accessed by a super and a dot(.) seperator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Relationship btw Superclass and Subclasses

A

Subclass CANNOT access the private members of its superclass, but CAN access the non-private members

Subclass, can invoke a constructor of its superclass by using the keyword “super” followed by parathensis with superclass arguments. Must appear in the first statement of the subclass constructor body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

_______ is a form of software reusability in which new classes acquire the members of existing classes and embellish those classes with new capabilities

A

Inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

A superclass’s _____ members can be accessed in the superclass declaration and in subclass declarations.

A

Public and Protected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In a ______ relationship, an object of a subclass can also be treated as an object of its superclass.

A

is-a or inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In a _____ relationship, a class object has references to objects of other classes as members

A

has-a or composition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

In a single inheritance, a class exists in a ____ relationship with its subclasses

A

hierarchical

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

A superclasses _____ members are accessible anywhere that the program has a reference to an object of that superclass or to an object of one of its subclasses

A

Public

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

When a object of a subclass is instantiated a superclass ______ is called implicityly or explicityly.

A

constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Subclass constructors can call superclass constructors via the ______ keyword

A

super

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Polymorphism

A

Enables us to write programs that process objects that share the same superclass as if they’re all objects of the superclass

Can simplify programming

We can design and implement systems that are easily extensible. The only parts of a program that must be altered to accomadate new classes are those that require direct knowlege of the new classes that you add to the hierarchy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

If a class contains at least one abstract method, its an_____ object

A

Abstract

may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

public abstract class GraphicObject {
    // declare fields
    // declare non-abstract methods
    abstract void draw();
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Classes from which objects can be instantiated are called _____ classes

A

Concrete

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

_____ involves using a superclass variable to invoke methods on superclass and subclass objects, enabling you to “program in the general”

A

Polymorphism

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Methods that are not interface methods and that do not provide implementations must be declared using the keyword _____

A

abstract

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Casting a reference stored in a superclass variable to a subclass type is called _______

A

downcasting

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Abstract Class

A

can include methods with implementations and abstract methods

25
Trying to invoke a ***subclass-only method*** with a superclass variable
Is NOT allowed
26
Only a **concrete subclass**
**MUST** implement the method
27
**_Encapsulation_**
Classes encapsulate attributes and mehods into objects, which are intimately related ***Implementation details are hidden within the objects thenmselves*** \*Hiding the the complexity of the code \*Hide data \*Variables and Methods or Programs \*Private or Public
28
\*Java IS case sensitve
- Standards, should tag several words to be descriptive (**Lowercase, WITHOUT spaces, first word after the first word starts with a uppercase letter**) keywords are always written in lowercase Always make sure the class name in your code and java filename match. - Never name with a # first
29
3 control structures in every program language and can be applied to Java
+ Always in default is "**sequential**" + Other 2 are exceptions to the standard "sequential" which is "**selection**" and "**repitition**" The IF statement lets you execute a sequence of statements conditionally ** IF-THEN, IF-THEN-ELSE, and IF-THEN-ELSIF** **IF sales \> quota THEN** compute\_bonus(empid); UPDATE payroll SET pay = pay + bonus WHERE empno = emp\_id; END IF; **\* Biggest mistake is not allowing an EXIT to occur**
30
\* IF PROGRAM LOCKS UP
it is in a indefinite loop + This is because the program didn't assign variable in the repeat loop
31
Categories of Repeat Loops (Logic Bases) what you want to do in the repeat loop
+ Entrance Control Loops **while** (test) { body-statements; } // while next-statement; **_ Do loops have the form_** **do** { body-statements; } while (test); next-statement; **for** (initialization; test; increment) { body-statements; } // for + Exit Control Loops
32
Lets say the Loop is using a lot of Arrays
+ to help index the Arrays you can use a ***counter loop *** int x = 0; **while (x \< 5)** { printf ("x = %d\n", x); x++; } + if you have a Array of integers you can use a "***value statement"*** int month = 8; **if (month == 1)** { System.out.println("January"); } else if (month == 2) { System.out.println("February"); } ... // and so on + Array of strings, fastest way to intialize that would be address the array by its apparent name
33
No subscript on Variable...how does Java view that?
+ It views it as the whole block of all of them
34
Catch Block
**declares a type and an exception parameter** The catch block can handle exceptions of the specified type Inside the catch block, you can use theparameters identifier to interact wtih a caught exception object **try {** ``` } **catch (FileNotFoundException e)** { System.err.println("FileNotFoundException: " + e.getMessage()); throw new SampleException(e); ``` } **catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage());** }
35
\* Mathematical Formula with a **Try Catch Block**
+ looking for a divide by zero
36
\* Value of a **Null Value**
+ means NO VALUE
37
\* overloading and overwriting
**_ Overloading_** in Java can occur when two or more methods in the same class **share the same name or even if a child class shares a method with the same name as one of it’s parent classes.** When you use the identifier form, there must be exactly one function with the specified name in scope at the pragma location. Attempting to use the identifier form of #pragma weak with an overloaded function is an error. For example: ``` int bar(int); float bar(float); #pragma weak bar // error, ambiguous function name ``` **Overriding** methods is completely different from overloading methods. If a derived class **requires a different definition for an inherited method**, then that method can be redefined in the derived class.
38
How do we name classes?
``` **class _MyClass_** { // field, constructor, and // method declarations } ``` Modifiers such as public, private, and a number of others that you will encounter later. The class name, with the **initial letter capitalized by** convention. The class body, surrounded by braces, {}.
39
Static Methods
Frequently used tasks Prevents you from reinventing the wheel Called by using its class name followed by a dot (.) and method name **Static methods**, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in **ClassName.methodName(args)**
40
Arrays
Make it conenient to process related groups of values. Remain the same in length once they are created although an array variable may be reassigned such that it refers to a new array of a different length uses keyword new use [] n array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. ``` **public static void main(String[] args)** { // declares an array of integers int[] anArray; ``` ``` // allocates memory for 10 integers **anArray = new int[10];** ``` ``` // initialize first element anArray[0] = 100; // initialize second element anArray[1] = 200; // etc. anArray[2] = 300; anArray[3] = 400; anArray[4] = 500; anArray[5] = 600; anArray[6] = 700; anArray[7] = 800; anArray[8] = 900; anArray[9] = 1000; ```
41
\*Diff btw subroutine and function
- function will ALWAYS return a value - looks the same though
42
When you open a Java file it would be nice to have a print heading to tell you what the program is along with the creator's name...and the method name
\*Look for any declarations that are global (available for all the methods) \*Imports \*Then look for where the program will start executing \*Main module should be a series of calls to different methods (named so well that you can just read the name and tell what it is doing ex: print document ID numbers) \*Any declarations done as a constant should be done at the top of the program
43
Constructors
constructor is **used in the creation of an object that is an instance of a class**. Typically it performs operations required to initialize the class **before methods are invoked or fields are accessed**. Constructors are never inherited. ## Footnote To create a new Bicycle object called myBike, a **constructor is called by the new operator:** **Bicycle myBike = new Bicycle(30, 0, 8);** new Bicycle(30, 0, 8) creates space in memory for the object and initializes its fields.
44
Floating Point Data
hold real numbers 8.8888 Char holds character data
45
**Each class declaration that begins with keyword \_\_\_\_\_** **must be stored in a file that has exactly the same name as the class and ends with the *.java* file name extension.**
***public***
46
**Keyword _____ in a class declaration is followed immediately by the class's name.**
***class*** ***Ex: pulbic class Welcome1***
47
**Keyword ____ requests memory from teh system to store an object, then calls the corresponding class's constructor to initialize the object**
***new***
48
**BY DEFAULT, CLASSES THAT ARE COMPILED IN THE SAME DIRECTORY ARE CONSIDERED TO BE IN THE SAME PACKAGE, KNOWN AS THE \_\_\_\_\_**
***DEFAULT PACKAGE***
49
**WHEN EACH OBJECT OF A CLASS MAINTAINS ITS OWN COPY OF AN ATTRIBUTE, THE FIELD THAT REPRESENTS THE ATTRIBUTE IS ALSO KNOWN AS A \_\_\_\_\_\_\_\_\_\_\_**
***INSTANCE VARIABLE***
50
**KEYWORD *_PUBLIC_* IS AN ACCESS \_\_\_\_\_\_.**
***MODIFIER***
51
**RETURN TYPE _____ INDICATES THAT A METHOD WILL NOT RETURN A VALUE**
***VOID***
52
**SCANNER METHOD \_\_\_\_\_\_\_** **READS CHARACTERS UNTIL IT ENCOUNTERS A NEWLINE CHARACTER, THEN RETURNS THOSE CHARACTERS AS A *STRING***
***nextLine***
53
**VARIABLE of type float represent _____ floating point numbers**
***SINGLE PRECISION***
54
**The format specifier _____ is used to output values of type float or double**
***%f***
55
**Every method's body is delimited by left and right braces ( *{ }* )**
***_TRUE_***
56
Floating-point values that appear in source code are known as floating-point literals and type float by default
***FALSE*** Such literals are of type ***double*** by default
57
Method Overloading
Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
58
A method is invoked with a \_\_\_\_\_\_
Method call