1/17
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Core OO Concepts in Java
encapsulation principles and keywords remain the same as C++
classes
attributes
methods
constructors
public
private
composition
Classes
Use the class keyword to define
Create in a .java file matching the name of the class
Almost always declare as public
There is no header file
Attributes
Create as variables inside the class
Almost always declare as private
Methods
Create inside the class
May be declared public or private
Implementation also goes inside the class definition
Constructors
Create as a standard method inside the class
Name must match the name of the class
No return type defined
Multiple constructors are permitted, provided the parameters are different
public
Access modifier that states the following method/variable is accessible to all code
Add the public keyword explicitly before each method/variable
private
Access modifier that states the following method/variable is accessible only to code inside the class
Add the private keyword explicitly before each method/variable
Composition
Classes may hold attributes of class type, not just primitive types…
Creating Object Instances - new
Just like C++, once defined, classes are types and can be realized into object instances
By yourself, or by other programmers you give your class file to
A realized class is called an object instance and is treated just like any variable
In Java object instances are built from a class by using the new keyword:
Car c = new Car("White");
Java Object Creation
Car c = new Car("White");
Car c is a reference to Car object
Java Object Referencing
Java objects are automatically garbage collected (deleted) when they have no references…
Car c = new Car();
Car x = new Car();
Value Types and Reference Types
it is the data itself you are accessing/modifying or passing into a function
use pure lower case
E.g. int, char, float, double, boolean…
Reference Type
use object references to objects
whenever they are used, it is the reference that you are accessing/modifying
should use camelCase
E.g. Car, String, FluffyElephant, Minion, CatDog…
The this keyword in Java
a special keyword that returns an object reference to the object instance which the current method is executing in
can be used to unambiguously refer to methods and variables:
which can be particularly useful in constructors
but also anywhere we want to be explicit
or anywhere we want to pass the current object as a parameter
(C++ also has this, but it's a pointer)
The Java Class Library
a vast set of pre-written classes
e.g. String, System, Math
API Documentation
Programmers document their classes, so other programmers can learn how to use them in their applications
Java uses a standard called JavaDoc
All Java programs use this, so they all have documentation in the same common format
This makes it very easy to learn about other people classes which promotes reuse
JavaDoc
Documentation is embedded inside your code using comment blocks
Remember Java can use /* */ comments like C
JavaDoc uses /** **/ to identify a JavaDoc comment
Additional ‘*’ characters on following lines just make it look pretty
JavaDoc is used to document all public classes, methods and constructors
Comment block goes immediately before the item it is documenting
/*Car class represents all drivable vehicles with 4 wheels
the class is also capable of modelling the outward appearance, car model and the number of miles driven*/
public class Car{
}
Keywords allow structured documentation of methods, return values and required parameters
@param documents the meaning of the parameter called ‘name’
@return documents the meaning of a value returned from a method
Many more: https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html
/**
*creates a new Car with the given characteristics.
*@param col = the colour of the car being created.
*@param miles = the number of miles the car has driven.
**/
public Car(String col, int miles) {
colour = col;
milesDriven = miles;
}
JavaDoc can then generate written documentation automatically
The semi-structured comments and the Java code are processed to build web pages documenting your classes
These can then be posted online let other programmers know how to use your classes
Simply run the javadoc tool from the command line to generate the web pages
javadoc –d doc *.java
The “-d” parameter specifies the directory (folder) to store the web pages in
After running javadoc, just double click ‘index.html’ in this folder
Recommend always specifying a folder – Javadoc produces a lot of html
The String Class Example
public static void main(String[] args) {
String s = "Hello World";
//examples of invoking methods on an instance
int len = s.length();
String l = s.toLowerCase();
char[] myArray = s.toCharArray();
}