Attributes and methods
The class as a mould and the object as an instance: attributes, methods, creation with new, dot notation, this and the state each object keeps for itself, in Java.
- 26 cards
- 4 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Understand
A few cards from the deck
Class: what is it?
Show the answer
A template that describes the attributes and methods shared by every item of the same kind.
class Livre { … }creates no book: it only says what a book holds and can do.What do
c1.soldeandc2.soldehold at the end of this program?
```java
Compte c1 = new Compte();
Compte c2 = new Compte();
c1.deposer(50);
c2.deposer(20);
c1.retirer(10);`40and2030and3040and1060and20
Show the answer
40and20Each object has its own
solde, starting at 0:c1receives 50 then loses 10 (40),c2only receives 20. The classComptedeclaresint solde;and methods that dosolde += morsolde -= m.A … describes what objects have in common; each object created by … is an … of it.
Show the answer
A class describes what objects have in common; each object created by new is an instance of it.
The class exists once in the program; its instances can be as numerous as needed.
Object: what is it?
Show the answer
An entity in memory built from a class, which holds its own attribute values.
Two books built from the same class are two separate entities: changing one title leaves the other untouched.
Why can two objects of the same class hold different attribute values?
- Each
newreserves a separate copy of the attributes - The compiler duplicates the class at each
new - The values are stored in the methods, one per call
- The attributes are recomputed at each read
Show the answer
Each
newreserves a separate copy of the attributesThe class holds only the description; the state lives in the objects. Methods are written once and shared by every object.
- Each