Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,

Similar presentations


Presentation on theme: "Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,"— Presentation transcript:

1 Classes and Objects  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

2 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

3 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

4 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.

5 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();

6 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

7 Instance Variables car car1 = new car(); car1 model: null speed:0 currentGear:0

8 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

9 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");

10 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.


Download ppt "Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,"

Similar presentations


Ads by Google