Java Flashcards
things an object knows about itself
instance variable
represents object’s state
things an object can do
methods
blueprint for an object
class
class describes what an object ………… and what an object ……….
knows
does
a method uses ……….. A caller passes ……….
parameter
arguments
java is pass-by-………..
value
that means pass-by-copy
getters and setters
accessors and mutators
access modifiers
public
private
hide data
private
declared inside class
instance variables
declared inside method
local variable
local variables …………. initialized before use
must be
check if two objects are equal
.equals()
to compare two primitives
==
a class can have any number of
instance variables
getter setter methods
a method can have only one
return
these help create encapsulation
getter
setter
private
public
convert String to int
Integer.parseInt(string);
iterate in array (enhanced for loop)
for(int cell : locationCells){……….}
get array length
array.length
Math.random()
returns a random number from 0 to just less than 1
while loop is good
when you don’t know how many times to loop and just want to keep going while some condition is true
ArrayList
add(Object elem) - adds elem to the list
remove(int index), remove(Object elem)
contains(Object elem) - returns true if there is a match for the object parameter
isEmpty()
indexOf(Object elem) - returns either the index of the object parameter or -1
size()
get(int index)
ArrayList over arrays
size changes dynamically
can remove elements
ArrayList is less efficient
in order to put primitives in ArrayList
wrap using primitive wrapper class unwrapping happens automatically when taking out the primitive from ArrayList
………… are faster to hold primitives
arrays
ArrayList has some extra load due to wrapping and unwrapping
new String[2];
new ArrayList();
short circuit operators
&&, ||
non short circuit operators
&, |
classes are grouped into
packages
packages importan
help the overall organization of a project or library
give you a name scoping
provide a level of security
java.lang
need not to be imported
preimported
import is not the same as ……….. in C
include
…………. extends …………….
subclass supeclass
add more stuff to super class method
public void method_name(){
super.method_name();
…………….
}
superclass can choose whether or not it wants a subclass to inherit a particular member by the level of access the particular member is given
private - not inherited
default
protected
public - inheritied
inherited methods can be overriden
instance variables cannot be overriden
by using inheritance
get rid of duplicate code by abstracting out the behavior common to a group of classes, and sticking that code in a superclass
with polymorphism, the reference type can be a ………
superclass of the actual object type
polymorphism makes use of
inheritance
with polymorphism
can write code that doesn’t have to change when you introduce new subclass
polymorphic arrays
polymorphic arguments
polymorphic return types
non-public class can be subclassed only by
classes in the same package
what if i don’t declare a class as public
then it is a non-public class
final modifier
end of the inheritance line
can’t extend a final class
can’t extend a class which has only ……….. constructor
private
if you want to protect a specific method from being overridden, mark the method with final modifier
mark the whole class as final if you want to guarantee that none of the methods in that class will ever be overridden
in order to overide
arguments must be same
return types must be compatible
the methods can’t be less accessible
method overloading
having two methods with the same name but different argument list
in order to overload
return types can be different
can’t change only the return type
can vary access levels in any directions
overloaded method has nothing to do with ……..
inheritance
abstract class
a class that can’t be instantiated
interface
100% abstract class
making a class abstract
abstract class Class_name{…..}
abstract class means
it must be extended
abstract method means
it must be overridden
creating an abstract method
public abstract void method_name();
no curly braces
if you declare an abstract method
you must mark the class abstract well
you must implement all ………. methods
abstract the first concrete class in the inheritance tree must implement all the abstract methods
every class we write extends
Object any class that doesn't explicitly extend another class, implicitly extends Object
Object class
boolean equlas() Class getClass() int hashCode() String toString()
is class Object abstrac
no
got method implementation code that all classes can inherit and use out-of-the-box, without having to overriide the methods
you are encouraged to override these methods in Object class
hashCode()
equals()
toString()
most common use of an instance of type Object
thread synchronization
mulitple inheritance problem
deadly diamond of death
to define an interface
public interface Interface_name{…..}
to implement an abstract class
public class Child_class extends Super_class{…..}