Java Flashcards

Learn Java concepts and patterns

1
Q

Class

A

An immutable blueprint for an object with methods and fields (variables). Like a struct in C. Building blocks for components

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Object

A

A stateful and unique instantiation of a class which are stateful and active, so they are and can do something

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Instantiation

A

Creating new objects using constructors and the ‘new’ keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Constructor method

A

Usually comes withtin the class and has the same name as the class. Can take parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Keyword: new

A

Constructor used when creating a new instance of a class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Keyword: private

A

An access modifier for attributes, methods and constructors making them only accessible within the declared class via getters and setters. Cannont be directly accessed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Keyword: public

A

An access modifier for attributes, methods and constructors making them visible and accessible to other classes (Globally) . They can also be modified unless they are declared final

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Keyword: final

A

Anything declared final cannont be modified, overriden and not extended if it is a class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Keyword: static

A

Elements declared as static are associated to a class, not actual object instances e.g. Main. They are shared between all instances of that class and can be accessed without instantiation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Attribute

A

Things like variables within a class. Each object has it’s own copies of attributes and can be plain data types like int or char. Capture what objects can be

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Methods

A

Capture what objects can do. Take parameters and return values. Main way to communicate with an object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

References

A

Like standing in for the object itself (e.g. SingleMove move = new SingleMove. move would be the reference). Really just placeholders that signify the presence of an object. If an object is no longer referenced it is slated for garbage collection. References to objects are given particular types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Null Refernces

A

References that point to nothing and can cause errors when called in the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Overloading

A

When two or more methods have the same name but with different parameters in the same class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Overriding

A

When a method in a subclass (child) has the same method as a parent class. When the subclass needs a different implementation of the method to the parent class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Keyword: this

A

Provides a reference to the current object whose method is being executed. Cannot be used in a static element but can be used in a method or constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Inheritance

A

When a new child class ‘extends’ a parent class and inherits all the features from the parent class (Attributes, Methods, etc.) but you can add or adapt features so the new class does what you want

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Usage of inheritance

A

Child classes can provide new or alter old functionality by: adding extra attributes/methods or overriding existing methods. Renders parent classes more re-useable as they are extendable and adaptable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Sub-class

A

A sub-type that supports both inheritance (recives parent features for free) and polymorphism (features of sub class can be used in place of a feature of a class)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Polymorphism

A

Every reference belongs to a class, but a reference can be made to any object of a sub-class of the references class which does not change the reference’s class. This is the priciple that arises from the fact that one reference can refer to various different classes. It lets you use an object of a sub-class as if it was an object of some super class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Class hierachy

A

A class is derived from one direct super class only. The root of the class hierarchy is the class object. Every class in Java, directly or indirectly, inherits from the class object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Single dynamic dispatch

A

When an overridden method is called via a reference, the actual method to execute is selected based on the type of the object referenced, not the reference type. Since the decision cannot be made at compile time, dynamic dispatch refers to the choice of code execution as resolved at runtime. The method to execute is chosen based on the availability of an implementation nearest to the reciver class upwards in the hierarchy. Parameters are treated as static

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Keyword: abstract

A

If a class is abstract, we cannot make instances of it. Often these classes are purely conceptual without instances e.g. shape. Abstract methods are declared but don’t have any implementation (any non-abstract sub-class is forced to implement all these methods). A class with one or more abstract method must also be declared abstract

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Benefits of inheritance

A

DRY code, code may be maintained reliably, faciltitates polymorphism

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Reference structure: Mammal dolphin = new Dolphin();
(Dolphin extends Mammal)

A

Reference type: Mammal, Reference: dolphin, Constructor: new, Type of object referenced: Dolphin

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Double/Multiple dispatch

A

Dispatch based on two or more types where the next code to run depends on the two types that are interacting with each other. This is also dynamic as it is chosen at runtime. Good use could be collisions in games.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Double/Multiple dispatch use in Java

A

To do this in Java we can use single dispatch twice recursively and dynamically dispatch on a reciver but also turn the parameter of the call into a dynamic receiver within the method that is dynamically dispatched

24
Q

How is the visitor pattern useful

A

Facilitates the addition of new operations to existing object structures without modifying those structures. Implements all appropriate specializations. The visitor takes the instance reference as input and implements the goal using double dynamic dispatch

25
Q

Label each part of the visitor pattern used in Scotland Yard (Based on the visitor pattern diagram)

A

Abstract base class element: Move
Concrete elements: SingleMove, DoubleMove (Second single dispatch happens here in the accept method. It looks at the underlying type of the incoming visitor and call its visit method, passing in the type of the element)
Abstract base class visitor: Visitor (With abstract methods for visiting single and double moves)
Concrete Visitors: MoveVisitor (With overloaded visit methods for single and double moves), FunctionalVisitor (Not used)
Client: Advance (First single dispatch happens here when the method called is that of the underlying type of the move being used.

26
Q

Exceptions

A

Occur when something fails and an ‘exceptional circumstance’ arises. Usually leads to the program exiting. Can handle exceptions using a try catch block

27
Q

Throwing exceptions

A

We can do this ourselves to see where errors are occuring. If a methods doesn’t deal with the exception, it can forward it to the caller instance to deal with

28
Q

Exit codes

A

Program produces an exit code when it finishes. The default code is 0 but you can force this to happen early and define the semantics of the code as numbers between 1-127 indicate an error

29
Q

Keyword: super

A

We can invoke an overridden parent method using super (e.g. super.doSomething()). We can also use it to access a shadowed parent attribute (e.g. super.name). We can also call a parent constructor, however this must be the first line in the sub-class constructor (e.g. super(name))

30
Q

Downcasting

A

When retreving objects, we might need to turn them from type object to the type we expect, so we can access particular members which is called casting. Dowcasting is casting from a super class to a sub-class

31
Q

Generics

A

Generics allow the compiler to keep track of object types and type checks at compile time rather than relying on the programmer . Generics allow us to parameterise classes, interfaces, and static methods with types which removes the need for uncontrolled downcasting. They allow us to use generic types (e.g. List<T> ListOfThings ... where T is the generic Type)</T>

32
Q

Garbage collection

A

When a reference is lost to an object on the heap it, it can’t be reached any more so the garbage collector will pick up the lost items. You can’t force Java to run the collector but you can strongly suggest it at certain times. But Java decides

33
Q

Reference counting pros and cons

A

When the count of references to an object falls to 0 it is marked for garbage. Unable to easily detect cycles of grabage object. Inefficient to keep count of the counts. Difficulty dealing with multiple threads. Detection of grabage is concurrent with execution

34
Q

Mark and Sweep

A

Mark things that are unreferenced and need collecting then delete the marked objects by removing the references. This is doe by pausing for milliseconds to clean and then back to the program to continue. But this leaves the heap with lots of holes, so if you need lots of continuous memory, we compact what’s left so there is lots of concurrent memory rather than lots of small amount of memory. This is not efficient though.

35
Q

Hotspot heap structure

A

Split the heap into areas of increasing object maturity based on the working set hypothesis. Hot areas are always changing and is needed a lot. Cold areas are permanent stuff that has been around for a long time. There is a hot subspace that is easy to clean (the begining, eden) and then anything that is kept is moved along and everything else is cleaned away giving min work for the garbage collector

36
Q

The G1 Garbage collector

A

There are lots of different eden, survivor, and old generation spaces that can be scaled for the program.

37
Q

Limitations of generics

A

No prim type instantitation (e.g. List<Integer> NOT List<int>)
No new instances of type parameters
No static fields with type parameters
No arrays of parameterized types
No generic exceptions
No overloading to common raw type</int></Integer>

38
Q

Generic inheritance

A

You can inherit from generic classes, where children are allowed to use all the type parameters of the parent and define their own ones (e.g. Child<T, V> extends Parent<T> where T is the parent type parameter)</T>

39
Q

Generic type inference

A

The comilers ability to consider a methods invocation and corresponing decleration to determine the arg types that make the invocation applicable. Tries to find the most specific type that works with all args

40
Q

Wildcards and Intersection types

A

Java allows us to inject a limitation to the type used as generic parameters by providing an upper or lower bound on the inheritance structure using a ‘?’ (e.g. Method1(Stack<? extends Robot> stack which accepts only parameters that are Robots or extend that)

41
Q

Type erasure

A

All generic types are removed at runtime and replaced with their bounds (or Object if unbound). Type casts are used if necessary to preserve type safety and bridge methods to preserve polymorphism

42
Q

Encapsulation

A

The budling of data within one unit like a class. Often used to hide an object’s internal representation or state from the outside. Includes getters, setters, and access modifiers. Encapsulation of a class allows the promise of existance of an attribute, but leaves you free to implement it how you wish

43
Q

State Consistency

A

Relationships between object features are required to garuntee a consistent state which can be done using invariants

44
Q

Invariants

A

Conditions that should stay true throughout the lifetime of the object. Constructors usually set an initial state, but the object structure should garuntee that invartiants are staying true

45
Q

Getters and Setters

A

The name given to methods that are used to access (getters) and mutate (setters) attributes instead of using direct access

46
Q

Use of Getters and Setters

A

Often, getter is simply passing the underlying attribute, and the setter is ensuring that all the required invariants hold true

47
Q

Access Modifiers

A

Classes, methods, and attributes can be given access modifiers to change their visibility sometimes calling for getters and setters to access them as they can’t be accessed directly (e.g. private)

48
Q

Keyword: protected

A

Access modifier that allows the attribute, method, or constructor visible to the class, package and subclass which is outside the package, but not the world

49
Q

Interface

A

Provides a representation, an independent contract, which all concrete implementations have to realise and implement concrete representations of all demanded methods. Can also extend classes

50
Q

Inner classes/interfaces

A

Are defined within another class (The outer class). Often local helper classes. Can be included as part of a larger expression such as a method call

51
Q

Anonymous class

A

A type of inner class, but are defined in a single expression using new where the definition itself is actually an expression. Often once use classes without an explicit handle to the code that defines it

52
Q

Interator pattern

A

There so that we can use them for interating over object structures, independant of the structure. To be iterated over using : the class must implement Iterable, to get hold of an iterator

53
Q

Label and explain the parts of the Iterator pattern

A

Abstract interface Iterable : Shipped with Java, has the iterator as a method that requires implementing, class must implement this to get hold of the iterator
Abstract interface Iterator: Also shipped with Java, gives the methods to be implemented so that an iterator knows how to step through all elements
Collection: Implements iterable and hence implements iterator via the Collection iterator
Collection iterator: Implements the iterator interface that is called for in the collection. Requires next() and hasNext() functions to be implemented.

54
Q

Java collections

A

A collection is a container that groups multiple elements into a single unit. The Java Collection Framework is one of the most important libraries. It uses generics to be flexible for types and is polymorphically structured

55
Q

Observer pattern

A

When the state of something changes, and other objects need to be notified about it. This is done using an observer object. There are two participants, being the subject who is observed that keeps a list of subscribers and updates them, and the observers that can register with a subject and be updated.

56
Q

Observer pattern within java

A

Abstract Observable: Shipped with Java, thing that is being watched (aka subject) Already complete in terms of functionality with required functions such as notify observers that we can use
Interface Observer: Shipped with Java, only requires the one update function to be implemented which runs when notify observers runs
Concrete Observable: Extends observable, don’t need to add anything other than implementing our state.
Concrete Observer: Implements the Observer interface, which demands update to be complete
Smart adapter: Allows for inheritance of a parent class and holds a delegate attribute that acts between the concrete observable and the observable. The adapter is a child to observable and a channel between the concrete observable and observable

57
Q

Factory method

A

Returns a new instance of a particular class, so that if the class needs changing you don’t have to go through every instance that uses new, just change the object returned from the factory method. The method could be abstract so the implementation could be deferred to subclasses to implement.

58
Q

Factory method parts

A

Abstract product: A specification of what is required of a product that we actually create
Concrete product: Extends product, we might want to swap this later on when programming e.g. Robot to Phone. Associated to a concrete creator
Concrete creator: Extends creator and implements the factory method, which returns the chosen product. Depends on the product as needs to return one. By adding a new one, we can swap the product
Abstract Creator: Has to create and utilise products, creation has an abstract factory method, so the child class must override this and return their own choice of product. Also an operation methods, indicating that there are other methods that can use the created products from the class. Operates on product, not the concrete product

59
Q

Bridge design pattern

A

When you have an orthogonal object hierarchy and you want to disentangle the two or more concerns. Decouples an abstraction from an implementations so that the two can vary independently so implementers can be added easily and used on the abstractions side without changing their code.

60
Q

Bridge design pattern parts

A

Abstract Abstraction: The general top of the hierarchy that provides the aggregations that are references to the objects that stand in for implementations. This is where the bridge is between this and Abstract Implementer
Refined Abstraction: Utilises the actions from the implementation side. Delegates to the chosen implementer
Abstract Implementer: The interface of what is to be implemented, which specifies how to access the concrete implementer actions and turn the concrete implementer into method calls to functionality
Concrete Implementer: Specific implementations of the implementer operation. (Implements Implementer)
Client: Instantiates the abstraction, by providing which implementations should be used, so concerns are localised to each hierarchy and class

61
Q

Mediator design pattern

A

For when all objects need to communicate with each other, so a middle man is introduced. All communication is routed through the mediator and every object has a reference to this