Attribute
When describing objects it is common to make reference to attributes. Attributes describe an object. Attributes are sometimes called fields or instance variables.
State
The term 'state' describes the value of an attribute/instance variable at any given time. The following example shows different attributes for an object, in this case a train. At any point in time the attributes will have a state. The state of an attribute is simply its current value.
Attribute/Instance variable
|
State
|
Manufacturer
|
Mordor
|
Power source
|
Coal
|
Maximum speed
|
80mph
|
Capacity
|
300
|
Carriages
|
6
|
Operation/Method
If a change is necessary we can alter an attribute’s state. The process of making a change to a state is called an operation. The term 'Method' is sometimes used instead of operation. In this example an operation could be to add another carriage to the train. This would change the state of the 'Carriages' attribute to 7.
In computing an example of an object could be a line drawn in a vector graphics package.
If an operation was carried out to change the line colour to black then the state of the line colour attribute would change from blue to black.
The term class is used to describe a group of objects that will have some common characteristics. In the example of 2-D vector graphics there would be a class called 'Shape'. Each type of shape within the class is known as a sub class.
In this example 'Shape' is a super class, meaning that there are a large number of different sub classes that could be thought of as shapes. As well as super classes and sub classes, there are also instances shown. An instance is a single member of a sub class.
In the example shown, the triangle subclass has two instances, equilateral and isosceles.
Operations/Methods are defined as part of a class and are then available to every object created within a class. There may be an operation that is created to allow the colour of a shape to be altered. In this case, the operation could be defined as part of the shape class and would then be available to each sub class and instance.
Inheritance
Inheritance is an excellent feature of object-oriented programming languages. Inheritance means that a subclass will inherit the characteristics of the superclass, similarly an instance will inherit the characteristics of a sub class.
In essence this means that we could define attributes and operations for the super class shape. Each time a sub class is created it will automatically inherit the attributes and operations of shape. The programmer only needs to add attributes and operations that make that subclass unique.
If line thickness were regarded as an attribute of a shape then each time a new sub class was created it would automatically contain an attribute for line thickness. This makes it much easier to make changes to objects as a change to the characteristics of a superclass will be reflected in the same change being made in each subclass and instance.
Inheritance allows for the re-use of the same code when creating a subclass or instance, such code can be contained within a class library, further reducing implementation time.
Q14: Match the words to the correct nodes to complete the inheritance diagram:
Share with your friends: |