» » Class and object

Class

  • Class is a structure.
  • Binding the data with its related and corresponding functions.
  • Class is the base for encapsulation.
  • Class is a user defined data type in java.
  • Class will act as the base for encapsulation and implement the concept of encapsulation through objects.
  • Any java applications looks like collection of classes but where as c- application looks like collection of functions.
public class Example{

         //variable declaration
          int id;

      // methods
        public int getId() {
          return id;
         }

    public void setId(int id) {
        this.id = id;
    }

}

Object

  • Object is nothing but instance (dynamic memory allocation) of a class.
  • The dynamic memory allocated at run time for the members [non-static variables] of the class is known as object.

What is a Reference variable?

  • Reference variable is a variable which would be representing the address of the object.
  • Reference will act as a pointer and handler to the object.
  • Since reference variable always points an object.
  • In practice we call the reference variable also as an object.
  • We can create an object by using new operator.
  • If we want to call any method inside the class we need object
               Syntax: A obj= new A();

Example program:

      public class A {
         int a;
     public  void Print(){
        System.out.println("value of a="+a);
    }
    public static void main(String[] args) {
   A obj=new A();
  obj.print();
    }
}

Output: value of a=0
What Object Contains?
  • Object of any class contains only data.
  • Apart from the data object of a class would not contains anything else.
  • Object of a class would not contain any functionalities or logic.
  • Thus object of a class would be representing only data and not represent logic.

What is state of the Object?

  • The data present inside object of a class at that point of time is known as state of the object

What is behavior of the object? 

  • The functionalities associated with the object => Behavior of the object.
     The state of the object changes from time-to-time depending up on the functionaities that are executed on that object but whereas behavior of the object would not change.

Naming conventions for declaring a class:

  • Class name should be relevant.
  • Use UpperCamelCase for class names. //StudentDemoClass
  • For Methods names follow camelCase(); //getName();
  • Declare variables lowercase first letter. // int rollNumber
  • To declare final static variables means which will acts like constants
    MIN_WIDTH=10;
    MAX_AGE=18;

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu