- Fields or data members
- Methods
- Constructor
1. Fields or Data Members
A class contains the data members or Fields, which define and store the properties, attribute or data for manipulation in its objects.e.g. A dog has attributes like, height, color, race type and age and many more. All these attributes of the dog and shown as Fields in the Class.
public class dog {
protected String name;
int height;
public String color;
private double weight;
}
protected String name;
int height;
public String color;
private double weight;
}
name, height, color and weight are called class variables or data members or fields of class dog.
A fields has the following properties
Access modifier : A fields or variable starts with access modifier public, private, protected or no modifier. Access modifier defines the access level of the field
Data Type: A variable or field must be given a data type like String, int, float, double or some other allowed JAVA data types.
Name : A field must be given a proper and meaningful name like color, height, weight etc are the names given to the variables or fields
There are three kinds of variables can exists in a class Member variables in a class—these are called fields. Variables in a method or block of code—these are called ocal variables. Variables in method declarations—these are called parameters.
Constructors:
Constructor are just like methods but different as these do not have return type or return values
Constructor has the same name as the name of the class. The purpose of the constructor is just to instantiate the objects of the class.
When ever an object of the class is created the first thing to be executed is the constructor.
It is called constructor because it constructs the objects.
public class dog {
dog() {
{
}
Rules or properties of a constructor
· Constructor will be called automatically when the object is created.
· Constructor name must be similar to name of the class.
· Constructor should not return any value even void also. Because basic aim is to place the value in the object. (if we write the return type for the constructor then that constructor will be treated as ordinary method).
· Constructor definitions should not be static. Because constructors will be called each and every time, whenever an object is creating.
· Constructor should not be private provided an object of one class is created in another class (Constructor can be private provided an object of one class created in the same class).
· Constructors will not be inherited from one class to another class (Because every class constructor is create for initializing its own data members).
· The access specifier of the constructor may or may not be private.
1. If the access specifier of the constructor is private then an object of corresponding class can be created in the context of the same class but not in the context of some other classes.
2. If the access specifier of the constructor is not private then an object of corresponding class can be created both in the same class context and in other class context.
Difference between Method and Constructor
Method | Constructor |
---|---|
Method can be any user defined name | Constructor must be class name |
Method should have return type | It should not have any return type (even void) |
Method is not provided by compiler in any case. | The java compiler provides a default constructor if we do not have any constructor. |
Method should be called explicitly either with object reference or class reference | It will be called automatically whenever object is created |
There are three types of constructors
1. Default Constructor
If no constructor is defined in a class a default constructor is called. The default constructor is not defined in the class therefore not coding is shown in the class.
A default constructor look like in a class called dog
public class dog {
dog() {
{
}
2. No Argument Constructor
A constructor defined with no arguments is called no argument constructor. It is similar to default constructor but different in way that it has some processing code. e.g.
public class dog {
dog() {
public int a=5;
public String name=”M NAVEED”;
{
}
An important point you should not forget is that when ever you define a subclass and a constructor other than default constructor is defined in the super class then you must need to define a default constructor in the super class. Otherwise you will get compile time error. Below example demonstrate the point.
public class dog {
dog() {
public int a=5;
public String name=”M NAVEED”;
{
}
Public class teadydog extends dog {
}
Then this program will generate compile time error
I order to overcome this problem you need
public class dog {
dog() {
{
dog() {
public int a=5;
public String name=”M NAVEED”;
{
}
Public class teadydog extends dog {
}
Then this will execute OKAY
3.Parameterized constructor
Constructor with arguments(or you can say parameters) is known as Parameterized constructor.
Example: parameterized constructor
In this example we have a parameterized constructor with two parameters
id
and name
. While creating the objects obj1
and obj2
I have passed two arguments so that this constructor gets invoked after creation of obj1 and obj2.public class Employee {
int empId;
String empName;
//parameterized constructor with two parameters
Employee(int id, String name){
this.empId = id;
this.empName = name;
}
void info(){
System.out.println("Id: "+empId+" Name: "+empName);
}
public static void main(String args[]){
Employee obj1 = new Employee(10245,"Chaitanya");
Employee obj2 = new Employee(92232,"Negan");
obj1.info();
obj2.info();
}
}
Output:
Id: 10245 Name: Chaitanya
Id: 92232 Name: Negan