Download Classes and Objects operationalised in Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Classes and Objects: Recap
 A typical Java program creates many objects which
interact with one another by sending messages.
Through the objects interactions, a Java program
can implement a GUI or send and receive
information over a network
 An object incorporates some data (variables) and
actions (methods) associated with that data.
 Classes are blue prints for objects
 Classes specify the data (variables) and actions
(methods) possessed both by themselves and by
objects built from them
Classes
 Objects fall into different categories or classes e.g.
you are a student and belong to the class student
and so is the person next to you; both of you are
students
 Objects created from the same class have the same
range of behaviours (methods) and the same
attributes (age, grades, subjects (variables) ) but
probably with some different values
Classes
 The class body contains all of the code that
provides for the lifecycle of the objects created
from it
 Constructors for initialising new objects
 Declarations for the variables that provide the state
of objects
 Methods to implement the behaviour of the class
and its objects
 Every class should be saved in its own .java file
and saved with the name of the class. All classes
in the one program should be saved in the same
directory
Object Instantiation
 There are TWO parts to instantiating an object
from a class
 The first procedure is a variable of the desired
object has to be declared in the program
 Student someStudent; (object variableName;)
 This notifies the compiler that you will use
someStudent to refer to data whose type is
student
 Declarations do not create new objects. The
code Student someStudent does not create a
new Student object; it just declares a variable,
named someStudent that will be used to refer
to a Student object.
Object Instantiation
 The Second set up procedure sets aside memory for all
the information about this student object
 The new operator instantiates a class by allocating
memory for a new object. The new operator requires a
single argument after the new keyword: a call to a
constructor.
 someStudent = new Student ();
 The new operator returns a reference to the object it
created. The someStudent variable is called a reference
to an instance of the student object
 It is common to combine the two statement as follows:
 Student someStudent = new Student();
Constructors
 A constructor is a method that creates an object. In
java constructors are instance methods with the
same name as their class. Constructors are invoked
using the new keyword
 A constructor is a special method which is executed
only when an object is created
 The purpose of a constructor is to setup (or
construct) the environment for the object (variable
values, etc) and to initialise an objects data when it
is created
 A constructor method is easily recognised as it
must:
 Have the same name as the class
Defining Constructor – Car Example
Public class car {
Private String Model;
…
Public Car ()
…
{
Public Car (String make)
{
model = make;
…
}
}
Car car1 = new Car();
Car car 2 = new car ("Audi");
Instance Variables
car car1 = new car();
car1
model: null
speed:0
currentGear:0
Instance Variables
 Instance Variables are declared like local variables
public class car{
String model;
int speed;
int currentGear:
}
……but may be initialised to default values
Constructors: Summary
 A constructor is generally the first method in a
class - it has the exact same name as the class (but
has parentheses and optional arguments)
 The arguments in a constructor are typically used
to initialise the instance variables of the class.
 Java has a default constructor if none is provided
which does not initialisation - this constructor is
called the no args constructor.