Classes and Objects:

Class:

It is a blue print or prototype from which individual objects are generated.

Objects:

Any real world entity that is having some states(data) and behavior(functionality).

Example for a class in Java:

public class Student {
	private int sno;//student no
	private String sname;//student name
	public void displayDetails(){
		System.out.println("student no is .."+sno);
		System.out.println("student name is.."+sname);
	}
}

Example for an Object in Java:

public class MainClass {
	public static void main(){
		Student s = new Student(); //this creates an object for Student class
	}
}
Real world example for Class & Object: Building plan is a class, and the building that is constructed is object. Building plan is a class because it is a blue print from which we have constructed the real building. Using the same building plan we can construct similar kind of buildings.

Representing the real world objects:

Real world objects will be represented with ellipses, pictorially. Below picture is a pen object - pictorial representation. REprasentation of a pen object.PNG
Object is a collection of states and behaviour where behaviour acts on the states. In the object representation, inner ellipse represents data, and outer ellipse represents behavior (functions).

Real world objects & Data security model:

Every real world object will have some private data which outside world cannot access directly. (data security). For example if you try to grab the money purse of others, how do they feel? It doesn't look good. They will try to protect that. Similary can I person change the speed of Bicycle with out peddling? It is impossible. Here speed is the data and peddling is the functionality. If they want to access the data then they have to go through behaviours(functions) only. In the below example steve is an object, and Bicycle is an object. Now think how steve can ride on the bicycle. He has to call peddling functionality of the bicycle object. Below picture is an example of 2 objects steve, and a bicycle. Steve can't change speed of the bicycle directly. If steve wanted to change speed of the cycle then he has to peedle() the cycle. So here two objects steve & bicycle communicates with methods.

Pictorial representation of Steve riding a bicycle:

Human being
In the above example Steve is an object and bicycle is also an object.

Object to object communication through behaviours:

In the real world scenario two objects will communicate through behaviours.
For example: Steve riding on the bicycle.

Writing software classes:


When we are creating software classes from real world objects below rules will apply. Rules: 1.Converts all the states into variables. 2.Convert all the behaviours to functions. We can achieve data security model in software classes by using "Access Modifiers-Private". So that ourside world can't directly access our data. States must be private (unless there is any specific reason). Behaviour must be public (generally, but not mandatory)
Below is the pictorial representation of a Bicycle object: BycycleObject
Lets see how to represent above object by using software classes:
File name : Bicycle.java
public class Bicycle {

private int speed, no_of_gears , no_of_wheels; private int cur_gear; private boolean brk_applied;


public void pedding() { speed++; } public void changeGear() { cur_gear++; } public void applyBreak() { speed--; brk_applied = true; }
}

Encapsulation: Object oriented principle

The wrapping up of data and behaviour together in a box is called encapsulation.
Class is also called encapsulation body. Because in above example we are wrapping both data & behavior togher. Encapsulation does not guarantee the data security but it provides a way to achieve it. (By using access modifiers "private"). Remember even if I remove private modifier to the data, still it is encapsulation.

Data Abstraction: Object oriented principle

General meaning of Abstraction is-showing what is wanted & hiding unwanted details. We know that in real world, every object will have some kind of data security. Hiding unwanted details about the internal data and showing what is wanted is called information hiding / data hiding / Data abstraction. In a class when you make the data as private, outsiders can't see the internal implementation details of the data. Hence you are hiding the internal details of the data. So this is called as data abstraction. Remember, even though we are saying it as information hiding or data hiding, we are not actually hiding it, because any one may read and access our private data through public functions. But what we are really hiding is only the "internal implementation or details about the data". So Data Abstraction is the right word to describe it.

Example of abstraction:


Bank - As an account holder you can with draw your money, but you will not be given full details like where is the money stored in the bank etc.
Note: Class is a data type because we can create variables with that class.
Example: int i = 10; //here int is a data type Bicycle b = new Bicycle(); //similarly here Bicycle is a datatype Class is a abstract data type because using class we can achieve data abstraction and class is a data type also. Data Abstraction + Data type = Abstract data type. (So class is an abstract data type)

Creating software objects

We have to follow below 3 steps when we are creating software objects. 1.Create the object with "new" operator. 2.Initialize the object. (i.e initialize instance variables) 3.Start using the object.
Below is the pictorial representation of pen object: pen object 2
Below is the code for above object:
File name : MainClass.java class pen {
private int inklevel; private String color; private boolean cap_state;
public void init() { ink _level = 100; color = 'Red'; cap state = false; }
}
  public class MainClass
 {
    public static void main(String[ ] args)
   {
     Pen p = new Pen();  // step 1: create object
     p. init();  // step 2: initialize object (though we use constructors for it)
     p. openCap();
     p. write();	// step 3: use the object, i.e call the functions with object.
    }
  }