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.

  1. Concepts
  2. Questions
  3. Sort and link
  4. 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.solde and c2.solde hold at the end of this program?

    ```java
    Compte c1 = new Compte();
    Compte c2 = new Compte();
    c1.deposer(50);
    c2.deposer(20);
    c1.retirer(10);
    `

    • 40 and 20
    • 30 and 30
    • 40 and 10
    • 60 and 20
    Show the answer

    40 and 20

    Each object has its own solde, starting at 0: c1 receives 50 then loses 10 (40), c2 only receives 20. The class Compte declares int solde; and methods that do solde += m or solde -= 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 new reserves 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 new reserves a separate copy of the attributes

    The class holds only the description; the state lives in the objects. Methods are written once and shared by every object.