CLASS
A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order:
- Modifiers: A class can be public or has default access.
- class keyword: class keyword is used to create a class.
- Class name: The name should begin with an initial letter (capitalized by convention).
- Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
- Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
- Body: The class body surrounded by braces, { }.
This is a simple example of a class.
public class Main
OBJECT:
It is a basic unit of Object-Oriented Programming and represents the real life entities. A typical Java program creates many objects, which as you know, interact by invoking methods. An object consists of :
- State: It is represented by attributes of an object. It also reflects the properties of an object.
- Behavior: It is represented by methods of an object. It also reflects the response of an object with other objects.
- Identity: It gives a unique name to an object and enables one object to interact with other objects.
An example of an object is dog, cow and cock.
They all belong to the class; animals.
This a simple example of an object.
public static void main(String[] args) {
Main myObj = new Main();
METHODS
A method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println
method, for example, the system actually executes several statements in order to display a message on the console.
CONTRUCTORS
Constructors are used to initialize the object’s state. Like methods, a constructor also contains a collection of statements(i.e. instructions) that are executed at the time of Object creation.
NEED FOR CONSTRUCTORS
Think of a Box.
If we talk about a box class then it will have some class variables (say length, breadth, and height).
But when it comes to creating its object(i.e Box will now exist in the computer’s memory), then can a box be there with no value defined for its dimensions. The answer is no.
So constructors are used to assign values to the class variables at the time of object creation, either explicitly done by the programmer or by Java itself (default constructor).
WHEN DOES A CONTRUCTOR NEED TO BE CALLED
Each time an object is created using a new() keyword, at least one constructor (it could be the default constructor) is invoked to assign initial values to the data members of the same class.
A constructor is invoked at the time of object or instance creation. For Example:
class shadow
{
.......
// A Constructor
new shadow() {}
.......
}
// We can create an object of the above class
// using the below statement. This statement
// calls above constructor.
shadow obj = new shadow();