Вы находитесь на странице: 1из 15

OBJECTS AND CLASS CONCEPTS

-MOHD ADNAN
OBJECTS

 Real world entities are known as objects


 Eg: chair, fan, bike, pen, keyboard, etc

 An object has 2 characteristics:

1. States: represents data (value) of an object.


2. Behaviour: represents the behaviour
(functionality) of an object
OBJECT (CONTD.)

 OBJECT: PEN
 State: Colour: Black, Name: Parker, Ink Color:Blue

 Behaviour: writing
OBJECT (CONTD)

 OBJECT: DOG
 STATE: name, colour, breed

 BEHAVIOUR: barking, fetching, sleeping, running


CLASS

 Class is a group of objects that has common


properties
 It is a blueprint from which objects are created

 A class can have a number of objects


CLASS (CONTD)
 It is a structure to hold variables & methods
together
 A class in java can contain:

 data member

 method

 constructor

 block

 class and interface


CLASS (CONTD)
 SYNTAX TO DECLARE A CLASS:
class <class_name>
{
data member;
method;
}
CREATION OF OBJECT
EXAMPLE OF OBJECT AND CLASS
class Student1{
int id; //data member (also instance variable)
String name;//data member(also instance variable)

public static void main(String args[]){


Student1 s1=new Student1();//creating an object o
f Student1
System.out.println(s1.id);
System.out.println(s1.name);
}
}
 INSTANCE VARIABLE: A variable that is created
inside the class but outside the method, is known as
instance variable.

 METHOD IN JAVA: In java, a method is like function


i.e. used to expose behaviour of an object.
Advantage of Method
 Code Reusability
 Code Optimization

new keyword
 The new keyword is used to allocate memory at
runtime.
ACCESS MODIFIERS

Access Modifier Class or member can be


referenced by
public methods of the same class, and
methods of other classes
private methods of the same class only

protected methods of the same class, methods


of subclasses, and methods of
classes in the same package
No access modifier methods in the same package only
(package access)
RULES FOR STATIC AND NON-STATIC METHODS

static Non-static
Method Method
Access instance variables? no yes

Access static class variables? yes yes

Call static class methods? yes yes

Call non-static instance no yes


methods?
Use the object reference this? no yes
FINAL FIELD

 A field can be declared final


 Both class and instance variables (static and non-
static fields) may be declared final.
 Effectively final declares the variable to be
constant

private final int PERFECT_SCORE = 100


JAVA PACKAGE
 A java package is a group of similar types of
classes, interfaces and sub-packages.

Вам также может понравиться