terms Flashcards
method overloading
multiple methods w/ same name different params
params can differ in quantity or data types
instantiate
create
eg create a objt by using ‘new’ keywords
initialize
to set up
assign an initial value
eg to initialize means to provide an initial value b4 it’s used.
immutable
cant be changed
declare
state type of var + name
assignment
throwing away old value and replacing with new one.
data types
describes the type of data such as int, str
scope
where something is accessible.
eg in java vars only accessible inside region they were created
What is an interface
why/purpose
how - created, connected
other
image = animal pet
dog class.
= like class, but a collection of abstract methods.
Purpose
- Since Java doesn’t support multiple inheritance for classes, interfaces allows a work around by allowing a class to rcv the behaviors of a class and the behaviors from the interface.
How
- interface is declared the same way a class is except the "interface" keyword is used instead of 'class.' eg public interface Name {
-interface is implemented by using the “implements” keyword the same way “extends” keyword is used for classes. eg
public class Name implements Nameofinterface
-an interface can extend to another interface by using the “extends” keyword. the child will then inherit parent’s methods.
What is abstraction
why/purpose
how - created, connected
other
image = atm
anything with abstraction think of hiding
= allowing the user to focus only on the functional deliverables of a service while hiding the internal implementation(process).
- security
- enhancements w/o impacting the user as user only sees buttons
- maintainabilty and modularity
-real world eg atm
by using GUI screen we can implement abstraction.
-
what is a data structure
= org of data in such a way so that it can be used efficiently.
algo
= recipe. step by step procedure for solving a problem
polymorphism
= many forms. the ability for an object to take on many forms.
it occurs when classes inherit the attributes and methods of another. poly allows us to use those methods to perform diff tasks. this allows us to perform a single action in diff ways.
There are 2 ways to implement polym. oload and oriding. o loading is when there’s a single method name, but diff params. these params can be different either in quantity or data type.
oriding = same method being implemented differently.
what
= many forms. e.g. 1 method multiple implementations.
how
There are 2 ways to implement poly: overloading and overriding.
And there are 2 ways to overload a method: a. same method name with diff number of parameters. b. same as a, but with different data types.
eg a class can have same method name, but different params.
method overriding = same name different implementation. eg if a child class has the same method as the parent class, but a different implementation.
if time
static poly > olading
dynamic poly > orriding, must be inheritance, is-a relationship.
abstraction
what
= useful - details
showing only what’s relevant and useful for the person using it while hiding the implementation process.
eg. atm, elevator
There are 2 ways to implement abstraction:
a. abstract classes (provides partial abstraction)
b. interfaces (100% abstraction)
a. abstract classes are classes that are declared abstract. They are parent classes that cant be instantiated. you would need to instantiate one of its child classes if you want to create a new objt. They may contain both abstract and non abstract methods. b. interfaces are similar to classes but consist of abstract methods.
Encapsulation
= binding data + behavior into a single unit. an example of this is the java class.
The purpose is to enhance data security. That’s achieved through the use of access modifiers and internal related methods such as getters and setters which provide indirect access to the fields.
how
It protects the data by making fields private and allowing access to them only through their related methods such as getters and setters.
1. via access modifiers. are keywords that facilitate the encapsulation of components by setting the accessibility of classes and methods. There are 4 types. private, public, protected, default. private- accessible only w/in declared class public - accessible by any other class protected - same package and subclass default - specifies default block of code in a switch statement.
- getters returns the value of the attribute.
naming - start with the word “get” followed by var name eg getName
setters takes a parameter and assigns it to the attribute.
naming - start with the word “set” followed by var name eg setName
how they work together
eg
- getter and setter is defined in the class
public class Vehicle { private String color;
// Getter public String getColor() { return color; }
// Setter public void setColor(String c) { this.color = c; } }
- once they have been defined we can use it in our main
public static void main(String[] args) { Vehicle v1 = new Vehicle(); v1.setColor("Red"); System.out.println(v1.getColor()); }
// Outputs “Red”
Data protection
Inheritance
= a child class acquires (inherits) the attributes and behaviors from the parent class.
why
REUSEABILITY AND METHOD OVERRIDING
how
by using the “extends” keyword you inherit and can reuse the methods and vars.
eg
class Subclass-name extends Superclass-name
There are 3 types of class inheritance:
single, multilevel and hierarchical.
multiple and hybrid are supported via interface only.
Instantiate an object from a class
class Book { String title; String author; int numberOfPages; String publisher; }
Book myBook = new Book (“Coding is art”, “Becky James”, 425);
book = var type (class name) myBook = var/objt name new = keyword to create object Book() = constructor with fields/ args?
fields
= attributes,
fields
dot notation
to access fields. to accss a method or var that lives in an objt.
write the name of an object followed by an attribute name of interest, separated by a dot: instanceVariableName.attributeName
eg
myBook.title = "Coding is Art"; myBook.author = "Becky James"; myBook.numberOfPages = myBook.numberOfPages + 10;
dot(.) = within
dodge.getGas()
accessing the getGas() method w/in the class / object dodge.
abstract method
a method with only declaration not implementation. ends with semicolon not curly braces
purpose
An abstract method is how you say, “Here’s something that all the things that extend this class have to do, but they each get to specify how exactly they will do it.”
can only be used in abstract class
declare: abstract type name ();
constructor
similar to a method. job is to initialize objt’s vars.
eg
Student s1 = new Student(“Durga”);
we’re creating an instance of the class student called s1. therefore, we inherit the var called name which is currently null. then we are passing in the name Durga to the student constructor and thus name now = Durga.
and creating a var / objct called s1. then the name Durga will be passed into the student constructor and the result will be stored in the var.
- new
- s1 objt created
- go to Student Constructor
- Input Durga (for name) and 101 for row number
rules
-name must be same as class
2 types:
-default(no args) and paramaterized (has specific number of parameters)
oop
= is a programming methodology for designing a program centered around the concepts of classes, objects, PAIE.
class
= a class is blueprint or template from which objects are created. It defines objt data type and methods.
object
= instance of a class. has state and behavior.
conditional statements
checks if a condition is true. if so, then a block of code will be executed.
statements:
- if
- if else
- if, else if - if() do x, else if() do y, else if() do z, else
- nested if if()
if()
print x and y - switch statement tests for equality. if the initial expression equals a case expression then that block of code will be executed + all cases below it.
if the test expression does not equal any of the cases then the default case will be executed.
looping
executes a code block as long as condition is true.
types of loops
while
do-while
for
while - checks a condition and then executes a block of code.
do-while executes a block of code and then checks a condition.
for - for( var; condition; counter) for (i = 1; i<=5; i++)
while
while(Boolean_expression) {
// Statements
}
do-while
do {
// Statements
}while(Boolean_expression);