File tree Expand file tree Collapse file tree 2 files changed +30
-11
lines changed
14_Abstract_Class_and_Methods/src
15_Interfaces/1_Intro/src Expand file tree Collapse file tree 2 files changed +30
-11
lines changed Original file line number Diff line number Diff line change 1313// Abstract Class
1414abstract class Parent {
1515
16+ int a = 100 ; // You can create properties in abstract class
17+
1618public Parent (){
1719System .out .println ("I am a constructor of Parent class" );
1820 }
@@ -45,12 +47,13 @@ public void th(){
4547public class Abstract {
4648public static void main (String [] args ){
4749
48- Parent p = new Parent (); // Error because Parent is abstract class
50+ // Parent p = new Parent(); // Error because Parent is abstract class
4951
5052Child c = new Child ();
5153c .greet (); // Good Morning
54+ System .out .println (c .a );
5255
53- Child2 c2 = new Child2 (); // Error because Child2 is abstract class
56+ // Child2 c2 = new Child2(); // Error because Child2 is abstract class
5457
5558 }
5659}
Original file line number Diff line number Diff line change 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
109interface Bicycle {
10+
11+ int a = 10 ; // You can create properties in interface, but not modify them (final)
12+
1113void applyBrake (int decrement );
1214
1315void speedUp (int increment );
1416}
1517
16- // ? implements keyword
17- class Hero implements Bicycle {
18+ interface Horn {
19+ void blowHorn ();
20+ }
21+
22+ // implements keyword
23+ class Hero implements Bicycle , Horn {
1824
1925int speed = 0 ; // Initial speed
2026
@@ -28,7 +34,12 @@ public void speedUp(int increment){
2834speed += increment ;
2935 }
3036
31- void printStates (){
37+ @ Override
38+ public void blowHorn (){
39+ System .out .println ("Pee Pee Poo Poo Dhaichuu!" );
40+ }
41+
42+ void printSpeed (){
3243System .out .println ("Speed: " + speed );
3344 }
3445}
@@ -37,9 +48,14 @@ public class Interfaces{
3748public static void main (String [] args ){
3849
3950Hero h = new Hero ();
51+
52+ System .out .println ("a: " + h .a );
53+ // h.a = 100; // Error because a is final
54+
4055h .speedUp (10 );
4156h .applyBrake (5 );
42- h .printStates (); // Speed: 5
57+ h .printSpeed ();
58+ h .blowHorn ();
4359
4460 }
4561}
You can’t perform that action at this time.
0 commit comments