Classes Flashcards

1
Q

Rekord - vlastnosti (3), co ma automaticky (5)

A

Vlastnosti: immutable, final (trida i fieldy), muze se anotovat (trida i fieldy)
konstruktor se vsemi parametry
accessory (gettery)
equals
hashCode
toString

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

Jak se v rekordu jmenuje getter pro promennou x

A

x()

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

Co je lokalni rekord

A

Rekord definovany uvnitr metody
(napr. pro ntici kvuli streamu)

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

Zapis rekordu s nazvem Point a fieldy x, y
Jeho instanciovani
Ziskani x

A

public record Point (int x, int y) {}
Point point = new Point(1, 2);
int x = point.x();

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

Zapis kanonicky konstruktor rekordu Point s fieldy x a y, obsahujici business logiku

A

public Point(int x, int y) {
// logika
this.x = x;
this.y = y;
}

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

Zapis kanonickeho kompaktniho konstruktoru rekordu Point s fieldy x a y, obsahujici business logiku

A

public Point {
// logika
}
fieldy se na konci priradi automaticky

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

UML diagram - zapis privatniho atributu a public metody

A

-atribut:typ
+metoda():typ

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

UML diagram - zapis dedicnosti a asociace (x obsahuje y)

A

dedicnost: sipka s dutym trojuhelnikem
asociace: kotova sipka s ozacenim poctu na kazde strane (0, 1, *)

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

Activity diagram - zapis eventu a vysledku

A

event: cerny puntik
vysledek: cerny puntik v kolecku

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

Activity diagram - zapis rozhodnuti a aktivity

A

rozhodnuti: piculka
aktivita: kulaty obdelnik

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

Sekvencni diagram - jak vypada

A

Kazdy objekt ma svou svislou lifeline
Obdelnik na care je zivot
Sipky mezi nimi jsou operace
Teckovana sipka je return

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

Co dela keyword new

A

Vytvori novou instanci objektu na heapu

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

Druhy promennych

A

Field - globalni promenna
Lokalni promenna - v bloku, nema modifikator pristupu

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

Type inference

A

Pro lokalni promenne lze pouzit var, pokud lze typ odvodit
Typ je maximalne konkretni, nemeni se

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

Konstanta - zapis + kdy se inicializuje

A

public static final NAME = …;
private final NAME = …;
final mohou byt i lokalni promenne a parametry
a. hned
b. pomoci vsech konstruktoru
c. pomoci inicializatoru

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

Staticky kontext

A

Class memory context
Je sdilen vsemi instancemi
Nelze v nem pouzit this
Lze volat z tridy i z instance

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

Staticky inicializator - syntaxe, kdy se vola
Poradi u dedicnosti

A

static {
staticVariable = “x”;
}
Jen jednou, pri loadu class
Object, Parent, Child

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

Inicializator, kdy se vola
Poradi u dedicnosti

A

{
field = “x”;
}
Pri vytvoreni instance jeste pred konstruktorem
Parent, Child (Object ho nema)

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

Syntax statickeho importu vseho ve tride

A

import static pkg.Class.*;

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

Immutable class (3)
+ priklady (3)

A

Fieldy jsou private, maji jen gettery
Pro zmenu parametru lze vratit repliku
Jsou thread safe

Wrappery, String, LocalDate

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

Pretizeni

A

Stejny nazev a navratovy typ, jine typy / pocet parametru
Nelze mit stejne jmeno a parametry, ale jiny navratovy typ!

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

Varargs + zapis

A

Vzdy posledni
Muzou byt hodnoty nebo jejich pole

public static void main(String… args) {
//values.length
//values[0]
}

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

Defaultni konstruktor

A

Bezparametricky
Vytvori se automaticky, ale jen pokud trida zadny nema
MyClass() { }

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

Vzajemne volani konstruktoru

A

Musi byt na prvnim radku konstruktoru - this(params);
Konstruktory nesmi cyklit

25
Q

Enumeration - priklad syntaxe pro Temperature s hodnotami hot a cold

A

public enum Temperature {
HOT,
COLD; - strednik je nepovinny
}

26
Q

Vlastnosti enumu (3)

A

Konstanty jsou public static final
Mohou mit instance variables / methods (musi se inicializovat konstruktorem)
Nelze vytvorit instanci, prepsat v nem tridu, implementovat v nem ifc

27
Q

Syntax enumu Templerature: hot, cold s instance promennou message

A

public enum Temperature {
HOT(“hot”),
COLD(“cold”);
private String message;
private Temperature(String message) { this.message = message; }
public String getMessage() { return message; }
}

28
Q

Enum metody
- ziskani prvni konstanty v enumu
- vraceni konstanty z hodnoty promenne “message”

A

MyEnum.values()[0]
MyEnum.valueOf(“message”)

29
Q

Jak se predavaji parametry?

A

pres Stack
primitivni typy - hodnotou, puvodni promenna se nemeni, pri zmene se hodnota zkopiruje do nove promenne
objekty - referenci, v metode lze zmenit puvodni objekt

30
Q

Parent kazde tridy + jeho metody (7)

A

Object
toString, equals, hashCode
clone
wait, notify, notifyAll

31
Q

Rozsireni - klicove slovo
Kolik maximalne muze mit trida parentu a childu

A

extends
1 parenta
X childu

32
Q

Subclass - dedicnost (3)
Jak vypadá v paměti

A

fieldy a metody:
- dedi vsechny
- k private nema pristup (ale existuji)
- muze pridavat / prepisovat
HEAP - je tam objekt s kodem childa i vsech jeho rodicu

33
Q

Polymorfismus

A

Reference na child muze byt typu parent, potom vidim jen metody/fieldy parenta
ALE!!!! vzdy se pouzije prepsane telo metody z childa

34
Q

Casting - kdy je a neni nutny
Priklad bezpecneho castovani (2)
Jak dopadne dotaz pro null?

A

Je nutny - z rodice na dite
Neni nutny - z ditete na rodice
if (a instanceof A.class)
{ A b = (A) a; }
if (a instanceof A.class aa)
{ /* A b = aa; */ }

null instanceof Object.class == false

35
Q

hashCode metoda
hashCode metoda pro vice objektu

A

vraci pro stejne objekty (dle equals) stejne cislo
Objects.hash(obj, obj, obj)

36
Q

Klicova slova pro tuto a parenti instanci
Povinnost

A

this, super
Nepovinne, pokud jde videt, o co jde

37
Q

Prepsani metody - zmena access prav

A

Child muze rozsirit access
protected -> public

38
Q

Jediny zpusob, kdy prepisuji metodu a pritom se pouzije kod parenta

A

Zavolat v prepsane metode
super.metoda();
Jinak ji zabiju

39
Q

Konstruktory v subclass

A

Child konstruktory nededi.
Kontruktor musi na zacatku volat konstruktor parenta (super(params)) nebo jiny, ktery ho vola.
Pokud ma parent bezparametricky konstruktor, vola se implicitne (super() se nemusi psat).

40
Q

Typicky priklad equals

A

public boolean equals(Object o) {
if (o == this) return true;
if (!o instanceof MyClass) return false;
MyClass m = (MyClass) o;
// compare fields
}

41
Q

Abstraktni trida - popis
Abstraktni metoda - popis
Syntax

A

Trida: Nelze vytvorit instanci, obsahuje aspon 1 abstraktni metodu (muze obsahovat i normalni metody a fieldy)
Metoda: Nema telo, child musi vsechny prepsat
public abstract class MyClass {
public abstract void method();
}

42
Q

Final class/method

A

Z class nelze dedit
Metodu nelze prepsat

43
Q

Sealed class/ifc - co to je
Podminky pro subclass (3)

A

Rozhoduje, kdo ho smi rozsirit/implementovat
Subclass:
* ve stejnem modulu (pro unnamed modul ve stejnem pkg)
* primo rozsiruje tridu
* obsahuje modifikator pro sealing (final, sealed, non-sealed)

44
Q

Sealed class + jeji subclass: syntax

A

public sealed class MyClass permits MyChild {}
public non-sealed class MyChild extends MyClass {}
…permits je vzdy az na konci…

45
Q

Genericka trida - syntax, instancializovani
Jak se rika “te veci”?
Kde jeste lze pouzit?

A

public class MyClass<T> {
private T field;
public T method() { ... }
}
new MyClass<String>();</String></T>

Typova promenna
V ifc

46
Q

Typova promenna - konvence pismenek

A

T type
K key, V value

47
Q

Co je raw type

A

Nepouziti genericu
Napr. List list misto List<String> list</String>

48
Q

Co je natural order

A

Trida ma metodu compareTo

49
Q

Metoda compareTo pro razeni dle jmena

A

public int compareTo(Clazz other) {
return this.getName().compareTo(other.getName());
}

50
Q

Metoda compareTo pro inty

A

public int compareTo(Integer other) {
return this-other;
}

51
Q

var k = System.out::println
Zkompiluje?

A

Ne, nezna typ, ktery vstupuje do funkce

52
Q

enum MyEnum { HOT, COLD }
Jak ziskam MyEnum.HOT ze Stringu “HOT”?

A

MyEnum.valueOf(“HOT”);

53
Q

Jaky access modifikator muze mit enum konstruktor?

A

private, default (package-protected)

54
Q

Muze rekord extendovat tridu / implementovat ifc?

A

Extendovat nesmi (uz extenduje java.lang.Record), implementovat ano

55
Q

Jake fieldy jsou povoleny na rekordu?
Je povolen instance initializer?

A

staticke: ano
member: pouze private final
neni

56
Q

Mam tridu Record v package pkg.a i v pkg.b.
Ktera se pouzije?
import pkg.a.*;
import pkg.b.Record;

A

Ta z becka (wildcard ma nizsi prioritu)

57
Q

Mam tridu Record v package pkg.a i v pkg.b.
Ktera se pouzije?
import pkg.a.*;
import pkg.b.Record;

A

Ta z becka (wildcard ma nizsi prioritu)

58
Q

A a = (B) b;
uz pri kompilaci vim, ze to nejde pretypovat.
co se stane?

A

kompilace ok, vyjimka se vyhodi za behu