-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructor.java
More file actions
34 lines (30 loc) · 862 Bytes
/
Constructor.java
File metadata and controls
34 lines (30 loc) · 862 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
34
public class Constructor {
public static void main(String[] args){
//constructor = constructor is a specila method type which has no return type
//Constructors are only called automatically when an object is created with the new keyword.
//It helps to create objects with different attributes
Human human1 = new Human("Weditha",24,76);
Human human2 = new Human("Kivindu",18,80);
System.out.println(human1.name);
System.out.println(human2.name);
System.out.println();
human1.drink();
human2.eat();
}
}
class Human{
String name;
int age;
double weight;
Human(String name, int age, double weight){
this.name = name;
this.age = age;
this.weight = weight;
}
void eat(){
System.out.println(this.name+" is eating");
}
void drink(){
System.out.println(this.name+" is drinking");
}
}