OOP - Inheritance

Inheritance is a fundamental element of object-oriented programming. It is based on the notion that a parent can pass on traits to their children.

In OOP, this is implemented as classes having sub-classes that inherit the attributes and behaviours of the base class.

This links strongly with the idea of generalization. As we model the world we group things together by common characteristics and develop hierarchies to show the relationships between the classes

Example:

All cats have retractable claws and rough tongues that strip flesh.

All domestic cats inherit this from the base class Felidae, as do Panthera, except panthera are big and domestic cats small.

Lions inherit all the attributes of Panthera but they also have a mane.

The sub-classes inherit the characteristics of their base class, and can be extended to include additional characteristics (attributes and/or behaviours).

Demo

Create:

  • Bank Account Class

  • Current Account Class

Set Current Account to inherit form Bank Account

Code from Demo

class BankAccount

{

//Define attributes

private string surname; //field

private string firstName;

private double balance;


public string Surname //property

{

get { return this.surname; } //get method

set { this.surname = value; } //set method

}


public string FirstName //property

{

get { return this.firstName; } //get method

set { this.firstName = value; } //set methd

}


public double Balance

{

get { return this.balance; }

}


//Define Constructors

public BankAccount()

{

this.balance = 0;

}


public BankAccount(string surname) //overload constructor

{

this.balance = 0;

this.surname = surname;

}


public BankAccount(string surname, string firstName) //overload constructor

{

this.balance = 0;

this.surname = surname;

this.firstName = firstName;

}



//Define Methods

public string GetSummary()

{

string summary = this.surname + ", " + this.firstName + " - £" + this.balance;

return summary;

}


public bool CreditBalance(double amount)

{

bool validTransaction = false;

if (amount >= 0)

{

validTransaction = true;

this.balance += amount;

}

return validTransaction;

}

}


class CurrentAccount : BankAccount

{

private double overDraftLimit;


public double OverDraftLimit { get { return this.overDraftLimit; } }


public void SetOverDraftLimit(double limit)

{

this.overDraftLimit = limit;

}

}


class MainClass

{

public static void Main(string[] args)

{

string theSurname;

string theFirstName;


Console.WriteLine("Bank Account Demo 4\n");


//The UI - ask for the surname

Console.Write("Enter surname: ");

theSurname = Console.ReadLine();

Console.Write("Enter first name: ");

theFirstName = Console.ReadLine();


//instantiate an object:

CurrentAccount myAccount = new CurrentAccount(); //call the constructor


//Assign Data to object

myAccount.Surname = theSurname;

myAccount.FirstName = theFirstName;


//Get data from object

Console.WriteLine("The name for this account is {0}, {1}", myAccount.Surname, myAccount.FirstName);

Console.WriteLine(myAccount.GetSummary());

}

}

Challenge

Implement the changes made in the demonstration.

Extend the program by adding a class for a savings account.

Keywords

Inheritance

This is the relationship among classes wherein one class shares the data structure and behaviour of another. In other words, a class has the same properties and methods as its parent class, and so it inherits the data fields (properties) and the methods of the parent.