# Modularity: Inheritance¶

Learning Outcomes

After this week you can explain the key properties of an object. Furthermore you can also explain the basic concepts of inheritance and understand the role of the inheritance hierarchy and the concept of abstract data types in programming.

The concepts of class and object are familiar from the prior courses and programming languages. On Programming 1:llä an introduction to classes and objects was covered. On Programming 2:lla an introduction to inheritance was covered. Next we will deepen the understanding of the role of inheritance and get to know its basics in Java.

## On Classes and Objects¶

In programming things are viewed from two viewpoints: the interface and the implementation. An object can thus be approached both from the user’s and the implementor’s point of view. The public interface of an object tells the user what kind of things the object can do. Object also offer a nifty way to implement the functionality offered by the object.

### An Object belongs into a Class¶

Each object always belongs into some class. The class defines how each object of a specific class behave. A class acts as a module i.e. it acts according to the principles of information hiding. A class also acts as a data type – an abstract type for which operations can be performed.

A class represents each object that is constructed in the same way. Objects are of the type of their class definition. A class defines the basic structure of the state of the class (attributes) and declares the interfaces that define the behaviour of the objects (services and behaviour). The creation of an object – instantiation – occures as determined by the class: an object with a inner structure and initial state determined by the class is created. A class is a concept meant for the design and implementation of a program. During program execution almost without exception objects are in use. It is thus good to think that a class is a way to depict the common characteristics of objects that belong together. In a final program there always is object implementing the functionality defined byt he class in use.

### An Object has a State¶

All objects in a program always have a state that changes during program execution. The state of the objects consists of the attributes of the class that define what information the state of the class needs. The state of the object is typically encapsulated behing a public interface. It can be investigated and changed only through the public interface. The state is specific to each object and independent of other objects.

### An Object has an Identity¶

The program must be able to unambiguously identify and handle each object. In programming languages there is the concept of object identity for this purpose. The identity can be for example a variable name of a memory address where the object is located. Identity is important as it can be checked if two objects are different objects without the current state of the objects affecting the result. Two objects can very well have exactly the same state at some point in program execution but they are still two separate objects.

Class

Class is a data type that defines the required operations and the information needed to implement them. At the same time a class can be seen as a module that implements the operations as a public interface and hides the information and characteristics needed behind it. Each object belongs to a class.

On objects and classes (duration 7:49)

## Keyword static¶

When an object is created in a program a new instance of the class is created. This means that each object has its own copy of the member variables of the class. However, there are situations where each object of the class should have access to a single shared member variable. These variables are called class variables. The need for a class variable arises in situations where something clearly belongs to the responsibility of the class but not solely to any of the objects of the class. Typically these are constants or for example in the case of the date class a list of month lengths. Both in Java and in C++ class variables are defined with the keyword static. For example,

class Object {
public:
Object();
private:
static unsigned int objects_;
};

unsigned int Object::objects_ = 0;
Object::Object() {
//each object increases the shared counter
objects_++;
}

public class Object
{
private static int objects_ = 0;

public Object() { object_++; }
}


Class functions can be similarly defined. Instead of the object, they focus on the entire class and handle the class variables.

## Introduction to Inheritance¶

The viewpoint of object-oriented programming is that of encapsulation: the public behaviour of the objects and their internal implementations. Objects can be grouped based on both. Some objects can have public behaviour that works in a shared manner. Grouping through shared behaviour saves effort: the same functionality needs to be implemented only once. The grouping of classes and managing the relations created by the shared features is called inheritance.

A class defines

The state of the object

A class variable

Posting submission...