Skip to content

Commit e194a7b

Browse files
oop 3
1 parent 634788a commit e194a7b

12 files changed

Lines changed: 238 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.kunal.properties.inheritance;
2+
3+
public class Box {
4+
private double l;
5+
double h;
6+
double w;
7+
// double weight;
8+
9+
static void greeting() {
10+
System.out.println("Hey, I am in Box class. Greetings!");
11+
}
12+
13+
public double getL() {
14+
return l;
15+
}
16+
17+
Box () {
18+
this.h = -1;
19+
this.l = -1;
20+
this.w = -1;
21+
}
22+
23+
// cube
24+
Box (double side) {
25+
// super(); Object class
26+
this.w = side;
27+
this.l = side;
28+
this.h = side;
29+
}
30+
31+
Box(double l, double h, double w) {
32+
System.out.println("Box class constructor");
33+
this.l = l;
34+
this.h = h;
35+
this.w = w;
36+
}
37+
38+
Box(Box old) {
39+
this.h = old.h;
40+
this.l = old.l;
41+
this.w = old.w;
42+
}
43+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.kunal.properties.inheritance;
2+
3+
public class BoxColor extends BoxWeight {
4+
5+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.kunal.properties.inheritance;
2+
3+
public class BoxPrice extends BoxWeight{
4+
5+
double cost;
6+
7+
BoxPrice () {
8+
super();
9+
this.cost = -1;
10+
}
11+
12+
BoxPrice(BoxPrice other) {
13+
super(other);
14+
this.cost = other.cost;
15+
}
16+
17+
public BoxPrice(double l, double h, double w, double weight, double cost) {
18+
super(l, h, w, weight);
19+
this.cost = cost;
20+
}
21+
22+
public BoxPrice(double side, double weight, double cost) {
23+
super(side, weight);
24+
this.cost = cost;
25+
}
26+
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.kunal.properties.inheritance;
2+
3+
import java.util.ArrayList;
4+
5+
public class BoxWeight extends Box{
6+
double weight;
7+
8+
public BoxWeight() {
9+
this.weight = -1;
10+
}
11+
12+
// @Override
13+
static void greeting() {
14+
System.out.println("Hey, I am in BoxWeight class. Greetings!");
15+
ArrayList list = new ArrayList();
16+
}
17+
18+
BoxWeight (BoxWeight other) {
19+
super(other);
20+
weight = other.weight;
21+
}
22+
23+
BoxWeight(double side, double weight) {
24+
super(side);
25+
this.weight = weight;
26+
}
27+
28+
public BoxWeight(double l, double h, double w, double weight) {
29+
// used to initialise values present in parent class
30+
super(l, h, w); // what is this? call the parent class constructor
31+
// System.out.println(super.weight);
32+
this.weight = weight;
33+
}
34+
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.kunal.properties.inheritance;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
Box box1 = new Box(4.6, 7.9, 9.9);
6+
box1.getL();
7+
Box box2 = new Box(box1);
8+
// System.out.println(box1.w + " " + box1.h);
9+
10+
// BoxWeight box3 = new BoxWeight();
11+
// BoxWeight box4 = new BoxWeight(2, 3, 4, 8);
12+
// System.out.println(box3.h + " " + box3.weight);
13+
14+
15+
// Box box5 = new BoxWeight(2, 3, 4, 8);
16+
// System.out.println(box5.w);
17+
18+
// there are many variables in both parent and child classes
19+
// you are given access to variables that are in the ref type i.e. BoxWeight
20+
// hence, you should have access to weight variable
21+
// this also means, that the ones you are trying to access should be initialised
22+
// but here, when the obj itself is of type parent class, how will you call the constructor of child class
23+
// this is why error
24+
// BoxWeight box6 = new Box(2, 3, 4);
25+
// System.out.println(box6);
26+
27+
// Box.greeting();
28+
29+
BoxWeight box = new BoxWeight();
30+
BoxWeight.greeting(); // you can inherit but you cannot override
31+
}
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.kunal.properties.polymorphism;
2+
3+
public class Circle extends Shapes{
4+
5+
// this will run when obj of Circle is created
6+
// hence it is overriding the parent method
7+
@Override // this is called annotation
8+
void area() {
9+
System.out.println("Area is pie * r * r");
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.kunal.properties.polymorphism;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
Shapes shape = new Shapes();
6+
Shapes circle = new Circle();
7+
Shapes square = new Square();
8+
9+
circle.area();
10+
}
11+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.kunal.properties.polymorphism;
2+
3+
public class Numbers {
4+
5+
double sum(double a, int b) {
6+
return a + b;
7+
}
8+
9+
double sum(int a, int b) {
10+
return a + b;
11+
}
12+
13+
int sum(int a, int b, int c) {
14+
return a + b + c;
15+
}
16+
17+
public static void main(String[] args) {
18+
Numbers obj = new Numbers();
19+
20+
obj.sum(2, 3);
21+
obj.sum(1, 3, 7);
22+
23+
// obj.sum(4, 5, 6, 8);
24+
}
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.kunal.properties.polymorphism;
2+
3+
public class ObjectPrint {
4+
5+
int num;
6+
7+
public ObjectPrint(int num) {
8+
this.num = num;
9+
}
10+
11+
@Override
12+
public String toString() {
13+
return "ObjectPrint{" +
14+
"num=" + num +
15+
'}';
16+
}
17+
18+
public static void main(String[] args) {
19+
ObjectPrint obj = new ObjectPrint(54);
20+
21+
System.out.println(obj);
22+
}
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.kunal.properties.polymorphism;
2+
3+
public class Shapes {
4+
void area() {
5+
System.out.println("I am in shapes");
6+
}
7+
8+
// Early binding: check notes
9+
// final void area() {
10+
// System.out.println("I am in shapes");
11+
// }
12+
}

0 commit comments

Comments
 (0)