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

9 Steps to Understanding the Mobile App Development Process

Are you considering getting on the mobile app development market? That can be a wise move. The market for mobile apps is expected to exceed $407 billion by 2026. However, getting in on this industry won't be easy. Building an app for your business can be tricky, particularly if you don’t have … [Read More...]

SCSI Interface Evolution

SCSI-1, the original 1986 standard, is now obsolete. It used asynchronous transfer, where the host and the device, blind to the other's maximum potential, slowly exchanged 8 bits at a time, offering a bandwidth of 3 MBps. … [Read More...]

Top 5 Blog Plugins For WordPress You Need To Integrate For 2020

So you’re ready to start a blog, or you need to revamp your website blog to drive more traffic. Well, the good news is that there are a few tech tools you can employ to do just that. Many of these tech-minded tools come in the form of WordPress plugins. And these plugins can make a big impact for … [Read More...]

Best Laptops for Professional Writers in 2023

If you need a new laptop as a writer, you may find yourself bewildered by all the choices. For one thing, there are so many brands to choose from. … [Read More...]

What You Need to Know About Automation with Chrome

Automation is drastically changing the way we interact with the Internet. One survey found that 76% of businesses use automation to streamline … [Read More...]

9 Steps to Take When Building a Web Scraper in Python

Web scraping is a method for collecting, organizing and analyzing information that is spread over the Internet in a disorganized way. It can … [Read More...]

8 Steps To Streamline Your Business Operations with Virtual Data Rooms

With the increased reliance on networking and cloud computing, businesses generate and store an ever-increasing amount of sensitive information. As a … [Read More...]

Advantages and disadvantages of using Perl in web programming

Web pages are designed using programming codes and a number of technologies as tools to achieve the desired objectives. An example of the above … [Read More...]

Why Cross-Chain Trading Is the Future of Crypto Investing?

The rapid growth and evolution of the cryptocurrency market have opened up exciting opportunities for investors. Within this dynamic landscape, … [Read More...]

Guides

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

Recent Posts

How to Remove Windows Easy Warden

Easiest Removal Option: Remote Repair: Paying an expert to remove this threat is always the easiest option. It's also the most expensive. … [Read More...]

Magneto-optical drives – MO technology

As implied by the name, these drives use a hybrid of magnetic and optical technologies, employing laser to read data on the disk, while … [Read More...]

Mastering the Nuances of Roku’s New Voice Controls

In a market where Google Chromecast, Apple TV and Amazon Fire TV compete, Roku seeks to strengthen its presence by updating its software and … [Read More...]

[footer_backtotop]

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