Classes Flashcards
Class
- have behaviors and states
- template used to instantiate objects. It is also called a type when used with a reference variable. A class used to instantiate an object determines what states and behaviors an object will possess. A class used as the type for a reference variable determines what behaviors of an object can be invoked, and how any variables get initialized.
Access Modifiers
- determine the visibility of states and behaviors to other classes
- public (all classes)
- private (only the class its declared in)
- protected (all classes in the same package or extended)
- default (only within the same class in which it is declared)
Getters and Setters
- aka accessor and mutator methods
- getter method returns the value of a variable stored in an object
- setter method allows the user to change the value of a variable
Constructor
- a special method called at instantiation, used to create an object and initialize variables
- its a block of code executed when a class is instantiated.
- all classes have at least one constructor
- same name as the class and no return type
Overloading
- Methods and constructors can be overloaded
- methods have the same name but have different number and type of parameters
- we tell methods apart through the input parameters
- overloaded method must have a different number and/or types of parameters and/or order of parameters
- overloaded method can throw Exceptions
- overloaded method can return a different datatype
this keyword
a keyword in Java that refers to the object in which the keyboard appears
Object
An instance of a class in memory. In Java, you never interact with objects directly. Instead, you interact with them through their reference, which is the memory address used by the JVM to find a particular object.
Reference variable
A variable that stores the reference to an object in memory. Just like the type of a primitive variable determines the range of values that a primitive variable can store, the type of a reference variable determines what types of objects a reference variable can store a reference to. When a class is used as the type of a reference variable, that reference can only be used to invoke behaviors of the object that are declared in the class/type.
Default constructor
if you create a class and don’t specify a constructor the compiler will generate one for you. The one generated is called the default no-arg constructor
Object Class
-it is the parent class of all classes defined in Java
Object toString() method
- gives a string representation of the object
- prints memory address of object by default
Object hashCode()
-unique identifier of the object
Object equals(Object o)
used to compare two objects
Object finalize()
called by the garbage collector for any object instance before it is removed from memory
Object clone()
- creates a copy of the object
- creates a new instance of an object with all the properties initialized to the same values