Difference between revisions of "Class Definitions"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Static, Abstract, Virtual)
(Private data values)
Line 21: Line 21:
 
  Class NameOfClass = ParentClass
 
  Class NameOfClass = ParentClass
  
==Private data values==
+
==Private DataItems==
 
It is normal to make all of the data items within a class private. This means they can't be accessed outside of the class itself, this could prevent any rogue updates of the data values. For each data item, the name and datatype should be specified:
 
It is normal to make all of the data items within a class private. This means they can't be accessed outside of the class itself, this could prevent any rogue updates of the data values. For each data item, the name and datatype should be specified:
  

Revision as of 14:35, 2 January 2017

Declaration

A class should be declared, for any exam question you don't need to use a specific syntax or language:

NameOfClass = Class

or

Class NameOfClass

Sub Classes

If your class is a subclass then you need to specify which class it is based upon:

NameOfClass = Class (ParentClass)

or

NameOfClass = Class of ParentClass

or

Class NameOfClass = ParentClass

Private DataItems

It is normal to make all of the data items within a class private. This means they can't be accessed outside of the class itself, this could prevent any rogue updates of the data values. For each data item, the name and datatype should be specified:

NameOfClass = Class
   Private int DataItem1
   Private String DataItem2

Public Methods to Access DataItems

In order to access the data items, public methods should be created. This way you have more control over your data items and can provide a very specific to access or change your data:

NameOfClass = Class
   Private int DataItem1
   Public int GetDataItem1()
   { 
      Return DataItem1
   }
   Public int SetDataItem1(int Value)
   {
      DataItem1 = Value
   }

Static, Abstract, Virtual

Static

The "static" modifier means that a class cannot be instantiated, and the subroutines must instead be called directly.

if you had a class of human and created an instance of the human called wayne, the static modifier doesn't allow access such as wayne.example() , or wayne.example = 5, or Console.WriteLine(wayne.example). They are not available in an instance of a class.

Abstract

The "abstract" modifier when declaring a class means that the class contains one or more abstract subroutine, and must have one or more subclasses for implementations of abstract methods. A subclass of an abstract class that is instantiated must implement any abstract subroutines contained in the abstract class.

Virtual

A virtual method is one which is inheritable and can be overridden.