This is an old revision of the document!
An object is an abstract entity in computer science. It represents an abstraction of a complex concept and thus allows programmers to deal with a large amount of interlinked data without having to deal with all of its detail at once. A real life example is a car engine. You don't need to be able to know exactly how an engine works to be able to operate it, you only need to know how its abstraction - the control elements in the cockpit - works. This also allows you to deal with many different types of engines because although they work differently but their abstractions are largely similar.
An class is the coded description of an object. It provides methods for the object to allow it to carry out actions, and describes its properties. An instantiation of an object is the physical storage space reserved for an object where its properties and current state can be stored. There can be only one class describing an object, but many instantiations of an object can exist within a program.
If many things are interdependent in a program, changing one part can be very difficult as all dependencies must be taken into account when making changes. This increases difficulty of maintenance. Therefore, dependencies should be kept to a minimum or standard calling procedures should be implemented.
Scenario: a school students&staff database has to be developed with personal details of every people in the school.
Because students and teachers both have common properties, a master class is useful from which both groups will inherit some of their properties.
Object people: This object stores the general details of the people in the school, like gender, age, phone number and home address.
Object students: This objects inherits all the properties of the people class and adds some more properties specific to students, like grade, marks for subjects and days missed.
Object teachers: This object inherits all the properties of the people class and addso some more properties specific to teachers, like homeroom, days sick, salary and subjects teached.
In this scenario, only objects of teachers and students would be instantiated, but because they inherit from the people class, they will possess properties of this class. people is now a superclass while teachers and students are a subclass.
Data is stored as a combination of binary values in the computer. Data types are used to store different kinds of data, like text, numbers, floats or other values. They are needed because they specify to the computer how to interpret the binary values in the storage.
In many programming languages, data can be passed to methods to calculate with. This is done by giving them to the method while calling it as parameters. In Java and C# parameters are given in brackets after the calling name of a method:
int s = addInt(3, 2); //<- The values to work with are given as parameters public int addInt(int parameter1, int parameter2) { int result = parameter1 + parameter2; //<- using the values passed as parameters return result; //<- return the result of the operation to the calling procedure }
Encapsulation is enclosing the properties and methods of an object so that they can easily be dealt with and are secured against invalid changes. This usually means making their object variables inaccessible from outside their class and using methods with security check mechanisms to change those variables.
Inheritance is when an object inherits or adapts the properties and methods of an other object and uses them as if they were its own. An object of the other class does not need to be instantiated for this.
Polymorphism is the method of defining multiple methods with same names but with different parameters in order to deal with different parameter configurations. When such a method is called the program automatically selects the method whose parameter configuration matches the parameters given by the calling method.
public class people { private int age; //<- private operator allows access to variable only from within the class public void setAge(int x) { if (age < 1) { throw exception; //<- throw exception for an invalid value - negative or 0 is not a valid age } else { age = x; } } }
It is possible that many objects share a set of common properties and functions that they have inherited from a common object. The advantage is that it is easy to add functionality to the objects. If for example, in the school database both the teachers and students would need a property for storing an email address, this could be implemented in the people class. This way, the properties and methods would have to be implemented only once, which would make development easier and less prone to errors.
A method can accept various sets of parameters. For example, the method in the people class that is used to set a phone number could be implemented in a way to accept a string as a phone number and in a way to accept an integer as phone number. This way, phone numbers could be entered using both ways.
Sorting and other complex algorithms and processes do not have to be re-invented.
Compared to working alone, programming teams can have many advantages:
int a = 3; int b = 2; if (a < b) //selection statement { output("b is larger"); } else { output("a is larger"); }