pctechguide.com

  • Home
  • Guides
  • Tutorials
  • Articles
  • Reviews
  • Glossary
  • Contact

Object Oriented Programming 3

Encapsulation

From what we’ve covered so far we can suggest that:

A class is an abstract data type made up of a combination of the relevant data and associated methods.

Each class not only has specific data members, it also has it’s member functions or methods. The methods are the tasks that the class performs, including everything from input/output to calculations to data updates. As a basic rule, if it does it to the class, it does it in the class.

This has considerable benefits. For instance, class data can be kept secure by introducing data interfaces. This is simply the provision of functions which shield the data within the class. In other words, a data member of the class can only be set, or its value retrieved, by using a specific class member function. In this way, the risk of accidentally altering a data value is hugely reduced. This extra security can in itself be a good argument for the use of an object oriented approach, but see below for the use of private and public data and methods for more on class security.

However, encapsulation also means that a class becomes highly reusable. For instance, one of the most commonly used objects in the Windows operating system is of the class window. This is simply a box with height and width values and some screen coordinates, with associated methods. This basic class has been reused countless times, by countless Windows programmers. They never need to develop this class, because it is simply provided. The only thing they need do is add their own functionality to it.

Private and Public

Class security is controlled by specifying whether data and function members are private or public. A private data member, for example, can not be read or changed by any function which is not a member of the class. Similarly, a private function can only be called by another function which is a class member.

Clearly it’s important that some class functions are public in order to allow access to the data. It’s up to the class designer how to approach this, but often the data is exclusively private, whilst most functions (methods) are public except for some internal class management functions which would be dangerous if called externally.

A

For instance, in this example class the data members staffNumber and salary are private members, so two functions have been provided for each value to perform the tasks of getting and setting the data values. This is a very common practice in an OO class, and though it can seem, sometimes, to be a little longwinded, in many cases complex operations are performed to alter a data value, and if the code to do this is included within the class, then the class becomes a transferable module that can be used as part of a class library to fit into other projects wherever it is useful.

The OO Class and the Object

It’s one thing to have a description of something and a list of all its jobs, but it’s another thing to actually have something.

Take people for example. You know what makes people. You are aware that certain variables help you to distinguish people from one another. You know what people do. But, this is all conceptual stuff. These attributes and tasks (think data and methods) are just… stuff waiting to happen. But when you actually have a person, that’s when you have an object. The object is where it all takes shape.

Think of a person versus human. There are 6 billion humans (give or take) on Earth. It could be said that each person is an instance of the class human.

Now this is important: even if there were no humans, you could still have the concept of humans, but it would not have any value. It’s each individual person that counts. Although truly unique, each person still has common traits and performs common tasks that are associated with people. In this way we can recognise even very wide variations from person to person as still being part of the group we call human.

And it’s the same with objects. The class has no real form, its a concept. It’s the objects that have form, and all the objects created of a particular class will have the class’s attributes and methods.

Class Constructors and Destructors

These are special functions which only occur at the birth and death of a class object, and are worth briefly mentioning in their own sake.

Very often a class object requires some default data values to be set at the time of its creation, or even some calculations performed, and the constructor function allows for this to be done. It is not explicitly called, but is called automatically whenever an object of a particular class is created. A simple example of what might happen in a constructor is the creation of a current timestamp. This might be so that the object has a unique identifier, or simply the current date and time is recored.

The destructor, not surprisingly, is called when the object goes out of scope and is, therefore, destroyed. The most common use of a destructor is to explicitly release any dynamically created variables in order to free RAM, an important task to prevent dangerous memory leaks.

So, after this introduction you should be able to answer:

  • What is a class?
  • What is a class method?
  • What is an ADT?
  • What is an object?

Any programming language that supports the class ADT is referred to as an object based language, but object oriented languages support more advanced techniques, which we’ll cover next. We’ll take a look at the object oriented extensions of the class system, namely inheritance and polymorphism, using a simple game as an example.

  • Object Oriented Programming 2
  • Object Oriented Programming 3
  • Object Oriented Programming 4
  • Object Oriented Programming 5
  • Object Oriented Programming 6
  • Object Oriented Programming 7
  • Object Oriented Programming 8

Filed Under: Object Oriented Programming

Latest Articles

Disclaimer

Brain Box Consultants LLC Web Site Agreement The pctechguide.com Web Site (the "Site") is an online information service provided by Brain Box Consultants LLC ("pctechguide.com "), subject to your compliance with the terms and conditions set forth below. PLEASE READ THIS DOCUMENT CAREFULLY … [Read More...]

Optimizing Startup Menu

Video Covering Optimizing Startup Menu - Part of our Fix Slow Computer tutorial found onĀ  https://www.pctechguide.com/how-to-fix-your-slow-computer Transcription of Video: Hi, this is Jacob with pctechguide.com. This is part four in our video series of "How to Speed Up Your Slow Computer" … [Read More...]

Superman Coupon Removal

  SupermanCoupon is a new adware program that is wreaking havoc on the Internet and on people's computers lately. The software should be removed from your system as soon as you notice it because it provides no clear positive purpose to be on your computer. Like many other applications it … [Read More...]

20 Cool Creative Commons Photographs About the Future of AI

AI technology is starting to have a huge impact on our lives. The market value for AI is estimated to have been worth $279.22 billion in 2024 and it … [Read More...]

13 Impressive Stats on the Future of AI

AI technology is starting to become much more important in our everyday lives. Many businesses are using it as well. While he has created a lot of … [Read More...]

Graphic Designers on Reddit Share their Views of AI

There are clearly a lot of positive things about AI. However, it is not a good thing for everyone. One of the things that many people are worried … [Read More...]

Redditors Talk About the Impact of AI on Freelance Writers

AI technology has had a huge impact on our lives. A 2023 survey by Pew Research found that 56% of people use AI at least once a day or once a week. … [Read More...]

11 Most Popular Books on Perl Programming

Perl is not the most popular programming language. It has only one million users, compared to 12 million that use Python. However, it has a lot of … [Read More...]

10 Exceptional Books on ChatGPT that Will Blow Your Mind

ChatGPT is a powerful new AI tool that is taking the world by storm. You are going to find a lot of amazing books that will teach you how to make the … [Read More...]

Guides

  • Computer Communications
  • Mobile Computing
  • PC Components
  • PC Data Storage
  • PC Input-Output
  • PC Multimedia
  • Processors (CPUs)

Recent Posts

Thermal Wax Printers

Thermal wax is another specialist technology - very similar to dye-sublimation - well-suited to printing on transparencies. … [Read More...]

Differences Between Spyware, Malware and Others

There are countless ways that your computer can be infected by harmful elements but the vast majority of them come down to the four usual … [Read More...]

Remo Recover Outlook Express Review

Remo Recover Outlook Express If you are having problems with corrupted files, databases, or emails in your Outlook Express, we definitely recommend … [Read More...]

[footer_backtotop]

Copyright © 2025 About | Privacy | Contact Information | Wrtie For Us | Disclaimer | Copyright License | Authors