Class and Object





What is class in Java ?

  • A class can be defined as a template/blue print that describes the behaviors/states that object of its type support.

  • A class, in the context of OOP, are description that are used to create objects, and to define object’s data members and behaviors.

  • The properties of class are called attributes or fields.

  • The behaviors of an object of a class are known as operations, and are defined using methods.

  • Fields and methods(functions) are collectively called member of class.

  • We declare a class by using class keyword.

Syntax :

class ClassName
 {
  //access_mode fields;
  //access_mode methods;
 }         

Where classNname is a valid identifier for the class.The class body can contain members, which can either be data or method declarations.

An access_mode (access specifier) is one of the following three keywords: private, protected, or public. These specifiers modify the access rights for the members.

NOTE If you do not specify any access specifier by-default it has default accessbility, not private like in C++.

Example :

class Test
{
  int a;
  private int b;
  protected int c; 
  public int d;
  void show()
   {
     System.out.print("show");
   }
}          

Object in Java ?

An entity that has state and behavior is known as an object e.g. dog,chair, table, pen, table, car etc.

An object has three characteristics:

  1. state: represents data (value) of an object.
  2. behavior: represents the behavior (functionality) of an object such as walk, talk, deposit etc.
  3. identity: Object identity is typically implemented via a unique ID. It is used internally by the JVM to identify each object uniquely.
  • Class is only a blueprint or a template. No storage is assigned when we define a class.

  • Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.

  • The process of creating objects from a class is called class instantiation.

  • An object is an instance of class.

  • Object contains data (variable) and behaviors (methods) declared in the class.


Object Instantiation :

The process of creating objects usually involves the following steps:

  1. Declaration of a variable to store the object reference : This involves declaring a reference variable of the appropriate class to store the reference to the object.

    e.g. ClassName ob1, ob2;

    Here are two reference variables ( ob1, ob2 ) that will denote/refer two distinct objects,
  2. Creating an object : This involves using the new operator in conjunction with a call to a constructor, to create an instance of the class.

    // Create two distinct objects.
        Ob1 = new ClassName();
        Ob2 = new ClassName();
    
     The new operator returns a reference to a new instance of class. This reference can be assigned to a reference variable of the appropriate class. Each object has a unique identity and has its own copy of the fields declared in the class definition.
    
     The purpose of the constructor call on the right side of the new operator is to initialize the newly created object.
    
     The new keyword is used to allocate memory at run time. All objects get memory in Heap memory area.
                       

Example :

WAP to demonstrate object creation and member accessing .
//Employee.java
class Employee
{
//field or data member or instance variable
  int id;  
  String name;

//instance method
  void showDetails()
  {
   System.out.println(id);
   System.out.println(name);
  }
 public static void main(String []arg)
 {
//creating an object of Employee
  Employee e = new Employee();

//accessing members
    e.id = 123;
  e.name = "Ayan Khan";
  e.showDetails();
 }
}                 
OUTPUT:
123
Ayan Khan

main() outside of class :

In application development it is not mandatory that every class has main () method. we create classes and use it from another class.

//MainClass.java
class Employee
{
  int id;  
  String name;

  void showDetails()
  {
   System.out.println(id);
   System.out.println(name);
  }
}

class MainClass
{
 public static void main(String []arg)
 {
//creating an object of Employee
  Employee e = new Employee();

//accessing members
    e.id = 123;
  e.name = "Ayan Khan";
  e.showDetails();
 }
}                 
OUTPUT:
123
Ayan Khan

In the above example we will pass MainClass.class file to JVM for execution, because it has main() method.


prowessapps.in Some Point to Understand :

  • Reference variable gets the memory in stack memory area, object gets the memory in heap memory area.

  • The reference variable refers to the object allocated in the heap memory area. prowessapps.in

  • Every Object of class has separate copies of data members. We can create as many objects of a class as we required.

    //Employee.java
    class Employee
    {
      int id;  
      String name;
    
      void showDetails()
      {
       System.out.println(id);
       System.out.println(name);
      }
     public static void main(String []arg)
     {
    //creating two object of Employee
      Employee e  = new Employee();
      Employee e1 = new Employee();
    
    //assigning first members
        e.id = 123;
      e.name = "Ayan";
    
    //assigning second members
        e1.id = 1234;
      e1.name = "Haaziq";
    
      e.showDetails();
      e1.showDetails();
     }
    }                 
    OUTPUT :
    123
    Ayan
    1234
    Haaziq 

    In the above example, created two objects class Employee. prowessapps.in


  • OBJECT ALIAS : We can create alias of an object.
    Alias is another name for existing object.
    Creation of Alias :

    ClassName new-ref-name = old-ref;
    e.g.
    Employee e = new Employee();
    //Aliasing
    Employee e2 = e;
    
    Now, e2 will point to the same object in heap, which is pointing by e.
    prowessapps.in
  • ANONYMOUS OBJECT : Anonymous simply means without name.
    An object which has no reference is known as anonymous object.
    It can be used at the time of object creation only.

    new Employee();//Anonymous object
    
    Calling method through reference:
    Employee e = new Employee();
    e.showDetails();
    
    Calling method through anonymous object:
    new Employee().showDetails();
    
    Anonymous object created, do the job and after that destroy. Anonymous object doesn't persist in memory.
                            

Next topic is class-members




 








Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved