Object Oriented Programming Part 2

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/25

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No study sessions yet.

26 Terms

1
New cards

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.

2
New cards

Static: methods

can be used when a method does not use any object

3
New cards

Factory methods

a static method that creates and returns an object
local date uses static factor

4
New cards

(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

5
New cards

java supports multiple inheritance of interfaces

6
New cards

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

7
New cards

Interface

A description of a set of behaviour or actions
A class that implements an interface must implement the methods declared in the interface

8
New cards

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

9
New cards

Interfaces and attributes

cant have attributes but can define constraints - public static final

10
New cards

Interface constant

public interface Shape{

double Pi = 3.14159; //implicitly public static final
}

11
New cards

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

12
New cards

Why default methods

Interfaces can evolve without breaking older code

13
New cards

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

14
New cards

Array List

flexible size (init 20)
Expands & shrinks automatically
Can take any type of object

15
New cards

ArrayList Syntax

ArrayList<Class> = new ArrayList<Class>;

16
New cards

Add new element to end of array

public void add(Base_type  newElement);

17
New cards

Add new element somewhere in array list based on index:

public void add(int index, Base_type newElement);

18
New cards

Return element at a particular position:

public Base_type get(int index)

19
New cards

Looping through an ArrayList

For(String element: namesList)
System.out.println(element);

20
New cards

Copying an array list

ArrayList<String> aList = new ArrayList<String>();

ArrayList secondList = aList.clone();

21
New cards

Primttive data types

Faster performance
Less error prone

22
New cards

Primitive vs Reference types

Primitive variables store the actual values.
Reference variables store the address of the object

23
New cards

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

24
New cards

Opening a file:

File fleExample = new File(“Example.txt”);

25
New cards

Reading a file:
java.util.Scanner

File fleExample = new File(“Example.xpl”);

Scanner myScanner = new Scanner(fleExample);

26
New cards

writing to a file: PrintWriter

PrintWriter myOutFile = new PrintWriter(someFile);
myOutFile.println(“whatever you want to write”);

myOutFile.close();