2.4.5

Object-Oriented Languages

Test yourself

Object-Oriented Languages

Object-oriented programming allows us to model real-world objects and manipulate them easily.

Illustrative background for Object-oriented programmingIllustrative background for Object-oriented programming ?? "content

Object-oriented programming

  • In object-oriented programming, most real-world objects, like cars, can be modelled.
  • Each object is an instance of the class. Objects contain:
    • Attributes: things we store about them e.g. the make and model of the car.
    • Methods: things the object can do e.g. starting the engine or indicating to turn.
Illustrative background for ClassesIllustrative background for Classes ?? "content

Classes

  • A class forms a blueprint that we can use to instantiate (create) an object.
  • A car class could be instantiated for every car ever made.
  • Each object would have the same behaviours, but slightly different values in their attributes.
Illustrative background for InheritanceIllustrative background for Inheritance ?? "content

Inheritance

  • We can define a subclass from what we call the 'super class'.
    • These are sometimes known as child class and parent class.
  • In inheritance, the subclass inherits all of the attributes and methods of the superclass.
  • The subclass can have its own attributes and methods.
Illustrative background for ExampleIllustrative background for Example ?? "content

Example

  • If we had a vehicle super class, we could have a subclass of car that would inherit all of the attributes and methods of the vehicle class.
  • The car subclass could add additional attributes such as colour and methods such as indicate_left that do not apply to the vehicle super class.

Classes

A class is a blueprint for creating objects.

Illustrative background for What is a class?Illustrative background for What is a class? ?? "content

What is a class?

  • A class is a blueprint for creating an object.
  • One class will often represent one type of real-world object.
    • For example: in a snooker game you might have a 'Ball' class.
Illustrative background for What is inside a class?Illustrative background for What is inside a class? ?? "content

What is inside a class?

  • A class contains two main components:
    • Attributes: these are variables which describe the object.
    • Methods: these are subroutines which allow the object to perform actions.
Illustrative background for Classes in JavaIllustrative background for Classes in Java ?? "content

Classes in Java

  • Classes have different levels of visibility.
  • In Java, classes are created using the class keyword.
    • These classes can be made public (can be used in any part of the program) using the public class keywords.
  • The image above shows a Java class.

Objects

Objects are made from classes.

Illustrative background for Creating an ObjectIllustrative background for Creating an Object ?? "content

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();
Illustrative background for Accessing attributesIllustrative background for Accessing attributes ?? "content

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;
Illustrative background for Calling methodsIllustrative background for Calling methods ?? "content

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();

Jump to other topics

1Components of a Computer

2Software & Software Development

3Exchanging Data

4Data Types, Data Structures & Algorithms

5Legal, Moral, Cultural & Ethical Issues

6Elements of Computational Thinking

6.1Thinking Abstractly

6.2Thinking Procedurally

6.3Thinking Logically

7Problem Solving & Programming

8Algorithms

Go student ad image

Unlock your full potential with GoStudent tutoring

  • Affordable 1:1 tutoring from the comfort of your home

  • Tutors are matched to your specific learning needs

  • 30+ school subjects covered

Book a free trial lesson