Sun. Jan 22nd, 2023

Object Oriented Programming – Features

This section contains a lot of new vocabulary specific to OOP. Where it would help, there are Visual Studio Projects linked in that illustrate how the code works/looks, along with task sheets for exercises.

Inheritance

There are many different types of animal. However, all of them share some features, even if they also have many others that they don’t share.

This type of relationship is a core feature in OOP. New classes can be defined that inherit from an existing class: by doing so, the new classes automatically start with the parent class’s attributes and methods. The new child can then add extra attributes or methods as required, and in some cases, can also alter the way in which some of those methods work.

In an object diagram, inheritance is shown using a diagram such as this:

Here, we have a diagram telling us that there is an Animal class, which defines two attributes (age and gender), and two methods (isMammal and mate). Note the three items below all have a line with an arrow pointing towards Animal: this signifies that all three of these classes inherit from Animal. That is, Duck’s attributes are: age, gender and beakColor. Duck’s methods are isMammal, mate, swim and quack – i.e. everything from the parent, plus the specifics for Duck.

Encapsulation

Encapsulation refers to the process of bundling both the data and the methods that apply to an object into one unit. Think of a medical capsule: the capsule holds together the contents – it encapsulates it.

A very useful feature in OO programming is the ability to create multiple instances of a single class, and each of those instances will have its own attributes. Each instance then has a copy of its own methods that manipulate that data: think of a person. Each person has two legs (attribute) and understands a method called ‘walk’ – although the method works the same way for each person, it clearly works only using a given person’s legs.

Polymorphism and overloading

Poly (many) morph (forms) allows you to create multiple versions of the same method: for example, a parent class could have a method called ‘walk’ which is deemed appropriate for most instances. However, you may inherit that class to create a new class – for example ‘toddler’. A toddler doesn’t walk the same way as an adult, and therefore you may use polymorphism to re-define the ‘walk’ method, with a toddler-specific version.

The method is still called ‘walk’, but now it does something different.

Overloading is the process of being able to define the same method (or function) multiple times, with each variant having a different signature (that means a different set of parameters: for example, an integer, or a string, or both). Console.ReadLine is a good example of this – it accepts integers, strings and much more. This is achieved by providing multiple versions of the function, with each having different parameters. It is perfectly allowable to have multiple methods with the same name and different parameters, as the compiler or interpreter is able to work out which version to run based on the parameters.

Data hiding

Each object has its own methods to manipulate data. If the attributes are left public, it means that code can manipulate the values directly, from outside of the class.

This means that you lose out on any in-built validation that the class may provide.

Instead, data attributes are left as private meaning they can only be directly accessed within the class, and accessor methods are provided to alter them or access them from elsewhere. This will guarantee that data is always validated (if appropriate).

Reusability

As each class can be instantiated to create multiple instances, it is inherently reusable.

Additionally, the process of overriding classes allows for code to be reused when defining child classes.

As reusability increases, and typically development time reduces, maintainability increases.