2.4.5
Object-Oriented Languages
Object-Oriented Languages
Object-Oriented Languages
Object-oriented programming allows us to model real-world objects and manipulate them easily.
![Illustrative background for Object-oriented programming](https://image-v2.cdn.app.senecalearning.com/2020-08/6c83a0af-b6fb-41f5-9cc3-969ceb84db49/car,h_400,q_80,w_640.jpg)
![Illustrative background for Object-oriented programming ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/6c83a0af-b6fb-41f5-9cc3-969ceb84db49/car,h_400,q_80,w_640.jpg)
Object-oriented programming
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 Classes](https://image-v2.cdn.app.senecalearning.com/2020-08/d4629819-2397-4c50-b572-23f272f66ec1/cars,h_400,q_80,w_640.jpg)
![Illustrative background for Classes ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/d4629819-2397-4c50-b572-23f272f66ec1/cars,h_400,q_80,w_640.jpg)
Classes
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 Inheritance](https://image-v2.cdn.app.senecalearning.com/2019-08/3a98750a-d4e8-4a3c-99a2-c68f35be2ff5/family-,h_400,q_80,w_640.png)
![Illustrative background for Inheritance ?? "content](https://image-v2.cdn.app.senecalearning.com/2019-08/3a98750a-d4e8-4a3c-99a2-c68f35be2ff5/family-,h_400,q_80,w_640.png)
Inheritance
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 Example](https://image-v2.cdn.app.senecalearning.com/2020-08/c77c8e05-2ef6-4f3e-ad00-fba86db9d48e/car-park,h_400,q_80,w_640.jpg)
![Illustrative background for Example ?? "content](https://image-v2.cdn.app.senecalearning.com/2020-08/c77c8e05-2ef6-4f3e-ad00-fba86db9d48e/car-park,h_400,q_80,w_640.jpg)
Example
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
Classes
A class is a blueprint for creating objects.
![Illustrative background for What is a class?](https://image-v2.cdn.app.senecalearning.com/2019-04/00cd4dfc-aa20-4f29-8f1b-9b3743d6bc76/ball-14-number-fourteen-,h_400,q_80,w_640.jpg)
![Illustrative background for What is a class? ?? "content](https://image-v2.cdn.app.senecalearning.com/2019-04/00cd4dfc-aa20-4f29-8f1b-9b3743d6bc76/ball-14-number-fourteen-,h_400,q_80,w_640.jpg)
What is a class?
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?](https://image-v2.cdn.app.senecalearning.com/courseImages/biology/OCR Gateway/5.1.5/coding ,h_400,q_80,w_640.jpg)
![Illustrative background for What is inside a class? ?? "content](https://image-v2.cdn.app.senecalearning.com/courseImages/biology/OCR Gateway/5.1.5/coding ,h_400,q_80,w_640.jpg)
What is inside a class?
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 Java](https://image-v2.cdn.app.senecalearning.com/2019-09/6f06dcec-affd-4cb8-a87a-57b471043c84/java-example-class-,h_400,q_80,w_640.png)
![Illustrative background for Classes in Java ?? "content](https://image-v2.cdn.app.senecalearning.com/2019-09/6f06dcec-affd-4cb8-a87a-57b471043c84/java-example-class-,h_400,q_80,w_640.png)
Classes in Java
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.
- These classes can be made public (can be used in any part of the program) using the
- The image above shows a Java class.
Objects
Objects
Objects are made from classes.
![Illustrative background for Creating an Object](https://image-v2.cdn.app.senecalearning.com/2018-08/789909dc-f5d8-4151-97cf-5012e4f86af3/coding-1853305_1920,h_400,q_80,w_640.jpg)
![Illustrative background for Creating an Object ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/789909dc-f5d8-4151-97cf-5012e4f86af3/coding-1853305_1920,h_400,q_80,w_640.jpg)
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();
![Illustrative background for Accessing attributes](https://image-v2.cdn.app.senecalearning.com/2019-08/c79251c5-b6da-43a8-b980-d737f16d7f15/code-1839406_1920,h_400,q_80,w_640.jpg)
![Illustrative background for Accessing attributes ?? "content](https://image-v2.cdn.app.senecalearning.com/2019-08/c79251c5-b6da-43a8-b980-d737f16d7f15/code-1839406_1920,h_400,q_80,w_640.jpg)
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;
![Illustrative background for Calling methods](https://image-v2.cdn.app.senecalearning.com/courseImages/biology/OCR Gateway/5.1.5/coding ,h_400,q_80,w_640.jpg)
![Illustrative background for Calling methods ?? "content](https://image-v2.cdn.app.senecalearning.com/courseImages/biology/OCR Gateway/5.1.5/coding ,h_400,q_80,w_640.jpg)
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();
1Components of a Computer
1.1Structure & Function of the Processor
1.2Types of Processors
2Software & Software Development
2.1Systems Software
2.2Applications Generation
2.3Software Development
3Exchanging Data
3.1Compression, Encryption & Hashing
3.3Networks
4Data Types, Data Structures & Algorithms
4.1Data Types
5Legal, Moral, Cultural & Ethical Issues
5.1Computing Related Legislation
6Elements of Computational Thinking
6.1Thinking Abstractly
6.2Thinking Procedurally
6.3Thinking Logically
7Problem Solving & Programming
7.1Programming Techniques
7.2Programming Construction
Jump to other topics
1Components of a Computer
1.1Structure & Function of the Processor
1.2Types of Processors
2Software & Software Development
2.1Systems Software
2.2Applications Generation
2.3Software Development
3Exchanging Data
3.1Compression, Encryption & Hashing
3.3Networks
4Data Types, Data Structures & Algorithms
4.1Data Types
5Legal, Moral, Cultural & Ethical Issues
5.1Computing Related Legislation
6Elements of Computational Thinking
6.1Thinking Abstractly
6.2Thinking Procedurally
6.3Thinking Logically
7Problem Solving & Programming
7.1Programming Techniques
7.2Programming Construction
![Go student ad image](/en-GB/revision-notes/_next/image?url=%2Fen-GB%2Frevision-notes%2Fimages%2Fgo-student-uk-ad.jpg&w=640&q=100)
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