Java Object-Oriented Programming
Java Object-Oriented Programming
Attributes
Variables found inside the class.
To access, use the object you’ve created and add a dot (.) and insert the variable name.
Syntax:
<objectName>.<variableName>
To modify, same procedure as accessing but add an equal sign (=) then the new value.
Syntax:
<objectName>.<variableName> = <newValue>;
To prevent modification, add final keyword before the variable’s data type. (final keyword is a modifier)
Syntax:
final <datatype> <variableName>;
Methods
Also called as functions
block of code which only runs when it is called.
Creating Methods
must be declared within a class
specify the access modifier (public, protected, default, private), non-access modifier (static, final,
abstract, etc…), return type you want to return, method name (any name will do), parameters (anything
you need in your method; requirements to access the method)
Syntax:
<access modifier> <non access modifier> <datatype> <method name> (<parameters>) {…}
Or
<non access modifier> <datatype> <method name> (<parameters>) {…}
NOTE: parameters are optional.
Accessing Methods
just type the method name and insert the parameters on the parenthesis in order.
Syntax:
<method name> (<parameters>)
Overloaded Methods
methods that share the same name but has different parameters.
RETURN Statement
will return the variable you specified in the return statement within the method.
Syntax:
return <variable name>;
Constructors
special method when an object is instantiated (constructed)
useful when creating multiple objects with different attributes.
Syntax:
<class name> (<parameters>);
when accessing…
<class name> <variable name> = new <constructor>;