- Class is the basis for object oriented programming in Java.
- A class defines a structure , a template which the objects
of that type have. - It defines a new data type.
- Object is an instance of a class. It follows the template
- Object is concrete and it occupies memory
Any Java code must be contained within a class
Lets take a simple example to understand the idea
of class and object
example
class Student
{
private String name ;
private int age ;
}
- This class defines structure for a student.
- Any object of Student type will have two
properties : name and age - Though each Student object will follow the
same template as defined by class Student,
each will have it's own data for name and age.
Let us declare some Student objects
Student one = new Student () ;
// assign values to student properties
one.name = "Harry" ;
one.age = 20 ;
Student two = new Student () ;
two.name = "Tom" ;
two.age = 25 ;
name,age are the attributes of Student which define it
Class usually also has associated methods
class Student
{
private String name ;
private int age ;
public String getName ()
{
return name ;
}
public void setName (String input)
{
name = input ;
}
}
Student one = new Student () ;
one.setName ("Harry") ;
Instead of directly assigning a value as above,
we called a function.
Same way, to access the attribute value, we call getName function
System.out.println ( one.getName () ) ; // prints Harry
The above concept can be extended to more complex classes
having number of attributes and functions.
General structure of a class is
class classname
{
type property1 ;
type property2 ;
type method1 (list of parameters)
{
// processing code
}
type method2 (list of parameters)
{
// processing code
}
}
N number of properties and methods can be there
properties: They define the data of object and are also known as instance variables. Each object has it's own data
methods: They contain code to do processing.Each object can access
these methods to initiate some action or do a process
setName method was called to assign name to student
getName method was called to get the name of student
methods can be used for handling basic tasks like above
to complex processes.They can interact with each other
to accomplish a task together
Declaring objects
In the above example, we declared Student object
Student one = new Student () ;
- Student is the name of class
- one is a variable of type Student (remember every class defines a new data type)
- new is an operator which dynamically allocates memory by calling
the constructor for class
new Student () ;
Three things happen - Constructor of Student class is called,
- memory is allocated to newly
defined object - memory address is returned which is assigned
to variable one
Once an object is created and it's reference stored, we can access it's
properties and invoke class methods on this reference
one.name ; // returns name of student
one.age ; // returns age of student
one.setName () ; // calls method setName
Obtaining a new object of class can be divided in two steps
Step 1
Student one ;
It declares a variable of type Student. one does not refer
to an actual object at this point.
System.out.println (one) ; // prints null
When a variable of Object type does not point to an actual object,
it holds null.
Step 2
one = new Student ( ) ;
Now, object is actually created and it's reference (or memory address)
is assigned to variable one
General form of creating objects
classtype1 var-name = new classtype2 () ;
- classtype1 : It is the type of the holding variable
- var-name : name of variable which will hold reference to object
- classtype2 : type of object being created
- In the above example classtype1 and classtype2 are both same (Student).
They can be different when classes are part of hierarchy.
How object reference variables work
It's easy to understand but very important concept.
Taking again the Student class example
Student one = new Student () ;
one.name = "Harry" ;
one.age = 20 ;
Student two = new Student () ;
two.name = "Tom" ;
two.age = 25 ;
System.out.println (one.name) ; // prints Harry
System.out.println (two.name) ; // prints Tom
two = one ; // assign address of one to two
System.out.println (one.name) ; // prints Harry
System.out.println (two.name) ; // prints Harry
Now two also refers to object referred by one
one = null ; // assign null explicitly
System.out.println (one.name) ; // will cause exception, as one does not refer to any object
System.out.println (two.name) ; // prints Harry
- one does not refer to any object now but two continues
to refer to an object - It's important to see which variable is referring to which
object at any time