Skip to content

Commit a2ee72b

Browse files
lajitlajit
authored andcommitted
Added inheritance and polymorphism
1 parent a05dfea commit a2ee72b

File tree

14 files changed

+340
-0
lines changed

14 files changed

+340
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import inheritanceExample.ParentCar;
2+
3+
public class ChildCar extends ParentCar {
4+
5+
public static void main(String[] args) {
6+
7+
ChildCar toy = new ChildCar();
8+
toy.drive(); // method name in parent class created in another package
9+
10+
}
11+
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package inheritanceExample;
2+
3+
public class ParentCar {
4+
5+
public int wheels = 4;
6+
int speed = 300;
7+
String body = "Metallic grey";
8+
9+
public void drive() {
10+
if (speed > 300) {
11+
System.out.println("overspeed");
12+
}
13+
else if(speed <= 300) {
14+
System.out.println("slow speed");
15+
}
16+
else {
17+
System.out.println("only wheels: " +wheels);
18+
}
19+
System.out.println("body color is: " +body);
20+
21+
}
22+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package learnAbstract;
2+
3+
public abstract class Audi extends CarParent {
4+
//should inherit all abstract classes defined in parent class (CarParent.class), else change this child class as abstract
5+
6+
public void brand() {
7+
System.out.println("My Audi brand is special");
8+
}
9+
10+
public void speed() {
11+
System.out.println("Audi is high speed > 350 mph");
12+
}
13+
14+
public abstract void drive();
15+
16+
17+
public static void main(String[] args) {
18+
// cannot create object for abstract class
19+
20+
/*Audi adi = new Audi();
21+
adi.brand();
22+
adi.speed();
23+
adi.engine();
24+
*/
25+
}
26+
27+
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package learnAbstract;
2+
3+
public class Benz extends CarParent {
4+
5+
public void color() {
6+
System.out.println("My Benz car color is BLACK");
7+
}
8+
9+
/*Should add ALL abstract methods of parent class (CarParent.class) in this child class, otherwise make this class as abstract class,
10+
and hence we cannot create object for this (abstract) class*/
11+
12+
@Override
13+
public void engine() {
14+
System.out.println("My Benz engine is the best");
15+
}
16+
17+
@Override
18+
public void speed() {
19+
System.out.println("My Benz speed is >300 mph");
20+
}
21+
22+
public static void main(String[] args) {
23+
CarParent benz = new Benz(); // using dynamic polymorphism
24+
benz.engine();
25+
benz.speed();
26+
27+
// should add ALL abstract methods of parent class in this child class, otherwise make this class as abstract class,
28+
// and hence we cannot create object for this (abstract) class
29+
Benz ben = new Benz();
30+
ben.color();
31+
ben.engine();
32+
ben.speed();
33+
34+
}
35+
36+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package learnAbstract;
2+
3+
public abstract class CarParent {
4+
5+
public abstract void engine();
6+
7+
public void wheels() {
8+
System.out.println("I have 4 super power wheels");
9+
}
10+
11+
public abstract void speed();
12+
13+
14+
public static void main(String[] args) {
15+
// TODO Auto-generated method stub
16+
17+
}
18+
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package learnAbstract;
2+
3+
4+
public interface LearnInterface {
5+
6+
String empName = "Ajeeth"; // declared variable is static, final and public by default
7+
8+
9+
/*public void emp() { // body should not be specified
10+
System.out.println("emp test");
11+
}*/
12+
13+
void empDetails(); // by default the modifier type is public for interface
14+
15+
void carDetails();
16+
17+
public void autoDetails();
18+
19+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package learnAbstract;
2+
3+
public class i10 extends CarParent implements LearnInterface {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
8+
}
9+
10+
//Added parent class (carparent.class) Unimplemented methods to avoid changing the class to abstract
11+
@Override
12+
public void engine() {
13+
// TODO Auto-generated method stub
14+
}
15+
16+
@Override
17+
public void speed() {
18+
// TODO Auto-generated method stub
19+
}
20+
21+
public void price() {
22+
System.out.println("cost is high");
23+
}
24+
25+
26+
//Inherited LearnInterface.interface unimplemented methods
27+
public void empDetails() {
28+
// TODO Auto-generated method stub
29+
30+
}
31+
32+
public void carDetails() {
33+
// TODO Auto-generated method stub
34+
35+
}
36+
37+
public void autoDetails() {
38+
// TODO Auto-generated method stub
39+
40+
}
41+
42+
43+
44+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package learnPolymorphism;
2+
3+
public class FriendOL {
4+
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package learnPolymorphism;
2+
3+
public class ManagerOL {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
8+
}
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package learnPolymorphism;
2+
3+
public class ParentsOL {
4+
5+
public static void main(String[] args) {
6+
7+
8+
}
9+
10+
}

0 commit comments

Comments
 (0)