classes, objects & methods · is used to define a member that is common to all objects and...
TRANSCRIPT
![Page 1: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/1.jpg)
CLASSES, OBJECTS &
METHODS
![Page 2: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/2.jpg)
TOPICS TO COVER
Introduction.
Defining a class.
Creating Objects.
Accessing Class Members.
![Page 3: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/3.jpg)
INTRODUCTION Underlying structure of each JAVA programs is
CLASSES.
CLASS
DATA ITEMS
FUNCTIONS
OBJECTS basic program
Components
CREATE
METHODS OBJECTS
FIELDS
METHODS
CREATE
![Page 4: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/4.jpg)
DEFINING A CLASS A class is a user-defined data type.
Variables and Functions can be created within class
SYNTAX EXAMPLE
class classname { field declaration; method declaration; }
class area { int side; int length; }
Declaring variables
Instance variables are declared exactly as LOCAL variables
INSATNCE VARIABLES
![Page 5: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/5.jpg)
METHOD DECLARATION Without methods class has NO LIFE.
Since objects created by such class cannot respond to any messages.
Thus, methods are necessary for MANIPULATING DATA.
SYNTAX EXAMPLE
type method-name(parameter list)
{
}
Type of the value the method returns. It can be
void, int, float, double
class area { int side; int length; void get(int s, int l) { side = s; length = l; } }
![Page 6: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/6.jpg)
EXAMPLE FOR CREATING CLASSES
Design a class Account that stores customer name, account number, and type of account. Include necessary methods to achieve following tasks:-
Deposit money.
Display balance.
Permit withdrawal and update balance.
![Page 7: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/7.jpg)
An object in JAVA is essentially a block of memory that contains
space to store all the instance variables.
Creating an object also refers to INSTANTIATING AN OBJECT.
Objects in JAVA are created using new. The new operator dynamically
allocates memory for an object an returns a reference to it.
SYNTAX:-
class area { int side; int length; void get(int s, int l) { side = s; length = l; }
classname objectname;
objectname = new classname();
area a1;
a1 = new area();
area a1 = new area();
combined
Allocates at run-time null
a1
Indicates that it does not point
to any object
a1
![Page 8: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/8.jpg)
Values are to be assigned to variables in order to use them in
our programs.
Since we are outside the class, we cannot access the instance
variables and methods directly.
Object and dot operator are used to do this.
SYNTAX:-
objectname.variablename = value;
objectname.methodname(parameter-list);
area a1 = new area();
a1.side = 10;
a1. get (10, 15);
class area { int side; int length; void get(int s, int l) { side = s; length = l; }
![Page 9: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/9.jpg)
![Page 10: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/10.jpg)
a1.tri();
a1.square();
}
![Page 11: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/11.jpg)
Constructors
Method Overloading
Constructor Overloading
Nesting of methods
![Page 12: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/12.jpg)
JAVA allows objects to initialize themselves when they are
created CONSTRUCTOR
PROPERTIES:-
• Initializes an object immediately upon creation.
• Same name as the class in which it resides and syntactically similar
to a method.
• Is called automatically after the object is created.
• Does not have return type.
EXAMPLE
![Page 13: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/13.jpg)
NO RETURN TYPE
OUTPUT
![Page 14: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/14.jpg)
![Page 15: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/15.jpg)
Methods have same name, but different parameter list .
Is used when objects are required to perform similar tasks but
using different input parameters.
Also known as POLYMORPHISM.
Here, the aim is to provide several method definitions all with
same name, but different parameter lists.
The difference may either in number or type of arguments
Method’s return type does not play any role in this
![Page 16: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/16.jpg)
In addition to overloading methods, you can also
overload constructor method
EXAMPLE
![Page 17: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/17.jpg)
A method of a class can be called only by an object of that class using dot operator.
A method can be called by using only its name by another method of the same class
NESTING OF METHODS
![Page 18: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/18.jpg)
STATIC MEMBERS
Is used to define a member that is common to all objects and accessed
without using a particular object.
Thus member belongs to the class as a whole rather than the objects
created from the class.
Used when we want to have a variable common to all instances of a
class.
SYNTAX:- static int count; static int max(int x, int y)
STATIC MEMBERS
Class variables and Class methods
Referred to as
![Page 19: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/19.jpg)
EXAMPLE
![Page 20: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/20.jpg)
RESTRICTIONS FACED BY STATIC METHODS:-
They can only call other static methods.
They can only access static data.
They cannot refer to this or super in any way.
STATIC
METHODS ARE
CALLED USING
CLASS NAME
![Page 21: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/21.jpg)
USING OBJECTS as PARAMETERS
We know how to pass simple types as parameters to methods.
It is possible, correct and common to pass OBJECTS to methods.
EXAMPLE
![Page 22: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/22.jpg)
CALL by VALUE vs. CALL by REFERENCE
CALL BY VALUE CALL BY REFERANCE
This method copies the value of an argument into the formal parameter of the subroutine
In this method, reference to an argument is passed to the parameter.
Does not access actual argument
This reference is used to access the actual argument.
Thus, changes made to parameter of the subroutine have no effect on the argument.
Thus, changes made to parameter will have an effect on the argument.
![Page 23: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/23.jpg)
EXAMPLE
CALL by VALUE:- Simple type arguments are passed
to methods.
CALL by REFERENCE:- Objects are passed to methods
REMEMBER
![Page 24: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/24.jpg)
RECURSION
JAVA supports recursion.
Recursion is the process of defining something in
terms of itself.
A method that calls itself is said to be recursive.
![Page 25: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/25.jpg)
VISIBILITY CONTROL
Visibility modifiers are used to restrict the access to certain variables and methods from outside the class.
Also known as ACCESS MODIFIERS.
Visibility Labels
PUBLIC PRIVATE
PROTECTED
![Page 26: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/26.jpg)
VISIBILITY CONTROL (Contd..)
PUBLIC Visible to entire class in which it is defined and All the class Outside
PRIVATE Enjoys highest degree of protection. Accessible only with their own class. Cannot be inherited, thus not accessible in sub-class.
Friendly When no access modifier is specified then the default version of public accessibility is known as “FRIENDLY”
PUBLIC
Friendly
Makes fields visible in all classes, regardless of their packages
Makes fields visible only in the same package.
PROTECTED Its level lies between PUBLIC ACCESS & FRIENDLY ACCESS. Makes the fields visible not only to all classes and subclasses in same package but also to subclasses in other packages
![Page 27: CLASSES, OBJECTS & METHODS · Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole](https://reader034.vdocuments.net/reader034/viewer/2022042308/5ed4b9e80b1c4b116053bacf/html5/thumbnails/27.jpg)