OOP Flashcards

(76 cards)

1
Q

class

A

template of blueprint from which objects are created and receive their states(properties/fields) and behaviour(methods)

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

static

A

class method
belongs to class
don’t need an instance to use it
underline
only 1 instance of a static variable exists

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

non static

A

instance method
belongs to object
need that instance to call it

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

void

A

method doesn’t return anything

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

object

A

an instance of a class
there can be multiple instances of a class in a program.
contains both the data and the function, which operates on the data.
has its own unique properties(data) and each method can be called on each object.

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

abstraction

A

displaying only essential information and hiding the details.

creating new data types suited for a specific application (that the programming language does not know about.)

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

encapsulation

A

hiding the code and data into a single unit to protect the inner workings of an object (concealing the implementation of the object)

makes use of information hiding (make properties private - use methods to access them.)

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

information hiding

A

inner workings of a class being hidden from the class user

use access modifier private (in order to have encapsulation)

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

difference between abstraction and encapsulation

A

Abstraction hides details at the design level (complexity) , while Encapsulation hides details at the implementation level (inner workings).

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

public

A

Accessible everywhere
can be accessed by the class.it is declared in and by by any other class
+
no restrictions on where an
instance variable or method can be use

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

private

A

-
Accessible only in its own class
instance variable or method cannot
be accessed by name outside of the class

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

protected

A


Accessible in its class and any subclass and by any other class in the same package

very weak protection compared to private

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

inheritance

A

one object acquires the properties and behaviours of the parent object (along w having their own unique attributes)

key word- extends use clear arrow

parent- child relationship between superclass + subclass

suitable when 2 classes have many fields + methods in common

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

default constructor

A

constructor w/ no parameters
sets attributes of the object to default values

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

polymorphism

A

ability of an object or method to take on multiple forms based on its current situation
overriding + overloading

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

variables

A

describes the fields/Attributes/Characteristics/Properties of the object

store data for processing.

given a name (identifier), are of a specific type and have a value assigned to it.

all take up a different amount of size + has a unique address

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

local variables

A

declared inside methods
do not exist when method is finished, cannot be used outside of method
used instead of global- space in RAM not wasted
Different methods can have local variables with the same names because the methods cannot see
each other’s local variables.

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

global variables

A

declared in class above all methods
scope is inside the entire program
can be used by all methods

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

access modifiers

A

used to tell which code has access to certain fields and methods

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

types of variables

A

primitive- cant invoke methods
reference/class type.-has methods

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

types of variables

A

primitive- cant invoke methods
reference/class type.-has methods

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

constructor

A

special method in class called when an object is instantiated
purpose- initialise class fields

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

accessor method

A

get method
have a specific data type/ return type
returns a field value
allows for visibility of data - other classes will have access to the values
no parameters
allow the programmer to obtain the value of an object’s
instance variables

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

mutator method

A

set method
modify field values
void
other classes can edit data of object
has parameters
allow the programmer to change the value of an object’s instance variables in a controlled manne

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
typed methods
return a value
26
void methods
don’t return a value
27
helper methods
declared as private solve some problem in the class called by public methods in the class
28
instantiation
act of creating an object from the class
29
scope of variable
defines where variable can be used in a programme and its lifetime depends on where it is declared in the program local or global
30
method overloading
creation of multiple methods in a single class with the same name but with different method signature (parameters/data types ) you cannot overload based on the return type —method signature changes -done in none class can overload static,final or private method -bonded by static binding at compile time A signature consists of the name of a method together with its parameter list Differing signatures must have diff numbers and/or types of parameters
31
method overriding
child class uses method created in parent class (same name) @override -method signature remains the same -only done in subclass -cannot override static,final + private method -subject to dynamic binding at run time (check which method to use )
32
method resolution
When you have methods that have been overloaded, Java needs to figure out which method to call based on the parameters given when the method is called.
33
constants
cannot be assigned another value later in the programme public static final NAME
34
formal vs actual parameters
actual- when you call a method and pass real values in it formal-when you create a method and pass values as parameters
35
method signature
first line that describes whether or not the method is typed or void, what its name is, what parameters it contains, modifier,return type,name,parameters
36
reasons protected access does not offer secure protection
1. Anyone can create a subclass from an existing class and could therefore access and change values in fields which could have dire consequences on applications using those classes. 2. In Java, all classes in the same package have access to protected fields, whether or not they are subclasses.
37
default access
nothing specified Accessible by any class or subclass in the same package. This is sometimes called package accessibility
38
array
data structure collection of variables of same type elements ordered + each has a specific + constant position (index)
39
method
collection of statements that are grouped together to perform an operation defines behaviour
40
toString method
create String representation of an object. every class has default toString method
41
for loops
allows you to efficiently write a loop that needs to execute the same code a specific number of times. for (initialisation,condition,change) { action}
42
while loops
repeatedly executes a target statement as long as a given condition is true. initialisation while (condition is true) { action change}
43
advantages of inheritance
Reusability of code: we can use our existing code in our subclass from the super class • Subclasses can take on existing functionality while creating their unique methods for their access only • information hiding
44
advantages of encapsulation
guarantees integrity of an object’s data as it is securely hidden restricted data access from outside sources
45
call method from parent class (override)
super.methodName() for constructor - super(field,field)
46
composition
The has-a relationship between two Java objects. One object is a field of another object. uml diagram - arrowhead is diamond
47
casting
Provided code compiles correctly, upcast will never generate run time errors Downcasting casts a parent object into one of its descendant classes
48
copy constructor
a type of constructor that creates an object using another object of the same class. The process of initializing members of an object through a copy constructor is known as copy initialization. It is also called member-wise initialization because the copy constructor initializes one object with the existing object, both belonging to the same class on a member-by-member copy basis. single argument of same type as class created object should be separate & independent copies instance variables, use setters for primitive variables, else make new object for class variables (class must have its own copy constructor)
49
static binding
aka early binding decision of which definition of a method to use (method definition is associated with its invocation) is made at compile time made based on the type of the variable naming the bject uses static, not late, binding with private, final, and static methods
50
final
method marked final indicates that it cannot be overridden with a new definition in a derived class ▪ If final, the compiler can use early binding with the method public final void someMethod() { . . . } ▪ A class marked final indicates that it cannot be used as a base class from which to derive any other classe
51
upcasting
taking a child instance to a parent type. object of a derived class is assigned to a variable of a base class (or any ancestor class)
52
downcasting
Downcasting is when a type cast is performed from a base class to a derived class (or from any ancestor class to any descendent class) ▪ Downcasting has to be done very carefully ▪ In many cases it doesn't make sense, or is illegal Downcasting to a specific type is only sensible if the object being cast is an instance of that typ discountVariable (DiscountSale)saleVariable; = //will produce run-time error discountVariable = saleVariable //will produce compiler error
53
clone method
Every object inherits a method named clone from the class Object ▪ The method clone has no parameters ▪ It is supposed to return a deep copy of the calling object ▪ However, the inherited version of the method was not designed to be used as is ▪ Instead, each class is expected to override it with a more appropriate version protected Object clone() If a class has a copy constructor, the clone method for that class can use the copy constructor to create the copy returned by the clone method Overriding the clone( ) method is better than using a copy constructor, because dynamic binding ensures that the copy is always of the right type; a copy constructor cannot do so.
54
covariant return types
overriding a method, if it is a class type then the returned type can be changed to a descendant class of the returned type when Changing the return type from Object to the type of the class being cloned is allowed because every class is a descendent class of the class Object method is one that can be replaced by a "narrower" (derived) type when the method is overridden in a subclass.
55
precondition of a method
states what is assumed to be true when the method is called
56
postcondition of a method
states what will be true after the method is executed, as long as the precondition hold
57
access permission overriden method
access permission of an overridden method can be changed from private in base class to public (or other more permissive access) in the derived class access permission of an overridden method can't be changed from public in base class to more restricted access permission in derived class
58
superclass
base class more general and inclusive, less complex
59
subclass
derived class is more specialized, less inclusive, more complex private method from parent is completely unavailable, unless invoked indirectly
60
package/default
aka friendky access instance variable or method definition that is not preceded w/ a modifier can be accessed by name inside definition of any class in same package more restricted than protected If a class in the current directory is not in any other package, then it is in the default package
61
binding
associating a method definition with a method invocation is called binding
62
late binding
aka dynamic binding method definition is associated with its invocation when method is invoked (at run time) for all methods (except private, final, static) allows for method to be written in base class to perform a task, even if portions of that task aren't yet defined
63
Animal myPet = new og() Dog extends Animal variable and object
variable type: animal object type: dog The type of a class variable determines which method names can be used with the variable ▪ However, the object/argument named by the variable determines which definition with the same method name is used
64
order of visibility
private → none (default/package-private) → protected → public private - Only accessible within the same class (most restrictive) none/default - Accessible within the same package protected - Accessible within the same package AND by subclasses in other packages public - Accessible everywhere (least restrictive)
65
dynamic binding can apply to instance methods
true
66
boolean default
false
67
primitives default
0 / 0.0 (zero of their type)
68
class type default
null
69
boxing
process of going from a value of a primitive type to an object of its wrapper class create an object of wrapper class using the primitive value as an argument new object will contain an instance variable that stores a copy of the primitive value
70
unboxing
process of going from an object of a wrapper class to the corresponding value of a primitive type no parameters Object.objectValue() eg : int i = integerObject.intValue();
71
references
Every variable is implemented as a location in computer memory. When the variable is primitive, its value is stored in the memory location assigned to the variable. Each primitive type always require the same amount of memory to store its value. When the variable is a class type, only the memory address (or reference) where its object is located is stored in the memory location assigned to the variable. The object named by the variable is stored in some other location in memory the value of a class variable is a memory address or reference
72
call by values
A method cannot change the value of a primitive variable that is an argument to the method A method can change the values of the instance variables of a class type that is an argument to the method
73
immutable
A class that contains no methods (other than constructors) that change any of the data in an object of the class is called an immutable class ▪ Objects of such a class are called immutable objects ▪ It is perfectly safe to return a reference to an immutable object because the object cannot be changed in any way ▪ The String class is an immutable class
74
mutable
A class that contains public mutator methods or other public methods that can change the data in its objects is called a mutable class, and its objects are called mutable objects ▪ Never write a method that returns a mutable object ▪ Instead, use a copy constructor to return a reference to a completely independent copy of the mutable objec
75
deep copy
copy that, with one exception, has no references in common with the original ▪ Exception: References to immutable objects are allowed to be shared
76
package access
aka default access package access is more restricted than protected can be accessed by name inside the definition of any class in the same package cant be accessed outside the package Package access gives more control to the programmer defining the classes Whoever controls the package directory (or folder) controls the package access All classes in the current directory (not belonging to some other package) belong to an unnamed package called the default package