Download presentation
Presentation is loading. Please wait.
Published byAlexander Gallagher Modified over 9 years ago
1
Classes
2
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.
3
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.
4
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]; }
5
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
6
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,
7
Accessibility of members - Introducing Access Control Java’s access specifiers are:- – public – private – protected – default level (i.e. when none of the is used)
8
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
9
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.
10
Static vs. non-static members
11
Table 1.1. Terminology for Class Members Instance 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 MethodA method which belongs to the class and not to any object of the class. Also called class method.
12
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()
13
More on methods Method parameters:- actual parameters & formal parameters. Explicit & implicit parameters. b1.method(a,b) class B { ….. public method(int x,int y) { …. }
14
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
15
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()
16
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.
17
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.