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.
//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
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?
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 .
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;
No comments