-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor.java
More file actions
28 lines (28 loc) · 873 Bytes
/
constructor.java
File metadata and controls
28 lines (28 loc) · 873 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
public class constructor {
public static void main(String[]args) {
fruit fr1=new fruit("Tanya","apple");
fruit fr2=new fruit("Patrick","banana");
fruit fr3=new fruit("Sponge","orange");
fruit[] fruits={fr1,fr2,fr3};
for(fruit fr:fruits)
{
fr.eat();
}
}
}
//this is a package private class which means its members can be accesed from any class in the same package
// and main is in same package as fruit so public not used
//we dont write public class fruit becoz in java the class name should match file name and a file can only have one top level class
class fruit {
String name;
String fruit;
fruit(String name,String fruit)
{
this.name=name;
this.fruit=fruit;
}
void eat()
{
System.out.println(this.name+" is eating "+this.fruit);
}
}