OOP Flashcards
class
template of blueprint from which objects are created and receive their states(properties/fields) and behaviour(methods)
static
class method
belongs to class
don’t need an instance to use it
underline
non static
instance method
belongs to object
need that instance to call it
void
method doesn’t return anything
object
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.
abstraction
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.)
encapsulation
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.)
information hiding
inner workings of a class being hidden from the class user
use access modifier private (in order to have encapsulation)
difference between abstraction and encapsulation
Abstraction hides details at the design level (complexity) , while Encapsulation hides details at the implementation level (inner workings).
public
Accessible everywhere
can be accessed by the class.it is declared in and by by any other class
+
private
-
Accessible only in its own class
protected
Accessible by class and any subclass (in any package) and by any other class in the same package
inheritance
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
default constructor
constructor w/ no parameters
sets attributes of the object to default values
polymorphism
ability of an object or method to take on multiple forms based on its current situation
overriding + overloading
variables
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
local variables
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.
global variables
declared in class above all methods
scope is inside the entire program
can be used by all methods
access modifiers
used to tell which code has access to certain fields and methods
types of variables
primitive- cant invoke methods
reference/class type.-has methods
types of variables
primitive- cant invoke methods
reference/class type.-has methods
constructor
special method in class called when an object is instantiated
purpose- initialise class fields
accessor method
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
mutator method
set method
modify field values
void
other classes can edit data of object
has parameters
typed methods
return a value
void methods
don’t return a value
helper methods
declared as private
solve some problem in the class
called by public methods in the class
instantiation
act of creating an object from the class
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
method overloading
creation of multiple methods in a single class with the same name but with different method signature (parameters/data types )
—method signature changes
-done innone class
can overload static,final or private method
-bonded by static binding at compile time
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 )
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.
constants
cannot be assigned another value later in the programme
public static final NAME
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
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
reasons protected access does not offer secure protection
- 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. - In Java, all classes in the same package have access to protected fields, whether or not they are subclasses.
default access
nothing specified
Accessible by any class or subclass in the same package. This is sometimes called package accessibility
array
data structure
collection of variables of same type
elements ordered + each has a specific + constant position (index)
method
collection of statements that are grouped together to perform an operation
defines behaviour
toString method
create String representation of an object.
every class has default toString method
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}
while loops
repeatedly executes a target statement as long as a given condition is true.
initialisation
while (condition is true) { action
change}
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
advantages of encapsulation
guarantees integrity of an object’s data as it is securely hidden
restricted data access from outside sources
call method from parent class (override)
super.methodName()
for constructor - super(field,field)
composition
The has-a relationship between two Java objects. One object is a field of another object.