Monday, 10 May 2021

Classes and objects in java

 Classes and Objects are the very basic entity of object oriented programing. Without them, we can not proof any concept of object oriented programing.

That's why we need to give very high attention. First we need to understand what is the class?

Class in java

Class is a blueprint. For example we are going to build a home, first we need to prepare a blueprint on paper. Same as building home, class is a blueprint of objects.

Same as home have many attributes and behaviors, like

Attributes: Windows, doors etc.
Behaviors: Open window, Close window, Open door, Close door.

Object also have their attributes and behaviors. Attributes of object are the fields or instance variable, and behaviors of objects are methods.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.pervezblogger.example

import java.util.Date;

public class Bank{

	private String accountNumber;
	private String accountHolderName;
	private Date accountOpeningDate;
	private Double accountBalance;
	
	public Bank(String accountNumber, String accountHolderName){
	
		this.accountNumber = accountNumber;
		this.accountHolderName = accountHolderName;
		this.accountBalance = 0.0;
		this.accountOpeningDate = new Date();
	
	}

	public String getAccountNumber(){
	
		return accountNumber;
	
	}
	
	public String getAccountHolderName(){
	
		return accountHolderName;
	
	}
	
	public Double getAccountBalance(){
	
		return accountBalance;
	
	}
	
	public Date getAccountOpeningDate(){
	
		return accountOpeningDate;
	
	}
	
	public Double addBalance(Double amount){
	
		accountBalance+=amount;
	
		return accountBalance;
	}

}

Into provided example, we are taking Bank class. We have used so many keywords into this example, we will discuss them also latter.

Lets discuss the basic components of class.

For creating any class we need class keyword. Before class keyword, we can use public access specifiers. If class is public, then the name of .java file should be same as class name.
Each .java file should have at least one class with public access specifiers.

After the class keyword, we give the class name, here Bank is the class name. According to naming convention rules, class name should be meaning full, and should be Capitalized. It means, The first character should be capital, no use of spaces, and each word should be separate with Capital letter.

Now after the class name we will start the class block. In JAVA blocks are denoted with {}. The block defined the scope in JAVA.

Into the class block we defined the class members and constructors. In java there are mainly two class members.

  • Instance variables or attributes
  • Methods or behaviors
We will discuss about class members in detail letter.

Object in java

Object is the physical entity of class. For example, we have prepared blueprint of home, now we have started construction of our home. For construction home we need land, and for constructing object we need memory space. In java we have a special memory space for save object that is heap memory.

Classes have constructors which is helpful for creating objects. Constructor have same name as Class name, and no return type. It can also have parameters. If we don't create any constructor, then JVM assumes that there is a default constructor, which have no parameters, and have one statements, that is

super()

which is helpful to call super class constructor. As we know that, JAVA supports Inheritance, and if Class not extends any class then JVM assumes that class extends Object class. So basically Object is a Super class of all JAVA classes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package com.pervezblogger.example

public class Bank extends Object{

	public Bank(){
	
	   super();
	
	}

	//Class members

}


For creating object of any class we call constructor with new keyword.

It will returns the reference of object, which we can hold into the Compatible Reference variable, which is either Into self reference variable like, 

Bank bank = new Bank();

or into Super class Reference variable,

Object bankSuperClass = new Bank();

here Object is the super class of Bank class. Or into the implementing reference variable.

1
2
3
4
5
6
7
package com.pervezblogger.example

public interface BankInterface{

	//Abstract methods

}


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package com.pervezblogger.example

public class Bank extends Object implements BankInterface{

	public Bank(){
	
	   super();
	
	}

	//Class members

}


BankInterface bankInterface = new Bank();


Here we have a syntax for creating object and holding it into reference variable:

TypeOfReferenceVariable nameOfReferenceVariable = new Constructor(params);

as we can see into syntax, we can also call parametrized constructor for creating objects.

This keyword

This keyword is used to hold the reference of current object. 

What is current object? 😢 😕

the current object is where we are currently doing the operation. like as we can see into Bank class, we have used this keyword into constructor. Here this keyword is used to solve the ambiguity between the local variable and instance variable because both have same name. But the question is how this keyword solve the ambiguity?

Because here this keyword have reference of current object i.e. Bank object. And when we call 

this.accountNumber;

It means we have used the instance variable of current class object.

Important: This keyword cannot be use outside the class. It means if this keyword is used into any class, then this keyword refer to that class only.

If I use this keyword into Bank class, then it refers Bank class only.

Usage of this keyword

1. To solve the ambiguity between local variable and instance variable.
2. For calling the current class constructor. It is called constructor Chaining.
3. this key can also call method, but this feature have no use.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.pervezblogger.example

import java.util.Date;

public class Bank{

	private String accountNumber;
	private String accountHolderName;
	private Date accountOpeningDate;
	private Double accountBalance;
	
	public Bank(){
	
		this.accountBalance = 0.0;
		this.accountOpeningDate = new Date();
		
	}
	
	public Bank(String accountNumber, String accountHolderName){
		
		//Constructor chaining
		this();
		this.accountNumber = accountNumber;
		this.accountHolderName = accountHolderName;
		
	
	}

	public String getAccountNumber(){
	
		return accountNumber;
	
	}
	
	public String getAccountHolderName(){
	
		return accountHolderName;
	
	}
	
	public Double getAccountBalance(){
	
		return accountBalance;
	
	}
	
	public Date getAccountOpeningDate(){
	
		return accountOpeningDate;
	
	}
	
	public Double addBalance(Double amount){
	
		accountBalance+=amount;
	
		return accountBalance;
	}

}

Line number 22 shows constructor chaining.

Class members

Class have two types of class members

  • Instance variable
  • Methods
Static variables are also class members. We will discuss them on another blog.

The instance variable are belongs to the object. For example we are making the object of Bank class

Bank b1 = new Bank("312001000111", "John wick");
Bank b2 = new Bank("312001000112", "Alladin");

Then objects which are hold by b1 and b2 reference variable, have different values of instance variable.

Methods are used to create the operations into class. Into methods, we can also use Instance variables.

No comments:

Post a Comment