Class & Objects

Class & Objects

What is Class?

  1. A class server as a blueprint or a plan or a template. It specific what data (Properties) and what method (Behavior) are included in object of that class. Class does not have existence (Memory will not be allocated).
  2. We can also define it like "A class is a way of binding the data and associated methods in a single unit".
  3. Class is a keyword which is used for developing or creating user defined data types. Class name represents a JAVA valid variable name and it is treated as name of the class. Class names are used for creating objects.
  4. Class contains two parts namely variable declaration and method definitions. Variable declaration represents what type of data members,we use as a part of the class. Method definition represents the type of methods,we use as a part of the class to perform an operation.

Syntax for defining a Class :

 class <Classname >
   Variable Declaration;
   Methods Definition;

Example :

  class A class name
  {    int i;   i is an attribute behaving as property of a class A.
      void show(){}   define a particular functionality(behavior) using the properties of a class.

Attributes: Properties of the each Entity is represented as an attributes in java.
Example name, age, height, weight, etc are properties for person entity/class.

Methods: Behaviors of the entity is called/represented as a method in a class.

What is an Object ?

  1. Instance of a class is known as an object.
  2. Blue print of a class is known as an object.
  3. State of the Entity is represented as the object.
  4. Grouped item (grouped item is a variable which allows us to store more than one value) is known as an object.
  5. Value form of a class is known as an object.
  6. Class variable is known as an object.
  7. Logical runtime entity is known as an object.
class A{
                                int i;
                                void show()
                                class Manager
                                public static void main(String []args)
                                A obj    = new A();
"obj" is an object reference of type class A,is referring to an object of type class A new operator dynamically allocates memory (at runtime)for an object and return the base address of object.;

Calling statement to show method of class A.

Memory Management :

Java Setup Wizard
Memory Management

Memory Management for Program :

Java Setup Wizard
(1),(2),(3a),(3b),(3c),(4) are the way memory has been utilized in program .
  1. During execution of class B. at first JRE(Java Runtime Environment)will be called to stack.
  2. JRE in turn calls main() method to stack.
  3. When A obj = new A(); Statemet execute.
    1. Then an object of class A will be created in Heap segment of a memory depending on attributes size.
    2. In Stack A obj reference create.
    3. Object reference create between reference and objetc.
  4. main method in turn calls show() method to stack for executing using object reference. Show() method uses attribute "i" for its functionality .After the execution of show() method it will be removed from the stack .As a step toward the completion of the execution of program all methods will be removed from the stack in the LIFO(Last In First Out) order as soon as there execution completes.
class Person
                                 int age;
                                 float weight;
                                 double height;
                                 double runSpeed()

                                 class PersonManager
                                 public static void main(String args[]) //Main Method
                                 Person kumar  = new Person();
                                 kumar .age  = 28;
                                 kumar .weight  =67.7f;
                                 kumar .height  = 5.11;
                                 double result  = kumar.runSpeed();
                                 System.out.println("KumarResult  : "+result);
                                 Person praveen= new Person();
                                 praveen.age  = 27;
                                 praveen.weight  = 62.8f;
                                 praveen.height  = 5.1;
                                 result  = praveen.runSpeed();
                                 System.out.println("Praveen Result  : "+result);