1/25
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
Static: attributes
Define class level members that belong to the class itself not to any individual object.
If one object changes the variables value, all others see the change too.
Static: methods
can be used when a method does not use any object
Factory methods
a static method that creates and returns an object
local date uses static factor
(i) private static multiplierFactor = 100
(ii) private static void calcTax(int month);
i) shared by all projects - there is only one copy of this variable in the whole class not one per object
stays in memory as long as the class exists
can use it without creating an object
ii) Class method: callable without an instance: ClassName.calcTax(…)
cannot refer to instance fields/methods directly
java supports multiple inheritance of interfaces
why use interfaces
if you need a set of classes to have specific functionality but dont want to force them into the same inheritance tree
Interface
A description of a set of behaviour or actions
A class that implements an interface must implement the methods declared in the interface
Interface syntax
All methods declared in an interface are public and abstract
All implementing classes must make these methods public in order to access and override them
Cannot make interface methods private, protected or package private
Interfaces and attributes
cant have attributes but can define constraints - public static final
Interface constant
public interface Shape{
double Pi = 3.14159; //implicitly public static final
}
why are interfaces useful
A way to guarantee behaviour across a set of unrelated classes.
A class can implement more than one interface.
Safe polymorphism
Why default methods
Interfaces can evolve without breaking older code
Rules
Can’t instantiate an interface.
All methods implicitly public
Where a class implements an interface, instances of the class can be treated as objects of the interface type as well as of their own class type.
Interfaces can inherit from each other
Array List
flexible size (init 20)
Expands & shrinks automatically
Can take any type of object
ArrayList Syntax
ArrayList<Class> = new ArrayList<Class>;
Add new element to end of array
public void add(Base_type newElement);
Add new element somewhere in array list based on index:
public void add(int index, Base_type newElement);
Return element at a particular position:
public Base_type get(int index)
Looping through an ArrayList
For(String element: namesList)
System.out.println(element);
Copying an array list
ArrayList<String> aList = new ArrayList<String>();
ArrayList secondList = aList.clone();
Primttive data types
Faster performance
Less error prone
Primitive vs Reference types
Primitive variables store the actual values.
Reference variables store the address of the object
Final
On a variable: value cannot change after its assigned.
On a method: Subclasses cant override - lock behaviour, protect logic in behaviour, prevent accidental overrides
Opening a file:
File fleExample = new File(“Example.txt”);
Reading a file:
java.util.Scanner
File fleExample = new File(“Example.xpl”);
Scanner myScanner = new Scanner(fleExample);
writing to a file: PrintWriter
PrintWriter myOutFile = new PrintWriter(someFile);
myOutFile.println(“whatever you want to write”);
myOutFile.close();