classes. constructor a constructor is a special method whose purpose is to construct and initialize...

17
Classes

Upload: alexander-gallagher

Post on 20-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

Classes

Page 2: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

Constructor

• A constructor is a special method whose purpose is to construct and initialize objects.

• Constructor name must be the same as the class name.

• Constructors cannot return a value and, hence, cannot specify a return type, not even void, in the constructor header

• Constructors can be overloaded - A class can have more than one constructor.

Page 3: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

Constructor

• Use constructor to construct new instances • new operator is used to create an object. – A

constructor can only be called in conjunction with new operator.

• A constructor can take zero, one, or more parameters.

Page 4: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

Constructor//Constructor

Student(){roll=0;name="A";age=0;batch="S4 BSc.";marks=new int[5];}

Student(String n,int a,int r){roll=r;name=n;age=a;batch="S4 BSc.";marks=new int[5];}

Page 5: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

Implicit and Explicit parameter• Implicit parameter is the object itself – this keyword holds it.

• Parameters within parentheses – explicit parameters

• Calling another constructor– The keyword this has another meaning.

– this can be used to call another constructor of the same class form the first line of a constructor

– e.g public Employee(){

this(“Amrita”); //calls Employee(String name);

}

– The keyword this has another meaning. this can be used to call another constructor

Page 6: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

Accessibility of members - Introducing Access Control

• Encapsulation provides another attribute: access control – restricting access.

• Through this we can control what parts of a program can access the members of a class.

• How a member can be accessed is determined by the access specifier that modifies the declaration.

• Java has a set of access specifers; some are related to inheritance and package,

Page 7: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

Accessibility of members - Introducing Access Control

• Java’s access specifiers are:-– public – private– protected–default level (i.e. when none of the is used)

Page 8: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

Accessibility of members - Introducing Access Control

• public – that member can be accessed by any other code.

• private – that member can only be accessed by other members of its class.

• (why main() is public – its called by the JVM)• no access specifier:- by default the member is

public within its own package, not accessed outside.

• Protected – during inheritance

Page 9: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

Static members

• Certain members should only belong to the class, and not be part of any object created from the class.

• An example of such a situation is when a class wants to keep track of how many objects of the class have been created.

• Maintain a counter for this.

Page 10: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

Static vs. non-static members

Page 11: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

Table 1.1. Terminology for Class MembersInstance Members These are instance variables and instance methods of an object.

They can only be accessed or invoked through an object reference.

Instance Variable A field that is allocated when the class is instantiated, that is, when an object) of the class is created. Also called non-static field.

Instance Method A method that belongs to an instance of the class. Objects of the same class share its implementation.

Static Members These are static variables and static methods of a class. They can be accessed or invoked either by using the class name or through an object reference.

Static Variable A field that is allocated when the class is loaded. It belongs to the class and not to any object of the class. Also called static field and class

variable.

Static Method A method which belongs to the class and not to any object of the class. Also called class method.

Page 12: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

Garbage Collection • After creation of object - What about destroying them?

• In Java, the destruction of objects takes place automatically.

• An object exists in the memory (heap), and it can be accessed only through variables that hold references to the object.

Student std = new Student(“Amrita");

std = null;

• Java uses a procedure called garbage collection to reclaim memory occupied by objects that are no longer accessible to a program. It is the responsibility of the system, not the programmer, to keep track of which objects are "garbage".

• System.gc()

Page 13: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

More on methods• Method parameters:- actual parameters & formal

parameters.• Explicit & implicit parameters.• b1.method(a,b)class B

{ …..

public method(int x,int y) {

…. }

}

Page 14: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

More on methods - 2• Parameter passing:-– Call by value vs call by reference.– When a method is called, the actual parameter

values are computed and copied into formal parameter variables.

• Accessors & Mutators• Static methods. (main() – structured

approach)• Return statement

Page 15: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

Structured approach in Java

• main() method calls another method.• Since main() is static it can only access static

methods.• Declare any static method in the public class

and it can be called from main()

Page 16: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

Initialization Blocks

• Three ways to initialize a data field: -– Setting a value in a constructor– Assigning a value in the declaration– Initialization blocks

• Class declaration can contain arbitrary blocks of code. These blocks are executed whenever an object of the class is constructed.

• The initialization block is executed first and then the body of the constructor is executed.

Page 17: Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the

Member Initialization

• In summary:-– All data fields are initialized to their default value

(0,false,null).– All field initializers and the initialization blocks are

executed, in the order in which they occur in the class declaration

– If the first line of the constructor calls a second constructor, then the body of the second constructor is executed.

– The body of the constructor is executed.