8000 Merge pull request #713 from javaistic/class-objects · javaistic/javaistic@cea7cc1 · GitHub
[go: up one dir, main page]

Skip to content

Commit cea7cc1

Browse files
authored
Merge pull request #713 from javaistic/class-objects
Add Class and objects docs
2 parents ec11ffc + 67774b9 commit cea7cc1

File tree

2 files changed

+221
-1
lines changed

2 files changed

+221
-1
lines changed

src/navs/documentation.js

Lines changed: 1 addition & 1 deletion
< 8000 /thead>
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ export const documentationNav = {
2828
pages['continue-statement'],
2929
],
3030
'Java Arrays': [pages['arrays'], pages['multidimensional-arrays']],
31-
'Java OOPS': [pages['static-keyword']],
31+
'Java OOPS(I)': [pages['class-objects'], pages['static-keyword']],
3232
}

src/pages/docs/class-objects.mdx

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
---
2+
title: Java Class and Objects
3+
description: In this tutorial, we will learn about classes and objects in Java with the help of examples.
4+
---
5+
import { TipInfo } from "@/components/Tip";
6+
7+
Java is an object-oriented programming language. The core concept of the object-oriented approach is to break complex problems into smaller objects.
8+
9+
An object is any entity that has a state and behavior. For example, a bicycle is an object. It has
10+
11+
- **States**: idle, first gear, etc
12+
- **Behaviors**: braking, accelerating, etc.
13+
14+
Before we learn about objects, let's first know about classes in Java.
15+
16+
## Java Class
17+
A class is a blueprint for the object. Before we create an object, we first need to define the class.
18+
19+
We can think of the class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.
20+
21+
Since many houses can be made from the same description, we can create many objects from a class.
22+
23+
### Create a class in Java
24+
We can create a class in Java using the class keyword. For example,
25+
26+
```java
27+
class ClassName {
28+
// fields
29+
// methods
30+
}
31+
```
32+
33+
Here, `fields` [(variables)](/docs/variables-literals) and [methods](/docs/methods) represent the **state** and **behavior** of the object respectively.
34+
35+
- fields are used to store data
36+
- methods are used to perform some operations
37+
38+
For our `bicycle` object, we can create the class as
39+
40+
```java
41+
class Bicycle {
42+
43+
// state or field
44+
private int gear = 5;
45+
46+
// behavior or method
47+
public void braking() {
48+
System.out.println("Working of Braking");
49+
}
50+
}
51+
```
52+
In the above example, we have created a class named `Bicycle`. It contains a field named `gear` and a method named `braking()`.
53+
54+
Here, `Bicycle` is a prototype. Now, we can create any number of bicycles using the prototype. And, all the bicycles will share the fields and methods of the prototype.
55+
56+
<TipInfo>
57+
58+
**Note:** We have used keywords private and public. These are known as access modifiers. To learn more, visit Java access modifiers.
59+
60+
</TipInfo>
61+
62+
---
63+
64+
## Java Objects
65+
An object is called an instance of a class. For example, suppose Bicycle is a class then MountainBicycle, SportsBicycle, TouringBicycle, etc can be considered as objects of the class.
66+
67+
Creating an Object in Java
68+
Here is how we can create an object of a class.
69+
70+
```java
71+
className object = new className();
72+
73+
// for Bicycle class
74+
Bicycle sportsBicycle = new Bicycle();
75+
76+
Bicycle touringBicycle = new Bicycle();
77+
```
78+
We have used the `new` keyword along with the constructor of the class to create an object. Constructors are similar to methods and have the same name as the class. For example, `Bicycle()` is the constructor of the `Bicycle` class. To learn more, visit [Java Constructors](/docs/constructors).
79+
80+
Here, `sportsBicycle` and `touringBicycle` are the names of objects. We can use them to access fields and methods of the class.
81+
82+
As you can see, we have created two objects of the class. We can create multiple objects of a single class in Java.
83+
84+
<TipInfo>
85+
86+
**Note:** Fields and methods of a class are also called members of the class.
87+
88+
</TipInfo>
89+
90+
---
91+
92+
## Access Members of a Class
93+
We can use the name of objects along with the `.` operator to access members of a class. For example,
94+
95+
```java
96+
class Bicycle {
97+
98+
// field of class
99+
int gear = 5;
100+
101+
// method of class
102+
void braking() {
103+
...
104+
}
105+
}
106+
107+
// create object
108+
Bicycle sportsBicycle = new Bicycle();
109+
110+
// access field and method
111+
sportsBicycle.gear;
112+
sportsBicycle.braking();
113+
```
114+
In the above example, we have created a class named `Bicycle`. It includes a field named `gear` and a method named `braking()`. Notice the statement,
115+
116+
```java
117+
Bicycle sportsBicycle = new Bicycle();
118+
```
119+
Here, we have created an object of Bicycle named sportsBicycle. We then use the object to access the field and method of the class.
120+
121+
- `sportsBicycle.gear` - access the field gear
122+
- `sportsBicycle.braking()` - access the method `braking()`
123+
124+
We have mentioned the word **method** quite a few times. You will learn about [Java methods](/docs/methods) in detail in the next chapter.
125+
126+
Now that we understand what is class and object. Let's see a fully working example.
127+
128+
### Example: Java Class and Objects
129+
```java
130+
class Lamp {
131+
132+
// stores the value for light
133+
// true if light is on
134+
// false if light is off
135+
boolean isOn;
136+
137+
// method to turn on the light
138+
void turnOn() {
139+
isOn = true;
140+
System.out.println("Light on? " + isOn);
141+
142+
}
143+
144+
// method to turnoff the light
145+
void turnOff() {
146+
isOn = false;
147+
System.out.println("Light on? " + isOn);
148+
}
149+
}
150+
151+
public class Main {
152+
public static void main(String[] args) {
153+
154+
// create objects led and halogen
155+
Lamp led = new Lamp();
156+
Lamp halogen = new Lamp();
157+
158+
// turn on the light by
159+
// calling method turnOn()
160+
led.turnOn();
161+
162+
// turn off the light by
163+
// calling method turnOff()
164+
halogen.turnOff();
165+
}
166+
}
167+
```
168+
#### Output:
169+
170+
```plaintext
171+
Light on? true
172+
Light on? false
173+
```
174+
In the above program, we have created a class named `Lamp`. It contains a variable: `isOn` and two methods: `turnOn()` and `turnOff()`.
175+
176+
Inside the `Main` class, we have created two objects: `led` and `halogen` of the `Lamp` class. We then used the objects to call the methods of the class.
177+
178+
- `led.turnOn()` - It sets the isOn variable to true and prints the output.
179+
- `halogen.turnOff()` - It sets the isOn variable to false and prints the output.
180+
181+
The variable `isOn` defined inside the class is also called an instance variable. It is because when we create an object of the class, it is called an instance of the class. And, each instance will have its own copy of the variable.
182+
183+
That is, `led` and `halogen` objects will have their own copy of the `isOn` variable.
184+
185+
### Example: Create objects inside the same class
186+
Note that in the previous example, we have created objects inside another class and accessed the members from that class.
187+
188+
However, we can also create objects inside the same class.
189+
190+
```java
191+
class Lamp {
192+
193+
// stores the value for light
194+
// true if light is on
195+
// false if light is off
196+
boolean isOn;
197+
198+
// method to turn on the light
199+
void turnOn() {
200+
isOn = true;
201+
System.out.println("Light on? " + isOn);
202+
203+
}
204+
205+
public static void main(String[] args) {
206+
207+
// create an object of Lamp
208+
Lamp led = new Lamp();
209+
210+
// access method using object
211+
led.turnOn();
212+
}
213+
}
214+
```
215+
#### Output
216+
217+
```plaintext
218+
Light on? true
219+
```
220+
Here, we are creating the object inside the `main()` method of the same class.

0 commit comments

Comments
 (0)
0