Object in java

What is Object ?

  •  An entity which have state and behavior is known as object.
  • state defines characteristic of object and behavior defines functionality of object
  • An object is a copy of class each time we create an object a copy of class will be created
  •  In the World of java each and everything is Object.
How to Create Object in Java ?
Let’s consider Notebook as object and understand the state and behavior
of Notebook.
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Notebook
 
 {
 
	//characteristic of notebook
 
	String brand;
	int pages;
	double price;
 
 
	//functionality of notebook
 
	void open()
	{
		System.out.println("open the book");
	}
	void read()
	{
		System.out.println("reading book");	
	}
	void close()
	{
		System.out.println("closing book");
	}
 
 
	//method to get details of notebook
 
	void bookdetail()
	{
 
		System.out.println("brand name: "+brand);
 
		System.out.println("number of pages: "+pages);
 
		System.out.println("price of notebook: "+price);
	}
 
 }
 
public class ObjectExample {
 
 
	public static void main(String[] args)
	{
        //creating instance of notebbok
		Notebook mbook=new Notebook();
 
		mbook.brand="class mate";
		mbook.pages=200;
		mbook.price=23.5;
 
		mbook.bookdetail();//calling function to get notebook detail
 
		System.out.println("************************");
 
 
		// creating second instance of notebook
 
		Notebook mbook1=new Notebook();
 
		mbook1.brand="camel";
		mbook1.pages=230;
	        mbook1.price=20;
 
	  mbook1.bookdetail();//calling function to get notebook detail
	}
 
}
 
output:
 
brand name: camel
number of pages: 230
price of notebook: 20.0

Leave a Reply

Your email address will not be published. Required fields are marked *