Class
Class can be defined as a template/ blueprint that describe the behaviors /states of a particular entityA class is declared using class keyword. A class contain both data and code that operate on that data. The data or variables defined within a class are called instance variables and the code that operates on this data is known as methods
Rules for java Class
- A class can have only public or default(no modifier) access specifier.
- It can be either abstract, final or concrete (normal class).
- A class can have extend only one one class/abstract class.
- It must have the class keyword, and class must be followed by a legal identifier(name).
- It may optionally extend one parent class. By default, it will extend java.lang.Object.
- It may optionally implement any number of comma-separated interfaces.
- The class's variables and methods are declared within a set of curly braces
{}. - Each .java source file may contain only one public class. A source file may contain any number of default visible classes.
- Finally, the source file name must match the public class name and it must have a .java suffix.
A simple class Example
Suppose, Student is a class and student's name, roll number, age will be its property. Lets see this in Java syntax
class Student.
{
String name;
int rollno;
int age;
}
Object
Object is an instance of class. You may also call it as physical existence of a logical template class.
or
what you see in naked eye is called Object.
When a reference is made to a particular student with its property then it becomes an object, physical existence of Student class.
Student std=new Student();
After the above statement std is instance/object of Student class. Here the new keyword creates an actual physical copy of the object and assign it to the std variable. It will have physical existence and get memory in heap area. The
new operator dynamically allocates memory for an object
No comments:
Post a Comment