- •Lecture 1 Programs and algorithms
- •1.1. How a computer operates?
- •1.2. Algorithms and programming languages
- •If (condition) then ... Or if (condition) then ... Else ...
- •1.3. Summary
- •1.4. Exercises
- •Lecture 2 Fundamentals of programming I
- •2.1. The language
- •2.2. Quick start: first program and basics of syntax
- •2.3. Data in a program (variables and literals)
- •2.4. Operations on data (operators and expressions)
- •2.5. Summary
- •2.6. Exercises
- •Lecture 3 Fundamentals of programming II
- •3.1. Making decisions
- •3.2. Iterations
- •3.3. Arrays
- •3.4. Functions
- •3.5. Summary
- •3.6. Exercises
- •Lecture 4 Java and object-oriented methodology
- •4.1. What is Java
- •4.1.1 Java as a universal programming language
- •4.1.2. Java as a cross-platform language
- •4.1.3. Java as a universal environment for gui programming
- •4.1.4. Java as a universal environment for accessing data bases
- •4.1.5. Java as a universal multimedia programming environment
- •4.1.6. Java as a universal means for client-server programming
- •4.1.7. Java in a distributed environment
- •4.1.8. Java as an environment for building applications from ready-to-use components.
- •4.1.9. Java as the environment for xml processing
- •4.1.10. Micro Java
- •4.1.11. Why is Java worth learning?
- •4.2. Introduction to objects
- •4.4. The first program
- •5.2. Literals
- •5.3. Types of variables. Declarations.
- •Type_name variable_name;
- •Identifiers
- •Naming conventions:
- •5.4. More on operators and expressions
- •5.5. Numeric promotions
- •5.6. Summary
- •5.7. Exercises
- •Lecture 6 Objects
- •6.1. Objects and references
- •6.2. The class String
- •6.3. Useful examples
- •6.4. Summary
- •7.2. Defining attributes of objects
- •7.3. Defining operations on objects (methods)
- •7.4 Defining methods of object creation (constructors)
- •7.5. Example
- •7.6. Inheritance
- •7.7. Summary
- •7.8. Exercises
- •Lecture 8 Classes II
- •8.1. Accessing class members. The variable this.
- •8.2. Static members
- •AClassName.AFieldName
- •8.3. Explicit initialization
- •8.4. Packages and imports
- •8.5. Scope of an identifier. Local variables. Access control.
- •8.6. Structure of a program. Running an application.
- •8.7. Summary
- •8.8. Exercises
- •Lecture 9 Decisions
- •9.1. A brief survey of control statements.
- •9.2. Comparison operators and expressions
- •9.3. Logical operators and expressions
- •9.4. Making decisions: the if and if-else statements.
- •9.5. Multivariant selections done with the switch statement.
- •9.6. The conditional operator ?:
- •9.7. Summary
- •9.8. Exercises
4.2. Introduction to objects
Object-oriented languages are based on the concepts of an object and a class. Precise definitions of these terms will be presented later. For now, instead of concentrating on abstract formulas, let us activate our imagination remembering that some notions will be fully explained further in the course.
So what is an "object"? Intuitively we feel that it is a kind of thing which may be isolated, named and characterized by its properties. For instance we may consider the following objects: a bicycle, a car, a dog or a man.
Each of these objects has different properties. A man has a name and age. A car has a colour and may be characterized by the power of engine or the number of doors. Two different cars have the same set of properties (attributes): a make, a colour or the power of engine. Although makes, colours and engine powers may differ, the cars are similar in a sense (as they are characterized by the same set of properties). We say that the cars are objects of the same class. A class is a description of invariable properties of a group of similar entities.
Furthermore, note that an object can perform some operations. So other objects can make requests to that object, asking it to perform some operation on itself. For instance a driver-object may ask a car-object to pull out or to stop.
We say that a message is sent to an object requesting it to perform some operation.
Objects cannot accept arbitrary messages: a car-object can start or stop but it cannot sing. The set of messages understood (accepted) by an object is its characteristic too.
Thus a class does not only describe the common properties of a group of similar objects such as colour, age or weight but also the set of messages accepted (understood) by the objects of this class.
The above reasoning is an abstraction of the real world.
Having such facilities incorporated into a programming language, we could easily translate real-world problems into programs. This is realized in object-oriented languages. It is a very important characteristic which simplifies programming.
Let us consider a class of electrical devices with the following properties: width, height, state (on-off) and satisfying the requests of switching on and off. We try to model this situation in a programming language. The messages accepted by an object of our class are implemented as methods (a method is a kind of function).
class ElDev {
width, height; <-- attributes: width, height, state
isOn;
================= messages
method on()
isOn = true; <--- the request on (switch the device on)
method off()
isOn = false; <--- the request off (switch the device off)
}
Let us assume we have two objects of the above class - electrical devices - denoted by a and b. We can simulate a sequence of actions: switch on the device a, switch on the device b, switch off the device a, switch off the device b with the help of messages sent to those objects (in other words: by invoking the methods for objects). For example, using the Java (or C++) syntax we could note the above sequence of requests as follows:
a.on(); // message sent to the object a requesting it to switch on
b.on(); // let b switches on
a.off(); // let a switches off
Besides reflecting the problem in the programming language, the object-oriented approach has one more advantage.
Usually object's attributes are not directly available. In a program objects talk to each other with the help of messages which manipulate their attributes.
The object's data (attributes) should be hidden and should be accessed only by means of messages sent to the object.
Such an approach is called encapsulation and allows for making more reliable programs.
Object-oriented languages allow for reusing existing classes by creating new ones. It simplifies programming and protects against mistakes. It reflects real-world situations too.
For example, a computer is an electric appliance. So it is an object of the class ElDev. But computers have their specific characteristics besides those mentioned in the definition of the class ElDev (width, height, state). Thus they constitute a subclass of the class representing electrical devices. In a programming language we reflect it by means of inheritance. A class can inherit attributes and methods from some other class adding some more specialized ones. As an example let us create a class Computer which inherits from the class ElDev. We do not have to write down all the properties and messages of an electrical device (we do not have to implement methods on() and off()). But we may add properties that are characteristic to a computer - for instance the request of executing some program.
Programs are objects of the class Program. A message sent to a computer requesting execution of a program should pass the program as an argument. The inheritance is expressed using the keyword extends. So our new class Computer which inherits from the ElDev and adds a new method run can be written down thus:
class Computer extends ElDev {
method run(Program p)
if (isOn()) execution_of_the_program_p;
}
It may be used in a program as follows:
Computer a, b, c;
Program x, y, z;
...
a.on(); a.run(x); ... a.off();
