Skip to content

Commit 6bcd0ca

Browse files
Custom Class
1 parent 8079c3f commit 6bcd0ca

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

38.Custom_Class/README.md

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Java Tutorial: Creating Our Own Java Class
2+
### Writing a Custom Class :
3+
- Syntax of a custom class :
4+
5+
```
6+
class <class_name>{
7+
field;
8+
method;
9+
}
10+
```
11+
12+
- Example :
13+
14+
```
15+
public class Employee {
16+
int id; // Attribute 1
17+
String name; // Attribute 2
18+
}
19+
```
20+
21+
**Note:** The first letter of a class should always be capital.
22+
23+
- Any real-world object = Properties + Behavior
24+
- Object in OOPs = Attributes + Methods
25+
26+
### A Class with Methods :
27+
- We can add methods to our class Employee as follows:
28+
29+
```
30+
/*
31+
public class Employee {
32+
public int id;
33+
public String name;
34+
35+
public int getSalary(){
36+
//code
37+
}
38+
public void getDetails(){
39+
//code
40+
}
41+
};
42+
*/
43+
```
44+
45+
### Code as Described in the Video
46+
47+
```
48+
class Employee{
49+
int id;
50+
int salary;
51+
String name;
52+
public void printDetails(){
53+
System.out.println("My id is " + id);
54+
System.out.println("and my name is "+ name);
55+
}
56+
57+
public int getSalary(){
58+
return salary;
59+
}
60+
}
61+
62+
public class cwh_38_custom_class {
63+
public static void main(String[] args) {
64+
System.out.println("This is our custom class");
65+
Employee harry = new Employee(); // Instantiating a new Employee Object
66+
Employee john = new Employee(); // Instantiating a new Employee Object
67+
68+
// Setting Attributes for Harry
69+
harry.id = 12;
70+
harry.salary = 34;
71+
harry.name = "CodeWithHarry";
72+
73+
// Setting Attributes for John
74+
john.id = 17;
75+
john.salary = 12;
76+
john.name = "John Khandelwal";
77+
78+
// Printing the Attributes
79+
harry.printDetails();
80+
john.printDetails();
81+
int salary = john.getSalary();
82+
System.out.println(salary);
83+
// System.out.println(harry.id);
84+
// System.out.println(harry.name);
85+
}
86+
}
87+
```
88+
89+
**Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-38/Ch8.pdf)**
90+
91+
**Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-38/UltimateJavaCheatSheet.pdf)**

0 commit comments

Comments
 (0)