Object-oriented programming
Object-oriented programming builds a program from objects that bundle data with the code that acts on it. The vocabulary is where most marks are, so learn each term with a short example.
Part 1 of 3: Learn it
In short
- A class is a blueprint; an object is one instance of it, made by the constructor.
- Encapsulation keeps attributes private and gives access through methods.
- Inheritance lets a subclass reuse a parent class; polymorphism lets a subclass override a method.
Where this is in your specification
Spec points: AQA 7517 4.1.2.3, OCR H446 1.2.4
Classes and objects
Pet is the class. name is an attribute, getName is a method, and new is the constructor that runs when an object is created. rex is an object: creating it is called instantiation.
Encapsulation
Attributes are usually private, so other code can only change them through public methods (getters and setters). A setter can check a value before storing it, which protects the object's data from invalid changes and lets the inside of the class change without breaking other code.
Inheritance and polymorphism
- Inheritance: class Dog inherits Pet. A Dog has everything a Pet has, plus its own extra attributes and methods. It describes an "is a" relationship.
- Overriding: Dog can define its own version of a method that Pet already has, such as describe().
- Polymorphism: code can call describe() on any Pet, and each object runs its own version.
Class diagrams
A class is drawn as a box with three parts: name, attributes, methods. A minus sign marks private members and a plus sign public ones. An arrow with an open triangle points from a subclass to its parent.
What is instantiation?
Show the answer
Creating an object from a class.
Part 2 of 3: See it worked
Worked examples
Example 1
Explain why the name attribute of Pet is private and accessed through getName().
- Private means code outside the class cannot change it directly
- Changes can only happen through methods, which can validate them
- This is encapsulation
Answer: Encapsulation: it protects the data from unintended changes and hides how the class works inside.
Example 2
Cat and Dog both inherit from Pet, and each overrides speak(). A list holds two Cats and a Dog. What happens when a loop calls speak() on each item?
- Each object belongs to its own subclass
- At run time, the subclass's own speak() is used
- So the cats run Cat's version and the dog runs Dog's
Answer: Each object uses its own version of speak(): this is polymorphism.
Common mistakes
- Using class and object as if they mean the same thing.
- Saying encapsulation just means putting code in a class, without mentioning access to data.
- Using inheritance for "has a" relationships.
- Confusing overriding (same method name in a subclass) with overloading.
What does a constructor do?
Show the answer
It runs when an object is created and sets up its initial attribute values.
Part 3 of 3: Test yourself
Check yourself
Answer each one in your head or on paper first, then open it to check.
What is instantiation?
Creating an object from a class.
What does a constructor do?
It runs when an object is created and sets up its initial attribute values.
Give one advantage of inheritance.
Code in the parent class is reused, so it is written and tested once.
Jobs that use this
- Software developer (se abre en otra pestaña)
- Computer games developer (se abre en otra pestaña)
- Web developer (se abre en otra pestaña)
Each link opens the job profile on the National Careers Service (England). In the rest of the UK: My World of Work (Scotland), Careers Wales, nidirect careers (Northern Ireland).
Estos apuntes están en inglés porque siguen los programas de examen del Reino Unido.
Full lessons and marked practice for this course are coming soon to Brainlag Learn. See courses