-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInheritance.java
More file actions
65 lines (51 loc) Β· 1.41 KB
/
Copy pathInheritance.java
File metadata and controls
65 lines (51 loc) Β· 1.41 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package day8;
import java.util.Scanner;
public class Inheritance {
public static void main(String[] args) {
Shape shape = new Shape();
Circle circle = new Circle();
Square square1 = new Square(10);
Square square2 = new Square(10);
// System.out.println(square1.equals(square2));
System.out.println(square1);
}
private static class Shape {
double area;
double perimeter;
// final methods can't be overriden
final void helloWorld() {
System.out.println("hello world : shape");
}
}
private static class Circle extends Shape {
int radius;
// not possible
// void helloWorld() {
// System.out.println("hello world circle");
// }
}
private static class Square extends Shape {
int side;
Square(int side) {
this.side = side;
}
@Override
public int hashCode() {
return 1000;
}
@Override
public boolean equals(Object object) {
if (object instanceof Square) {
Square other = (Square) object;
return this.side == other.side;
}
return false;
}
@Override
public String toString() {
return "Square{" +
"side=" + side +
'}';
}
}
}