Skip to content

Commit 896c83a

Browse files
committed
Add more about Interfaces
1 parent 65b86c5 commit 896c83a

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

‎14_Abstract_Class_and_Methods/src/Abstract.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// Abstract Class
1414
abstractclassParent{
1515

16+
inta = 100; // You can create properties in abstract class
17+
1618
publicParent(){
1719
System.out.println("I am a constructor of Parent class");
1820
}
@@ -45,12 +47,13 @@ public void th(){
4547
publicclassAbstract{
4648
publicstaticvoidmain(String[] args){
4749

48-
Parentp = newParent(); // Error because Parent is abstract class
50+
// Parent p = new Parent(); // Error because Parent is abstract class
4951

5052
Childc = newChild();
5153
c.greet(); // Good Morning
54+
System.out.println(c.a);
5255

53-
Child2c2 = newChild2(); // Error because Child2 is abstract class
56+
// Child2 c2 = new Child2(); // Error because Child2 is abstract class
5457

5558
}
5659
}
Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
// ? Interface
2-
// * In English, point where two systems meet and interact.
3-
// * In Java, group of related methods with empty bodies.
2+
// * Group of related methods with empty bodies.
43

54
// ? Abstract class vs Interface
6-
// * We can't extend multiple abstract classes
7-
// * But we can implement multiple interfaces.
5+
// * We can't EXTEND multiple abstract classes
6+
// * But we can IMPLEMENT multiple interfaces.
87

9-
// ? Interface
8+
// Interface
109
interfaceBicycle{
10+
11+
inta = 10; // You can create properties in interface, but not modify them (final)
12+
1113
voidapplyBrake(intdecrement);
1214

1315
voidspeedUp(intincrement);
1416
}
1517

16-
// ? implements keyword
17-
classHeroimplementsBicycle{
18+
interfaceHorn{
19+
voidblowHorn();
20+
}
21+
22+
// implements keyword
23+
classHeroimplementsBicycle, Horn{
1824

1925
intspeed = 0; // Initial speed
2026

@@ -28,7 +34,12 @@ public void speedUp(int increment){
2834
speed += increment;
2935
}
3036

31-
voidprintStates(){
37+
@Override
38+
publicvoidblowHorn(){
39+
System.out.println("Pee Pee Poo Poo Dhaichuu!");
40+
}
41+
42+
voidprintSpeed(){
3243
System.out.println("Speed: " + speed);
3344
}
3445
}
@@ -37,9 +48,14 @@ public class Interfaces{
3748
publicstaticvoidmain(String[] args){
3849

3950
Heroh = newHero();
51+
52+
System.out.println("a: " + h.a);
53+
// h.a = 100; // Error because a is final
54+
4055
h.speedUp(10);
4156
h.applyBrake(5);
42-
h.printStates(); // Speed: 5
57+
h.printSpeed();
58+
h.blowHorn();
4359

4460
}
4561
}

0 commit comments

Comments
(0)