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:
class: An abstract model for objects. It defines the properties and methods of an object. It is like a data type for an object. For example, in Java strings are not fundamental variables like integers but objects.
identifier: A pointer that explicitly identifies an object. Generally, it is the variable name.
primitive: primitive data types are the ones involving the most basic entities in informatics: numbers. Other data types like arrays are abstract because they combine many primitive data types.
instance variable: A variable in a class from which every instantiated object gets its own copy. If a class called people would have an instance variable called age, every object of this calss would have its own variable called age.
parameter variable: A variable that is passed along to a function that is called to perform operations with. In Java it is the argument passed in the brackets with the function caller.
int a = aSimpleFunction(int b); //<- see the parameter variable in the brackets? Magnificent!
local variable: A variable declared inside a function. This variable is only known and accessible by the function it is declared inside.
method: A method is a set of rules defining operations that are executed.
accessor: A method used to return values of a private variable of an object.
mutator: A mutator is a method used to control changes to the private variables of an object. Setter and accessor methods are mutator methods.
constructor: The method called when an object is instantiated.
signature: Defines the inputs and outputs of a method. In some programming languages it even defines the errors thrown by the function.
return value: Specifies the data type that a function returnes to its caller.
private: A constructor that hides variables and methods from access from outside the class.
protected: A member access modifier. A protected is only accessible within its own or derived class instances.
public: Member can be accessed by any method that references its calss.
extends: This keyword is used to state from which class the current class inherits its methods and properties.
static: Declares a static member of a class that will be the same for all members.
In examination questions the primitive types will be limited to int, long, double, char and Boolean.
Primitive data types are most commonly used to store simple values. They are also used as the building blocks of the more complex abstract data types.
Let us design a class. It should be used to model people in a system:
public class People //The begin of the declaration of the class { private int age; //Note the private keyword restrict access to variable only within this class private string name; private int phone; new People() //The constructor method is used to initialise some values when an object is instantiated. { age = 0; name = ""; phone = 0; } public void setAge(int x) //The setter method is used to modify values of variables. { age = x; } public int getAge() //The accessor method (part of the mutator methods) is used to access values of variables. { return age; } public void setPhone(int x) //The next two methods are overloaded, the parameter variables decide the function to be called. { phone = x; } public void setPhone(string x) { phone = Transform(x); } private int Transform(string number) //Private keyword restricts use of method for only within the class. { int numerical = Convert.toInt(number); //local variable return numerical; //Return specifies what value the method returns to its caller. } }
int a = 3; int b = 2; if (a < b) //selection statement { output("b is larger"); } else { output("a is larger"); }
/* This code adds up all the numbers from 0 to 10. Notice that instead of typing 0+1+2+3.....+10 a loop is used to add up the numbers */ int count = 0; int total = 0; while (count <= 10) { total = total + count; count = count + 1; }
/* Following example illustrates the use of static arrays. Sequential search is used to find a number in a given array. */ public void searchNumber(int[] numbers, int desired) { int count = 0; //set up counter for loop while (count < numbers.count) //loop until end of array is reached { if (numbers[count] == desired) //if desired number equals to number at position count, return count { return count; } } }