Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Inheritance in object oriented programming languages

Inheritance in object oriented programming languages

Abstract

This article will discuss how inheritance is a very useful manner of preventing code copying and pasting. It will also demonstrate how to do it with Visual Basic .NET and Java. Inheritance, in the simplest way possible to describe, is a cloning of code without having to make code messy to replicate an already implemented method. The only thing is, with inheritance you do not need to rewrite the code. All of the methods and fields are inherited from its mother.

Public, private and protected

Better known as public, private and friend in VB.NET, these are the modifiers, or as some call them the "access modifiers". They are designed to show how much of a class is exposed to the outside world. From the lowest tier to the highest; private exposes nothing, users can read about it using documentation and the IDE will know it is there if explicitly typed into the editor, but it will explain how it is private, protected or friend is the next stage and it is exposed to other classes in a package or project, and it is similar to the private statement apart from the fact that it will appear within the package or project whilst public is totally in the open (the signature, parameters, values etc.) will all be accessible and may be modified.

Inheriting a structure or class

Why would you want to inherit a class? The answer is simple. It saves rewriting complex methods and we can even extend this by adding more methods. For instance, take a look at a simple real-life inheritance tree.

An inheritance tree of an anaimal

In this tree, Cat and Rabbit inherit from Mammal, which also inherits from Animal.

With a structure like this, we can implement an application using inheritance. If we take a look at Animal at the top (i.e. the superclass or mother class) we can see that it has 2 methods, namely; "Act" and "Eat". Both of these are then inherited by Mammal. But on top of the default methods of the Animal, Mammal defines several fields, specifically "Warm blooded" and "Hair". As such, this inheritance goes on to Cat and Rabbit, who then both add their own fields and methods.

An animal can be a rabbit, but a rabbit cannot be an animal. This section is very important. Before you start to declare the following:

Rabbit r = new Animal();

you should note that this will not work, as the dynamic type, Rabbit, has more methods and fields than Animal does and therefore a call on those methods will not work. The compiler will not compile if you attempt this anyway. Conversely, this will work:

Animal a = new Rabbit();

To see an example, look at the VB.NET tutorial here and select the article on inheritance.

Posted by jamiebalfour04 in Technology
oops
object
oriented
programming
paradigm
inheritance
Comments
Powered by DASH 2.0
Scan and keep for the latest article or review every time!
https://www.jamiebalfour.scot/redirect/action/latest-article/