-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass_Objects.java
More file actions
36 lines (30 loc) · 1.43 KB
/
Class_Objects.java
File metadata and controls
36 lines (30 loc) · 1.43 KB
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
35
36
public class Class_Objects {
public static void main(String[] args) {
Box box1 = new Box(4.6, 7.9, 9.9);
// box1.getL();
Box box2 = new Box(box1); // copying the constructor
System.out.println(box1.w + " " + box1.h);
BoxWeight box3 = new BoxWeight(2.2, 3.1, 4.3, 10.5);
BoxWeight box4 = new BoxWeight(box3);
System.out.println(box3.h + " " + box3.weight);
Box box5 = new BoxWeight(2, 3, 4, 8);
System.out.println(box5.l);
// there are many variables in both parent and child classes
// you are given access to variables that are in the ref type i.e. BoxWeight
// hence, you should have access to weight variable
// this also means, that the ones you are trying to access should be initialised
// but here, when the obj itself is of type parent class, how will you call the constructor of child class
// this is why error
// Box box6 = new BoxWeight(2);
// System.out.println(box6);
// Box.greeting();
// BoxWeight box = new BoxWeight();
// System.out.println(box);
// BoxWeight.greeting(); // you can inherit but you cannot override
Box box8 = new Box(34); // 3,4,2 -> Functions Parameters
System.out.println(box8.h);
BoxWeight BOX = new BoxWeight(3);
// System.out.println(BOX.w);
System.out.println(BOX.weight);
}
}