Learn Java

 

The basics

Java is an object oriented programming language.

Each object is a self contained piece of software and consists of attributes and behaviour.

An attribute is a property or characteristic of an object such as height, position, size.

The behaviour of an object is the actions that the object can respond to, such as move 3 places to the right, or jump.

An attribute will have a value such as '100' or 'blue'.

All the values of an object determine its state.

To get an object to do something it needs to be sent a message.

To be able to send messages to objects we need a way to be able to refer to them, to do this we use variables. A variable is a named block of computer memory into which data can be stored

The messages an object will respond to is called the message protocol and these messages may change the state of an object or get the object to do something without changing its state.

A class is a template and describes the attributes, behaviours and protocol of an object.

When an instance of a class is created, this instance becomes an object belonging to that class.

When an object is first created its attributes are set to specific values (its state). This is called initialisation and the object is said to have been initialised.

If objects of more than one class can respond to the same message, then this message is said to be polymorphic. The objects of these different classes are not however required to behave the same why to this message just respond to it in some way.

The ability of an object to respond to a message can depend on its state, i.e. if the overdraft facility of an bank account object is at its maximum and it is sent a message to increase by "100" it may not complete this request. This object is said to be state-dependent.

A class can have a subclass. This subclass will have all the attributes of the superclass (the superclass is the class that has the subclass). These attributes can be initialised differently to the superclass. The subclass may also have additional attributes to its superclass. The protocol of the subclass includes the protocol of the superclass (it can respond to the same messages) but may also respond to additional messages.

A subclass inherits the attributes and protocol from its superclass

A message is in the form do-something(), i.e right()

A message-send combines the object that the message is being sent to (called the receiver and in this case doggy) and the message. i.e. doggy.right()

An argument is extra information sent with a message and is placed between the parenthesis i.e. doggy.forward(10). There can be more than one argument separated by commas i.e. doggy.forward(patch, 10).

The message name is the message without any arguments i.e right() or forward()

Some messages can set an attribute of an object to a value, such as doggy.setPosition(2). This message would set the position of doggy to 2. Also some messages can get the value of an objects attribute, such as doggy.getPosition(). This message would get the position value of the object doggy. The value returned is called the message answer.

The message that gets the value of an attribute is called a getter message and the message that sets the value of an attribute is called a setter message. Together they form what are called accessor messages.

Previous page Page 1 Next page

 

Page 1 2 3 4 5 6 7