3. Definition of class
and objects:
JAVA program must be encapsulated
in class that defines the state and
behaviour of the basic components
known as objects classes create
object and objects use methods to
communicate between them.
In JAVA the data items are called
fields and functions are called
methods.
4. Defining a class:
Class is a were defined data
type.once the class type has been
defined then we create variables of
type using declaration syntax.
Class class name{extends superclass}
Class classname{extends
superclassname}
{
{variable declaration}
5. Adding variables:
Data is encapsulation in class by
placing data field inside the body of
class def.These variables are known
ass instance variables.we can declare
the instance variables exactly the
same way to declare the local
variables.
6. Eg:
Class Rectangle
{
int lenth;
int width;
}
Class rectangle containss the two integer
type instance variables.It allowed to
declare them in one line.
int length,width;
Instance variable also known as
member variables.
7. Adding methods:
A class with only data field has no
file.We must add methods that are
necessary for manipulating the data
contained in the class.Methods are
declared inside the body of the
class.syntax,
Type
methodname(parrameter list)
{
8. Creating objects:
Object in JAVA is a block of memory
that contains space to store all the
instance variables.Creating an object
is also referred to infantiating an
object.
Rectangle rect1;
rect1=new Rectangle();
9. Accessing class
members:
We created objects each containing
its own set of variables and assign
values to these variables in order to
use our program.All variables must be
assigned values before they are used.
We cannot access the instance
variables and the methods directly.
Objectname.variable
name
10. This again can be done in two ways.
The first approach is to access the
instance variables using the dot
operator and compute the area.That
is,
Int
area1=rect1.length*rect1.width;
The second approach is to call the
method rectArea declared inside the
class.That is,
int area1=rect1.rectarea();
11. Constructors:
JAVA supports a special type of
method called a constructor,that
enables an oobject to initialize itself
when it is created.
Constructors have the same name as
the class itself.secondly,they do not
specify a return type,not even
void.this is because they return the
instance of the class itself.
12. Methods overloading:
JAVA to create methods that have
the same,but different parameter list
and different definitions.This is called
method overloading.
JAVA matches up the method name
first and then the number and type of
parameter to decide which one of the
definites to excute.This process is
known as polymorphism.
13. Static members:
Class basically contains two sections.
One declares variables and the other
declares methods.These variables and
methods are called is instance
variables and instance methods.
Member can be defined as follows,
static int count;
static int max(int x,int y);
14. Nested of methods:
The class nesting defines one
constructor and two methods,namely
largest( )and display ( ) calls the
method largest( ) to determine the
largest of the two numbers and then
displays the result.