-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassandobj.java
More file actions
33 lines (27 loc) · 904 Bytes
/
classandobj.java
File metadata and controls
33 lines (27 loc) · 904 Bytes
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
// Write a program to implement class and object concept
class Person {
// Instance variables
String name;
int age;
// Constructor method
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Instance method
public void sayHello() {
System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
}
}
public class classandobj {
public static void main(String[] args) {
// Create a new Person object with name "Alice" and age 25
Person person1 = new Person("Alice", 25);
// Call the sayHello method of the person1 object
person1.sayHello();
// Create a new Person object with name "Bob" and age 30
Person person2 = new Person("Bob", 30);
// Call the sayHello method of the person2 object
person2.sayHello();
}
}