Objects

Computer Science: OCR A Level, topic 2.4.10

Objects

Objects are made from classes.

Creating an Object

Creating an Object

  • Objects are created from classes.
  • To create an object we use the keyword new and the name of the class:
  • MyClass myObj = new MyClass();
Accessing attributes

Accessing attributes

  • If an attribute of an object is public, then it can be accessed from outside the class.
  • Object attributes are accessed using dot notation:
  • int attr = myObj.attr;
Calling methods

Calling methods

  • If a method of an object is public, then it can be accessed from outside the class.
  • Object methods are accessed using dot notation:
  • myObj.doSomething();

Test yourself on Objects

  • Which keyword is used to create an object?

  • How would the attribute name of the class myClass be accessed?

Practise this topic for free