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

Pentium SEC

The Pentium II's Single Edge Contact cartridge technology allows the core and L2 cache to be fully enclosed in a plastic and metal cartridge. These sub-components are surface mounted directly to a substrate inside the cartridge to enable … [Read More...]

Number Systems 4

At first glance all this switching between number systems (decimal, binary and hex) may not seem to be a help at all, but we've seen that only a very little effort can make it perfectly clear, not only in principle but also in practice. To enable you to get more practice, this interactive … [Read More...]

ISA Bus – Industry Standard Architecture

When it appeared on the first PC the 8-bit ISA bus ran at a modest 4.77MHz - the same speed as the processor. It was improved over the years, eventually becoming the Industry Standard Architecture (EISA), developed by a consortium made up of IBM's major competitors. Although these systems … [Read More...]

Revolutionize Your Internet Experience with Orbi 960 – The Ultimate WiFi System

In a world where seamless connectivity is essential, slow and unreliable internet connections are a major problem. Whether you are running a business, … [Read More...]

Do You Need a VPN When Trading Cryptocurrency?

There’s no doubt that the biggest global industries in 2023 are tech-driven, while there remains a significant crossover between many of these … [Read More...]

Goodbye Bitcoin: the 3 alternative cryptocurrencies that have great upside potential, according to experts

Bitcoin has been a very lucrative investment for people that got into it early. One report from The Motley Fool pointed out that $10 of bitcoin … [Read More...]

Self-driving cars face their Achilles’ heel and may be targets of hackers

The market for self-driving cars is booming. Customers spent $22.22 billion on these autonomous vehicles in 2021 and they will likely spend more in … [Read More...]

How to avoid scams with bitcoin and other cryptocurrencies

Cryptocurrencies got a bad reputation when scams multiplied like ants on a piece of cake. Even today many people associate bitcoin and other … [Read More...]

WHAT IS CLOUD COMPUTING AND WHAT ARE ITS MAIN BENEFITS?

Users are Increasingly using cloud computing to store their information, which is replacing local storage. The business digitization process goes … [Read More...]

Guides

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

Recent Posts

MyStart Search Protection Removal

The MyStart Search Protectoin program is another annoying piece of software that is meant to protect other programs it bundles with. This is to make … [Read More...]

Creative Commons Images for Your Web Projects

Do you know which images from the Internet you can reuse and which you can't? Do you know Creative Commons? There is a misconception that all the … [Read More...]

Navegaki.com.br Removal

Navagaki.com.br is a browser hijacker. The .br in the domain name is for domain names that are registered in Brazil. However, that does not mean it … [Read More...]

[footer_backtotop]

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