Object and Classes
Classes and Objects in Java
In java language everything is depend on Classes and Objects. The basic concepts of Object Oriented Programming which revolve the real life entities.
Class
A class is a sample / prototype or blueprint which is defined by the user where objects are created. A Class represents the group of properties or methods or function that are common to all objects of one type.
Simply we can say, When we declare a class declarations can include these components..
- Modifiers : access modifiers as we discussed.
- Class name: Name of the class.
- Superclass(if any): parent class name
- Interfaces(if any): Number of interfaces
- Body: The class body surrounded by braces, { }.
Object : - Everything is object in real world. Object have state, behavior and identity.
- State : It is represented by the attribute of an object. It also unfold the properties of an object.
- Behavior : It is represented by the methods of an object. It also unfold the response of an object with other objects.
- Identity : What is your identity. Identity provides a unique name to an object and enables one object to interact with other objects.
Example of an object : dog
Object
Syntax for the Object :
class_name object_name = new class_name();
Creating an Object:
As mentioned previously a class provides the blueprints for objects. So basically an object is created from a class. In java the new key word is used to create new objects.
There are three steps when creating an object from a class:
- Declaration . A variable declaration with a variable name with an object type.
- Instantiation . The 'new' key word is used to create the object.
- Initialization . The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
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).
- It must have the class keyword, and class must be followed by a legal identifier.
- 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; }
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
post a comment